_useStaticRegistry = $staticRegistryName; self::$_staticPrefixToPaths[$staticRegistryName] = array(); self::$_staticLoadedPlugins[$staticRegistryName] = array(); } foreach ($prefixToPaths as $prefix => $path) { $this->addPrefixPath($prefix, $path); } } /** * Format prefix for internal use * * @param string $prefix * @return string */ protected function _formatPrefix($prefix) { return rtrim($prefix, '_') . '_'; } /** * Add prefixed paths to the registry of paths * * @param string $prefix * @param string $path * @return Zend_Loader_PluginLoader */ public function addPrefixPath($prefix, $path) { if (! is_string($prefix) || ! is_string($path)) { require_once 'external/Zend/Loader/PluginLoader/Exception.php'; throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.'); } $prefix = $this->_formatPrefix($prefix); $path = rtrim($path, '/\\') . '/'; if ($this->_useStaticRegistry) { self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path; } else { $this->_prefixToPaths[$prefix][] = $path; } return $this; } /** * Get path stack * * @param string $prefix * @return false|array False if prefix does not exist, array otherwise */ public function getPaths($prefix = null) { if ((null !== $prefix) && is_string($prefix)) { $prefix = $this->_formatPrefix($prefix); if ($this->_useStaticRegistry) { if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) { return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]; } return false; } if (isset($this->_prefixToPaths[$prefix])) { return $this->_prefixToPaths[$prefix]; } return false; } if ($this->_useStaticRegistry) { return self::$_staticPrefixToPaths[$this->_useStaticRegistry]; } return $this->_prefixToPaths; } /** * Clear path stack * * @param string $prefix * @return bool False only if $prefix does not exist */ public function clearPaths($prefix = null) { if ((null !== $prefix) && is_string($prefix)) { $prefix = $this->_formatPrefix($prefix); if ($this->_useStaticRegistry) { if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) { unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]); return true; } return false; } if (isset($this->_prefixToPaths[$prefix])) { unset($this->_prefixToPaths[$prefix]); return true; } return false; } if ($this->_useStaticRegistry) { self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array(); } else { $this->_prefixToPaths = array(); } return true; } /** * Remove a prefix (or prefixed-path) from the registry * * @param string $prefix * @param string $path OPTIONAL * @return Zend_Loader_PluginLoader */ public function removePrefixPath($prefix, $path = null) { $prefix = $this->_formatPrefix($prefix); if ($this->_useStaticRegistry) { $registry = & self::$_staticPrefixToPaths[$this->_useStaticRegistry]; } else { $registry = & $this->_prefixToPaths; } if (! isset($registry[$prefix])) { require_once 'external/Zend/Loader/PluginLoader/Exception.php'; throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.'); } if ($path != null) { $pos = array_search($path, $registry[$prefix]); if ($pos === null) { throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.'); } unset($registry[$prefix][$pos]); } else { unset($registry[$prefix]); } return $this; } /** * Normalize plugin name * * @param string $name * @return string */ protected function _formatName($name) { return ucfirst((string)$name); } /** * Whether or not a Plugin by a specific name is loaded * * @param string $name * @return Zend_Loader_PluginLoader */ public function isLoaded($name) { $name = $this->_formatName($name); if ($this->_useStaticRegistry) { return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]); } return isset($this->_loadedPlugins[$name]); } /** * Return full class name for a named plugin * * @param string $name * @return string|false False if class not found, class name otherwise */ public function getClassName($name) { $name = $this->_formatName($name); if ($this->_useStaticRegistry && isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name])) { return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]; } elseif (isset($this->_loadedPlugins[$name])) { return $this->_loadedPlugins[$name]; } return false; } /** * Load a plugin via the name provided * * @param string $name * @return string */ public function load($name) { $name = $this->_formatName($name); if ($this->_useStaticRegistry) { $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry]; } else { $registry = $this->_prefixToPaths; } if ($this->isLoaded($name)) { return $this->getClassName($name); } $found = false; $registry = array_reverse($registry, true); foreach ($registry as $prefix => $paths) { $paths = array_reverse($paths, true); foreach ($paths as $path) { $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php'; $className = $prefix . $name; if (class_exists($className, false)) { $found = true; break 2; } if (Zend_Loader::isReadable($path . $classFile)) { include_once $path . $classFile; if (! class_exists($className, false)) { throw new Zend_Loader_PluginLoader_Exception('File ' . $classFile . ' was loaded but class named ' . $className . ' was not found within it.'); } $found = true; break 2; } } } if ($found) { if ($this->_useStaticRegistry) { self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className; } else { $this->_loadedPlugins[$name] = $className; } return $className; } require_once 'external/Zend/Loader/PluginLoader/Exception.php'; throw new Zend_Loader_PluginLoader_Exception('Plugin by name ' . $name . ' was not found in the registry.'); } }