PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-1.5.0PR/library/Zend/Loader/PluginLoader.php

https://github.com/bhaumik25/zend-framework
PHP | 337 lines | 175 code | 45 blank | 117 comment | 37 complexity | 50807e0539db5d203de61f890dc82d8d MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @subpackage PluginLoader
  18. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Loader_PluginLoader_Interface */
  22. require_once 'Zend/Loader/PluginLoader/Interface.php';
  23. /** Zend_Loader */
  24. require_once 'Zend/Loader.php';
  25. /**
  26. * Generic plugin class loader
  27. *
  28. * @category Zend
  29. * @package Zend_Loader
  30. * @subpackage PluginLoader
  31. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
  35. {
  36. /**
  37. * Static registry property
  38. *
  39. * @var array
  40. */
  41. static protected $_staticPrefixToPaths = array();
  42. /**
  43. * Instance registry property
  44. *
  45. * @var array
  46. */
  47. protected $_prefixToPaths = array();
  48. /**
  49. * Statically loaded plugins
  50. *
  51. * @var array
  52. */
  53. static protected $_staticLoadedPlugins = array();
  54. /**
  55. * Instance loaded plugins
  56. *
  57. * @var array
  58. */
  59. protected $_loadedPlugins = array();
  60. /**
  61. * Whether to use a statically named registry for loading plugins
  62. *
  63. * @var string|null
  64. */
  65. protected $_useStaticRegistry = null;
  66. /**
  67. * Constructor
  68. *
  69. * @param array $prefixToPaths
  70. * @param string $staticRegistryName OPTIONAL
  71. */
  72. public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
  73. {
  74. if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
  75. $this->_useStaticRegistry = $staticRegistryName;
  76. self::$_staticPrefixToPaths[$staticRegistryName] = array();
  77. self::$_staticLoadedPlugins[$staticRegistryName] = array();
  78. }
  79. foreach ($prefixToPaths as $prefix => $path) {
  80. $this->addPrefixPath($prefix, $path);
  81. }
  82. }
  83. /**
  84. * Format prefix for internal use
  85. *
  86. * @param string $prefix
  87. * @return string
  88. */
  89. protected function _formatPrefix($prefix)
  90. {
  91. return rtrim($prefix, '_') . '_';
  92. }
  93. /**
  94. * Add prefixed paths to the registry of paths
  95. *
  96. * @param string $prefix
  97. * @param string $path
  98. * @return Zend_Loader_PluginLoader
  99. */
  100. public function addPrefixPath($prefix, $path)
  101. {
  102. if (!is_string($prefix) || !is_string($path)) {
  103. require_once 'Zend/Loader/PluginLoader/Exception.php';
  104. throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
  105. }
  106. $prefix = $this->_formatPrefix($prefix);
  107. $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  108. if ($this->_useStaticRegistry) {
  109. self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
  110. } else {
  111. $this->_prefixToPaths[$prefix][] = $path;
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Get path stack
  117. *
  118. * @param string $prefix
  119. * @return false|array False if prefix does not exist, array otherwise
  120. */
  121. public function getPaths($prefix = null)
  122. {
  123. if ((null !== $prefix) && is_string($prefix)) {
  124. $prefix = $this->_formatPrefix($prefix);
  125. if ($this->_useStaticRegistry) {
  126. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  127. return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
  128. }
  129. return false;
  130. }
  131. if (isset($this->_prefixToPaths[$prefix])) {
  132. return $this->_prefixToPaths[$prefix];
  133. }
  134. return false;
  135. }
  136. if ($this->_useStaticRegistry) {
  137. return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  138. }
  139. return $this->_prefixToPaths;
  140. }
  141. /**
  142. * Clear path stack
  143. *
  144. * @param string $prefix
  145. * @return bool False only if $prefix does not exist
  146. */
  147. public function clearPaths($prefix = null)
  148. {
  149. if ((null !== $prefix) && is_string($prefix)) {
  150. $prefix = $this->_formatPrefix($prefix);
  151. if ($this->_useStaticRegistry) {
  152. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  153. unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
  154. return true;
  155. }
  156. return false;
  157. }
  158. if (isset($this->_prefixToPaths[$prefix])) {
  159. unset($this->_prefixToPaths[$prefix]);
  160. return true;
  161. }
  162. return false;
  163. }
  164. if ($this->_useStaticRegistry) {
  165. self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
  166. } else {
  167. $this->_prefixToPaths = array();
  168. }
  169. return true;
  170. }
  171. /**
  172. * Remove a prefix (or prefixed-path) from the registry
  173. *
  174. * @param string $prefix
  175. * @param string $path OPTIONAL
  176. * @return Zend_Loader_PluginLoader
  177. */
  178. public function removePrefixPath($prefix, $path = null)
  179. {
  180. $prefix = $this->_formatPrefix($prefix);
  181. if ($this->_useStaticRegistry) {
  182. $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  183. } else {
  184. $registry =& $this->_prefixToPaths;
  185. }
  186. if (!isset($registry[$prefix])) {
  187. require_once 'Zend/Loader/PluginLoader/Exception.php';
  188. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
  189. }
  190. if ($path != null) {
  191. $pos = array_search($path, $registry[$prefix]);
  192. if ($pos === null) {
  193. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
  194. }
  195. unset($registry[$prefix][$pos]);
  196. } else {
  197. unset($registry[$prefix]);
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Normalize plugin name
  203. *
  204. * @param string $name
  205. * @return string
  206. */
  207. protected function _formatName($name)
  208. {
  209. return ucfirst((string) $name);
  210. }
  211. /**
  212. * Whether or not a Plugin by a specific name is loaded
  213. *
  214. * @param string $name
  215. * @return Zend_Loader_PluginLoader
  216. */
  217. public function isLoaded($name)
  218. {
  219. $name = $this->_formatName($name);
  220. if ($this->_useStaticRegistry) {
  221. return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
  222. }
  223. return isset($this->_loadedPlugins[$name]);
  224. }
  225. /**
  226. * Return full class name for a named plugin
  227. *
  228. * @param string $name
  229. * @return string|false False if class not found, class name otherwise
  230. */
  231. public function getClassName($name)
  232. {
  233. $name = $this->_formatName($name);
  234. if ($this->_useStaticRegistry &&
  235. isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]))
  236. {
  237. return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
  238. } elseif (isset($this->_loadedPlugins[$name])) {
  239. return $this->_loadedPlugins[$name];
  240. }
  241. return false;
  242. }
  243. /**
  244. * Load a plugin via the name provided
  245. *
  246. * @param string $name
  247. * @return string
  248. */
  249. public function load($name)
  250. {
  251. $name = $this->_formatName($name);
  252. if ($this->_useStaticRegistry) {
  253. $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  254. } else {
  255. $registry = $this->_prefixToPaths;
  256. }
  257. if ($this->isLoaded($name)) {
  258. return $this->getClassName($name);
  259. }
  260. $found = false;
  261. $registry = array_reverse($registry, true);
  262. foreach ($registry as $prefix => $paths) {
  263. $paths = array_reverse($paths, true);
  264. foreach ($paths as $path) {
  265. $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
  266. $className = $prefix . $name;
  267. if (class_exists($className, false)) {
  268. $found = true;
  269. break 2;
  270. }
  271. if (Zend_Loader::isReadable($path . $classFile)) {
  272. include_once $path . $classFile;
  273. if (!class_exists($className, false)) {
  274. throw new Zend_Loader_PluginLoader_Exception('File ' . $classFile . ' was loaded but class named ' . $className . ' was not found within it.');
  275. }
  276. $found = true;
  277. break 2;
  278. }
  279. }
  280. }
  281. if ($found) {
  282. if ($this->_useStaticRegistry) {
  283. self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
  284. } else {
  285. $this->_loadedPlugins[$name] = $className;
  286. }
  287. return $className;
  288. }
  289. require_once 'Zend/Loader/PluginLoader/Exception.php';
  290. throw new Zend_Loader_PluginLoader_Exception('Plugin by name ' . $name . ' was not found in the registry.');
  291. }
  292. }