Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Loader/PluginLoader.php
Go to the documentation of this file.
00001 <?php
00024 require_once 'Zend/Loader/PluginLoader/Interface.php';
00025 
00027 require_once 'Zend/Loader.php';
00028 
00038 class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
00039 {
00044     protected static $_includeFileCache;
00045 
00051     protected $_loadedPluginPaths = array();
00052 
00058     protected $_loadedPlugins = array();
00059 
00065     protected $_prefixToPaths = array();
00066 
00072     protected static $_staticLoadedPluginPaths = array();
00073 
00079     protected static $_staticLoadedPlugins = array();
00080 
00086     protected static $_staticPrefixToPaths = array();
00087 
00093     protected $_useStaticRegistry = null;
00094 
00101     public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
00102     {
00103         if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
00104             $this->_useStaticRegistry = $staticRegistryName;
00105             if(!isset(self::$_staticPrefixToPaths[$staticRegistryName])) {
00106                 self::$_staticPrefixToPaths[$staticRegistryName] = array();
00107             }
00108             if(!isset(self::$_staticLoadedPlugins[$staticRegistryName])) {
00109                 self::$_staticLoadedPlugins[$staticRegistryName] = array();
00110             }
00111         }
00112 
00113         foreach ($prefixToPaths as $prefix => $path) {
00114             $this->addPrefixPath($prefix, $path);
00115         }
00116     }
00117 
00124     protected function _formatPrefix($prefix)
00125     {
00126         if($prefix == "") {
00127             return $prefix;
00128         }
00129 
00130         $last = strlen($prefix) - 1;
00131         if ($prefix{$last} == '\\') {
00132             return $prefix;
00133         }
00134 
00135         return rtrim($prefix, '_') . '_';
00136     }
00137 
00145     public function addPrefixPath($prefix, $path)
00146     {
00147         if (!is_string($prefix) || !is_string($path)) {
00148             require_once 'Zend/Loader/PluginLoader/Exception.php';
00149             throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
00150         }
00151 
00152         $prefix = $this->_formatPrefix($prefix);
00153         $path   = rtrim($path, '/\\') . '/';
00154 
00155         if ($this->_useStaticRegistry) {
00156             self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
00157         } else {
00158             if (!isset($this->_prefixToPaths[$prefix])) {
00159                 $this->_prefixToPaths[$prefix] = array();
00160             }
00161             if (!in_array($path, $this->_prefixToPaths[$prefix])) {
00162                 $this->_prefixToPaths[$prefix][] = $path;
00163             }
00164         }
00165         return $this;
00166     }
00167 
00174     public function getPaths($prefix = null)
00175     {
00176         if ((null !== $prefix) && is_string($prefix)) {
00177             $prefix = $this->_formatPrefix($prefix);
00178             if ($this->_useStaticRegistry) {
00179                 if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
00180                     return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
00181                 }
00182 
00183                 return false;
00184             }
00185 
00186             if (isset($this->_prefixToPaths[$prefix])) {
00187                 return $this->_prefixToPaths[$prefix];
00188             }
00189 
00190             return false;
00191         }
00192 
00193         if ($this->_useStaticRegistry) {
00194             return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
00195         }
00196 
00197         return $this->_prefixToPaths;
00198     }
00199 
00206     public function clearPaths($prefix = null)
00207     {
00208         if ((null !== $prefix) && is_string($prefix)) {
00209             $prefix = $this->_formatPrefix($prefix);
00210             if ($this->_useStaticRegistry) {
00211                 if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
00212                     unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
00213                     return true;
00214                 }
00215 
00216                 return false;
00217             }
00218 
00219             if (isset($this->_prefixToPaths[$prefix])) {
00220                 unset($this->_prefixToPaths[$prefix]);
00221                 return true;
00222             }
00223 
00224             return false;
00225         }
00226 
00227         if ($this->_useStaticRegistry) {
00228             self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
00229         } else {
00230             $this->_prefixToPaths = array();
00231         }
00232 
00233         return true;
00234     }
00235 
00243     public function removePrefixPath($prefix, $path = null)
00244     {
00245         $prefix = $this->_formatPrefix($prefix);
00246         if ($this->_useStaticRegistry) {
00247             $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
00248         } else {
00249             $registry =& $this->_prefixToPaths;
00250         }
00251 
00252         if (!isset($registry[$prefix])) {
00253             require_once 'Zend/Loader/PluginLoader/Exception.php';
00254             throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
00255         }
00256 
00257         if ($path != null) {
00258             $pos = array_search($path, $registry[$prefix]);
00259             if ($pos === null) {
00260                 require_once 'Zend/Loader/PluginLoader/Exception.php';
00261                 throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
00262             }
00263             unset($registry[$prefix][$pos]);
00264         } else {
00265             unset($registry[$prefix]);
00266         }
00267 
00268         return $this;
00269     }
00270 
00277     protected function _formatName($name)
00278     {
00279         return ucfirst((string) $name);
00280     }
00281 
00288     public function isLoaded($name)
00289     {
00290         $name = $this->_formatName($name);
00291         if ($this->_useStaticRegistry) {
00292             return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
00293         }
00294 
00295         return isset($this->_loadedPlugins[$name]);
00296     }
00297 
00304     public function getClassName($name)
00305     {
00306         $name = $this->_formatName($name);
00307         if ($this->_useStaticRegistry
00308             && isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name])
00309         ) {
00310             return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
00311         } elseif (isset($this->_loadedPlugins[$name])) {
00312             return $this->_loadedPlugins[$name];
00313         }
00314 
00315         return false;
00316     }
00317 
00324     public function getClassPath($name)
00325     {
00326         $name = $this->_formatName($name);
00327         if ($this->_useStaticRegistry
00328             && !empty(self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name])
00329         ) {
00330             return self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name];
00331         } elseif (!empty($this->_loadedPluginPaths[$name])) {
00332             return $this->_loadedPluginPaths[$name];
00333         }
00334 
00335         if ($this->isLoaded($name)) {
00336             $class = $this->getClassName($name);
00337             $r     = new ReflectionClass($class);
00338             $path  = $r->getFileName();
00339             if ($this->_useStaticRegistry) {
00340                 self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = $path;
00341             } else {
00342                 $this->_loadedPluginPaths[$name] = $path;
00343             }
00344             return $path;
00345         }
00346 
00347         return false;
00348     }
00349 
00360     public function load($name, $throwExceptions = true)
00361     {
00362         $name = $this->_formatName($name);
00363         if ($this->isLoaded($name)) {
00364             return $this->getClassName($name);
00365         }
00366 
00367         if ($this->_useStaticRegistry) {
00368             $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
00369         } else {
00370             $registry = $this->_prefixToPaths;
00371         }
00372 
00373         $registry  = array_reverse($registry, true);
00374         $found     = false;
00375         $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
00376         $incFile   = self::getIncludeFileCache();
00377         foreach ($registry as $prefix => $paths) {
00378             $className = $prefix . $name;
00379 
00380             if (class_exists($className, false)) {
00381                 $found = true;
00382                 break;
00383             }
00384 
00385             $paths     = array_reverse($paths, true);
00386 
00387             foreach ($paths as $path) {
00388                 $loadFile = $path . $classFile;
00389                 if (Zend_Loader::isReadable($loadFile)) {
00390                     include_once $loadFile;
00391                     if (class_exists($className, false)) {
00392                         if (null !== $incFile) {
00393                             self::_appendIncFile($loadFile);
00394                         }
00395                         $found = true;
00396                         break 2;
00397                     }
00398                 }
00399             }
00400         }
00401 
00402         if (!$found) {
00403             if (!$throwExceptions) {
00404                 return false;
00405             }
00406 
00407             $message = "Plugin by name '$name' was not found in the registry; used paths:";
00408             foreach ($registry as $prefix => $paths) {
00409                 $message .= "\n$prefix: " . implode(PATH_SEPARATOR, $paths);
00410             }
00411             require_once 'Zend/Loader/PluginLoader/Exception.php';
00412             throw new Zend_Loader_PluginLoader_Exception($message);
00413        }
00414 
00415         if ($this->_useStaticRegistry) {
00416             self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]     = $className;
00417         } else {
00418             $this->_loadedPlugins[$name]     = $className;
00419         }
00420         return $className;
00421     }
00422 
00433     public static function setIncludeFileCache($file)
00434     {
00435         if (null === $file) {
00436             self::$_includeFileCache = null;
00437             return;
00438         }
00439 
00440         if (!file_exists($file) && !file_exists(dirname($file))) {
00441             require_once 'Zend/Loader/PluginLoader/Exception.php';
00442             throw new Zend_Loader_PluginLoader_Exception('Specified file does not exist and/or directory does not exist (' . $file . ')');
00443         }
00444         if (file_exists($file) && !is_writable($file)) {
00445             require_once 'Zend/Loader/PluginLoader/Exception.php';
00446             throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
00447         }
00448         if (!file_exists($file) && file_exists(dirname($file)) && !is_writable(dirname($file))) {
00449             require_once 'Zend/Loader/PluginLoader/Exception.php';
00450             throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
00451         }
00452 
00453         self::$_includeFileCache = $file;
00454     }
00455 
00461     public static function getIncludeFileCache()
00462     {
00463         return self::$_includeFileCache;
00464     }
00465 
00472     protected static function _appendIncFile($incFile)
00473     {
00474         if (!file_exists(self::$_includeFileCache)) {
00475             $file = '<?php';
00476         } else {
00477             $file = file_get_contents(self::$_includeFileCache);
00478         }
00479         if (!strstr($file, $incFile)) {
00480             $file .= "\ninclude_once '$incFile';";
00481             file_put_contents(self::$_includeFileCache, $file);
00482         }
00483     }
00484 }
 All Data Structures Namespaces Files Functions Variables Enumerations