<?php
require_once 'Zend/Loader/PluginLoader/Interface.php';
require_once 'Zend/Loader.php';
class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
{
protected static $_includeFileCache;
protected $_loadedPluginPaths = array();
protected $_loadedPlugins = array();
protected $_prefixToPaths = array();
protected static $_staticLoadedPluginPaths = array();
protected static $_staticLoadedPlugins = array();
protected static $_staticPrefixToPaths = array();
protected $_useStaticRegistry = null;
public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
{
if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
$this->_useStaticRegistry = $staticRegistryName;
if(!isset(self::$_staticPrefixToPaths[$staticRegistryName])) {
self::$_staticPrefixToPaths[$staticRegistryName] = array();
}
if(!isset(self::$_staticLoadedPlugins[$staticRegistryName])) {
self::$_staticLoadedPlugins[$staticRegistryName] = array();
}
}
foreach ($prefixToPaths as $prefix => $path) {
$this->addPrefixPath($prefix, $path);
}
}
protected function _formatPrefix($prefix)
{
if($prefix == "") {
return $prefix;
}
return rtrim($prefix, '_') . '_';
}
public function addPrefixPath($prefix, $path)
{
if (!is_string($prefix) || !is_string($path)) {
require_once '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 {
if (!isset($this->_prefixToPaths[$prefix])) {
$this->_prefixToPaths[$prefix] = array();
}
if (!in_array($path, $this->_prefixToPaths[$prefix])) {
$this->_prefixToPaths[$prefix][] = $path;
}
}
return $this;
}
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;
}
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;
}
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 '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) {
require_once 'Zend/Loader/PluginLoader/Exception.php';
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;
}
protected function _formatName($name)
{
return ucfirst((string) $name);
}
public function isLoaded($name)
{
$name = $this->_formatName($name);
if ($this->_useStaticRegistry) {
return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
}
return isset($this->_loadedPlugins[$name]);
}
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;
}
public function getClassPath($name)
{
$name = $this->_formatName($name);
if ($this->_useStaticRegistry
&& !empty(self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name])
) {
return self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name];
} elseif (!empty($this->_loadedPluginPaths[$name])) {
return $this->_loadedPluginPaths[$name];
}
if ($this->isLoaded($name)) {
$class = $this->getClassName($name);
$r = new ReflectionClass($class);
$path = $r->getFileName();
if ($this->_useStaticRegistry) {
self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = $path;
} else {
$this->_loadedPluginPaths[$name] = $path;
}
return $path;
}
return false;
}
public function load($name, $throwExceptions = true)
{
$name = $this->_formatName($name);
if ($this->isLoaded($name)) {
return $this->getClassName($name);
}
if ($this->_useStaticRegistry) {
$registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
} else {
$registry = $this->_prefixToPaths;
}
$registry = array_reverse($registry, true);
$found = false;
$classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
$incFile = self::getIncludeFileCache();
foreach ($registry as $prefix => $paths) {
$className = $prefix . $name;
if (class_exists($className, false)) {
$found = true;
break;
}
$paths = array_reverse($paths, true);
foreach ($paths as $path) {
$loadFile = $path . $classFile;
if (Zend_Loader::isReadable($loadFile)) {
include_once $loadFile;
if (class_exists($className, false)) {
if (null !== $incFile) {
self::_appendIncFile($loadFile);
}
$found = true;
break 2;
}
}
}
}
if (!$found) {
if (!$throwExceptions) {
return false;
}
$message = "Plugin by name '$name' was not found in the registry; used paths:";
foreach ($registry as $prefix => $paths) {
$message .= "\n$prefix: " . implode(PATH_SEPARATOR, $paths);
}
require_once 'Zend/Loader/PluginLoader/Exception.php';
throw new Zend_Loader_PluginLoader_Exception($message);
}
if ($this->_useStaticRegistry) {
self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = (isset($loadFile) ? $loadFile : '');
} else {
$this->_loadedPlugins[$name] = $className;
$this->_loadedPluginPaths[$name] = (isset($loadFile) ? $loadFile : '');
}
return $className;
}
public static function setIncludeFileCache($file)
{
if (null === $file) {
self::$_includeFileCache = null;
return;
}
if (!file_exists($file) && !file_exists(dirname($file))) {
require_once 'Zend/Loader/PluginLoader/Exception.php';
throw new Zend_Loader_PluginLoader_Exception('Specified file does not exist and/or directory does not exist (' . $file . ')');
}
if (file_exists($file) && !is_writable($file)) {
require_once 'Zend/Loader/PluginLoader/Exception.php';
throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
}
if (!file_exists($file) && file_exists(dirname($file)) && !is_writable(dirname($file))) {
require_once 'Zend/Loader/PluginLoader/Exception.php';
throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
}
self::$_includeFileCache = $file;
}
public static function getIncludeFileCache()
{
return self::$_includeFileCache;
}
protected static function _appendIncFile($incFile)
{
if (!file_exists(self::$_includeFileCache)) {
$file = '<?php';
} else {
$file = file_get_contents(self::$_includeFileCache);
}
if (!strstr($file, $incFile)) {
$file .= "\ninclude_once '$incFile';";
file_put_contents(self::$_includeFileCache, $file);
}
}
}