Show
Ignore:
Timestamp:
09/05/06 21:44:30 (2 years ago)
Author:
gerd
Message:

Added feature: addModuleDir / addModuleBaseDir in patForms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/patForms.php

    r374 r378  
    208208    'defaultAttributes' =>  array(), 
    209209    'elementCounter'    =>  0, 
     210    'moduleDirs'        =>  array( '__all' => array() ) 
    210211); 
    211212 
     
    561562    */ 
    562563    var $observers = array(); 
     564 
     565   /** 
     566    * add a directory where patForms should search for 
     567    * modules. 
     568    * 
     569    * You may either pass a string or an array of directories. 
     570    * 
     571    * patTemplate will be searching for a module in the same 
     572    * order you added them. If the module cannot be found in 
     573    * the custom folders, it will look in 
     574    * patForms/$moduleType. 
     575    * 
     576    * @static 
     577    * @access   public 
     578    * @param    string          module type or NULL for any 
     579    * @param    string|array    directory or directories to search. 
     580    */ 
     581    function addModuleDir($moduleType, $dir) 
     582    { 
     583        $dirs   =&  patForms::getStaticProperty( 'moduleDirs' ); 
     584 
     585        if (!is_array($dir)) { 
     586            $dir    =   array($dir); 
     587        } 
     588 
     589        if ($moduleType === null) { 
     590            $dirs['__all']  =   array_merge($dirs['__all'], $dir); 
     591            return true; 
     592        } 
     593 
     594        if (!isset($dirs[$moduleType])) { 
     595            $dirs[$moduleType]    =   array(); 
     596        } 
     597 
     598        $dirs[$moduleType]    =   array_merge($dirs[$moduleType], $dir); 
     599        return true; 
     600    } 
     601     
     602   /** 
     603    * add a base directory where patForms should search for 
     604    * modules. 
     605    * 
     606    * You may either pass a string or an array of directories. 
     607    * 
     608    * patForms will be searching for a module in the same 
     609    * order you added them. It will first look in module specific  
     610    * directory (added with addModuleDir()), afterwards in module  
     611    * base dirs. If the module cannot be found in the custom  
     612    * folders, it will look in patForms/$moduleType. 
     613    * 
     614    * @static 
     615    * @access   public 
     616    * @param    string|array    directory or directories to search. 
     617    * @see addModuleDir()  
     618    */ 
     619    function addModuleBaseDir($dir) 
     620    { 
     621        return patForms::addModuleDir(null, $dir); 
     622    } 
    563623 
    564624   /** 
     
    14661526        $moduleClass = patForms::_loadModuleClass($type, $name, $hasBaseClass); 
    14671527        if (patErrorManager::isError($moduleClass)) { 
    1468             return $result
     1528            return $moduleClass
    14691529        } 
    14701530 
     
    14731533    } 
    14741534 
     1535   /** 
     1536    * load module class  
     1537    * 
     1538    * @access   private 
     1539    * @param    string  $type       type of the module. Possible values are 'Renderer', 'Rule' 
     1540    * @param    string  $name       The name of the renderer to create - have a look at the renderers/ subfolder for a list of available renderers. 
     1541    * @return   bool    $hasBaseClass   whether this module dependents on a base class or not, default is "true" 
     1542    */ 
    14751543    function _loadModuleClass($type, $name, $hasBaseClass = true) 
    14761544    { 
     1545        $moduleClass = 'patForms_'.$type.'_'.$name; 
     1546        if (class_exists($moduleClass)) { 
     1547            return $moduleClass; 
     1548        } 
     1549 
    14771550        if ($hasBaseClass) { 
    1478             $baseFile  = PATFORMS_INCLUDE_PATH . '/'.$type.'.php'; 
    14791551            $baseClass = 'patForms_'.$type; 
    14801552            if (!class_exists( $baseClass)) { 
     1553                $baseFile  = PATFORMS_INCLUDE_PATH . '/'.$type.'.php'; 
    14811554                if (!file_exists($baseFile)) { 
    14821555                    return patErrorManager::raiseError( 
     
    14901563        } 
    14911564 
    1492         // if there is an underscore in the module name, we want 
    1493         // to load the module from a subfolder, so we transform 
    1494         // all underscores to slashes. 
     1565        /*  
     1566         * if there is an underscore in the module name, we want 
     1567         * to load the module from a subfolder, so we transform 
     1568         * all underscores to slashes. 
     1569         */  
    14951570        $pathName = $name; 
    14961571        if (strstr($pathName, '_' )) { 
     
    14981573        } 
    14991574 
    1500         $moduleFile  = PATFORMS_INCLUDE_PATH . '/'.$type.'/'.$pathName.'.php'; 
    1501         $moduleClass = 'patForms_'.$type.'_'.$name; 
    1502  
    1503  
    1504         if (!class_exists($moduleClass)) { 
    1505             if (!file_exists( $moduleFile)) { 
    1506                 $error = patErrorManager::raiseError( 
    1507                     PATFORMS_ERROR_MODULE_NOT_FOUND, 
    1508                     $type.' "'.$name.'" file "'.$moduleFile. '" could not be found.' 
    1509                 ); 
    1510                 return $error; 
    1511             } 
    1512             include_once $moduleFile; 
    1513         } 
     1575        // where are the include folders? 
     1576        $moduleDirs =   patForms::getStaticProperty('moduleDirs'); 
     1577 
     1578        // special module dirs 
     1579        if (!isset( $moduleDirs[$type])) { 
     1580            $moduleDirs[$type]  =   array(); 
     1581        } 
     1582 
     1583        // other module folder follow standard directory structure 
     1584        foreach ($moduleDirs['__all'] as $all) { 
     1585            $moduleDirs[$type][]    =   $all . '/' . $type; 
     1586        } 
     1587 
     1588        // patForms standard folder 
     1589        $moduleDirs[$type][]    =   PATFORMS_INCLUDE_PATH . '/' . $type; 
     1590         
     1591        $found  =   false; 
     1592        foreach ($moduleDirs[$type] as $dir) { 
     1593            if (!file_exists($dir . '/' . $pathName . '.php')) { 
     1594                continue; 
     1595            } 
     1596 
     1597            include_once $dir . '/' . $pathName . '.php'; 
     1598            $found = true; 
     1599            break; 
     1600        } 
     1601 
     1602        if (!$found) { 
     1603            $error =& patErrorManager::raiseError( 
     1604                PATFORMS_ERROR_MODULE_NOT_FOUND, 
     1605                $type.' "'.$name.'" could not be found in module dirs: "'. implode( '", "', $moduleDirs[$type] ) .'".' 
     1606            ); 
     1607            return $error; 
     1608        } 
     1609 
    15141610        return $moduleClass; 
    15151611    } 
     
    17361832    *      ) 
    17371833    * </pre> 
    1738    
     1834   
    17391835    * @access   public 
    17401836    * @param    string  $name       The name of the element