[TYPO3-ect] Hippie loader
Elmar Hinz
elmar.DOT.hinz at team.MINUS.red.DOT.net
Fri Mar 31 10:54:16 CEST 2006
Elmar Hinz schrieb:
> Hello typoniks,
>
> my good old emacs provides me with a pretty function that tries a
> bunch of different rules to expand the beginning of a word to
> something usefull. It's called hippie-expand.
>
> With a similar idea I write some functions that try to guess an
> extension key or a load and instanciate a class from a minimum of
> given informations.
>
I found out that I needed to build diferent types of loaders first,
before I can combine them to the hippieLoader. The pearLoader is
ready, but tx_lib currently to chaotic to publish. So here comes a
preview of the pearLoader:
<?php
/***************************************************************
* Copyright notice
*
* (c) 2006 Elmar Hinz
* Contact: elmar.hinz at team-red.net
* All rights reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
***************************************************************/
require_once(t3lib_extMgm::extPath('div') . 'class.tx_div.php');
class tx_lib_pearLoader{
/**
* Load a pear class
*
* Loads from extension directories ext, sysext, etc.
*
* with class only:
* tx_key '.../ext/key.php' (yet not supported by manager)
* tx_key_file '.../ext/key/file.php'
* tx_key_subs_file '.../ext/key/subs/file.php'
*
* without alternative key 'alt':
* tx_key '.../ext/key.php' (yet not supported by manager)
* tx_key_file '.../ext/alt/file.php'
* tx_key_subs_file '.../ext/alt/subs/file.php'
*
* with prefix 'class.' and suffix '.inc.php'
* tx_key '.../ext/class.key.inc.php' (yet not
supported by manager)
* tx_key_file '.../ext/key/class.file.inc.php'
* tx_key_subs_file '.../ext/key/subs/class.file.inc.php'
*
* @par string classname
* @par string extension key that varies from classnames
* @par string prefix of classname
* @par string ending of classname
* @return void
*/
function load($class, $alternativeKey='', $prefix = '', $suffix =
'.php'){
$path = tx_lib_pearLoader::_find($class, $alternativeKey,
$prefix, $suffix);
if($path) {
require_once($path);
if(t3lib_extMgm::isLoaded($class)) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
/**
* Load a pear class and make an instance
*
* See load. Returns ux_ extension class if any.
*
* @par string classname
* @par string extension key that varies from classnames
* @par string prefix of classname
* @par string ending of classname
* @return object instance of class
*/
function makeInstance($class, $alternativeKey='', $prefix = '',
$suffix = '.php'){
tx_lib_pearLoader::load($class, $alternativeKey, $prefix,
$suffix);
return t3lib_div::makeInstance($class); // includes ux_ classes
}
/**
* Load a pear class and make an instance
*
* See load. Returns ux_ extension classname if any.
*
* @par string classname
* @par string extension key that varies from classnames
* @par string prefix of classname
* @par string ending of classname
* @return string classname or ux_ classname
*/
function makeInstanceClassName($class, $alternativeKey='', $prefix
= '', $suffix = '.php'){
tx_lib_pearLoader::load($class, $alternativeKey, $prefix,
$suffix);
return t3lib_div::makeInstanceClassName($class); // returns
ux_ classes
}
/****************************************************************
* Private functions
****************************************************************/
/**
* Find path to load
*
* see load
*
* @par string see tx_div::laodPath
* @par string classname prefix i.e. 'class.'
* @return string the path, FALSE if invalid
*/
function _find($class, $alternativeKey='', $prefix = '', $suffix =
'.php'){
if(preg_match('/^tx_[A-Za-z]+.*$/', $class)){ // with tx_ prefix
$parts = split('_', trim($class));
array_shift($parts); // strip tx
}elseif(preg_match('/^[A-Za-z]+.*$/', $class)){ // without tx_
prefix
$parts = split('_', trim($class));
}else{
$error = 'classError';
}
// The last part is the file anyway. Pop off.
if(!$error && $last = array_pop($parts)){
$file = $prefix . $last . $suffix;
}
// Now we find out the root
if(!$error && $alternativeKey){
if(count($parts)> 0) {
// If the key is given as argument it can either
replace the first part
// of the classname or precede it. We shift it into
// $firstWhileKeyIsGiven to remember for the case of
cases.
$firstWhileKeyIsGiven = array_shift($parts) . '/';
$root = t3lib_extMgm::extPath($alternativeKey);
}else{
// If there is no part left now, the file is in the
common extension root.
// We need to pop off the extension directory again.
if(preg_match('|(.*/)[^/]*/|',
t3lib_extMgm::extPath($alternativeKey),
$matches)){
$root = $matches[1];
} else {
die('Programming error');
}
}
}elseif(!$error){
if(count($parts) > 0) {
// If there is still a first part, it gives us the
extension key.
$first = array_shift($parts);
if($key = tx_div::getValidKey($first)){
$root = t3lib_extMgm::extPath($key);
} else {
$error = 'invalidKey 2';
}
}else {
// If there is not a first part left, the file is in
the common extension root.
// We have to find the extension path by filename
"last" in this case.
// We need to pop off the extension directory again.
if($key = tx_div::getValidKey($last)){
if(preg_match('|(.*/)[^/]*/|',
t3lib_extMgm::extPath($key),
$matches)){ // pop directory
$root = $matches[1];
} else {
die('Programming error');
}
} else {
$error = 'invalidKey 1';
}
}
}
// In case of subdirectories loop them.
if(!$error && count($parts) > 0) {
$loop = '';
foreach($parts as $part) {
$loop .= $part . '/';
}
}
// Combine the elements and test the path.
if(!$error && !$path && !is_file($path = $root . $loop . $file)) {
$path = FALSE;
}
if(!$error && !$path
&& !is_file($path = $root . $firstWhileKeyIsGiven . $loop .
$file)) {
$error = 'fileNotFoundError';
}
if($error){
$path = FALSE;
}
return $path;
}
private function v($value){
print chr(10);
print_r($value);
print chr(10);
}
}
?>
More information about the TYPO3-team-extension-coordination
mailing list