2
0

PluginLoader.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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-2008 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-2008 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. * Class map cache file
  38. * @var string
  39. */
  40. protected static $_includeFileCache;
  41. /**
  42. * Instance loaded plugin paths
  43. *
  44. * @var array
  45. */
  46. protected $_loadedPluginPaths = array();
  47. /**
  48. * Instance loaded plugins
  49. *
  50. * @var array
  51. */
  52. protected $_loadedPlugins = array();
  53. /**
  54. * Instance registry property
  55. *
  56. * @var array
  57. */
  58. protected $_prefixToPaths = array();
  59. /**
  60. * Statically loaded plugin path mappings
  61. *
  62. * @var array
  63. */
  64. protected static $_staticLoadedPluginPaths = array();
  65. /**
  66. * Statically loaded plugins
  67. *
  68. * @var array
  69. */
  70. protected static $_staticLoadedPlugins = array();
  71. /**
  72. * Static registry property
  73. *
  74. * @var array
  75. */
  76. protected static $_staticPrefixToPaths = array();
  77. /**
  78. * Whether to use a statically named registry for loading plugins
  79. *
  80. * @var string|null
  81. */
  82. protected $_useStaticRegistry = null;
  83. /**
  84. * Constructor
  85. *
  86. * @param array $prefixToPaths
  87. * @param string $staticRegistryName OPTIONAL
  88. */
  89. public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
  90. {
  91. if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
  92. $this->_useStaticRegistry = $staticRegistryName;
  93. if(!isset(self::$_staticPrefixToPaths[$staticRegistryName])) {
  94. self::$_staticPrefixToPaths[$staticRegistryName] = array();
  95. }
  96. if(!isset(self::$_staticLoadedPlugins[$staticRegistryName])) {
  97. self::$_staticLoadedPlugins[$staticRegistryName] = array();
  98. }
  99. }
  100. foreach ($prefixToPaths as $prefix => $path) {
  101. $this->addPrefixPath($prefix, $path);
  102. }
  103. }
  104. /**
  105. * Format prefix for internal use
  106. *
  107. * @param string $prefix
  108. * @return string
  109. */
  110. protected function _formatPrefix($prefix)
  111. {
  112. if($prefix == "") {
  113. return $prefix;
  114. }
  115. return rtrim($prefix, '_') . '_';
  116. }
  117. /**
  118. * Add prefixed paths to the registry of paths
  119. *
  120. * @param string $prefix
  121. * @param string $path
  122. * @return Zend_Loader_PluginLoader
  123. */
  124. public function addPrefixPath($prefix, $path)
  125. {
  126. if (!is_string($prefix) || !is_string($path)) {
  127. require_once 'Zend/Loader/PluginLoader/Exception.php';
  128. throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
  129. }
  130. $prefix = $this->_formatPrefix($prefix);
  131. $path = rtrim($path, '/\\') . '/';
  132. if ($this->_useStaticRegistry) {
  133. self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
  134. } else {
  135. $this->_prefixToPaths[$prefix][] = $path;
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Get path stack
  141. *
  142. * @param string $prefix
  143. * @return false|array False if prefix does not exist, array otherwise
  144. */
  145. public function getPaths($prefix = null)
  146. {
  147. if ((null !== $prefix) && is_string($prefix)) {
  148. $prefix = $this->_formatPrefix($prefix);
  149. if ($this->_useStaticRegistry) {
  150. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  151. return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
  152. }
  153. return false;
  154. }
  155. if (isset($this->_prefixToPaths[$prefix])) {
  156. return $this->_prefixToPaths[$prefix];
  157. }
  158. return false;
  159. }
  160. if ($this->_useStaticRegistry) {
  161. return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  162. }
  163. return $this->_prefixToPaths;
  164. }
  165. /**
  166. * Clear path stack
  167. *
  168. * @param string $prefix
  169. * @return bool False only if $prefix does not exist
  170. */
  171. public function clearPaths($prefix = null)
  172. {
  173. if ((null !== $prefix) && is_string($prefix)) {
  174. $prefix = $this->_formatPrefix($prefix);
  175. if ($this->_useStaticRegistry) {
  176. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  177. unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
  178. return true;
  179. }
  180. return false;
  181. }
  182. if (isset($this->_prefixToPaths[$prefix])) {
  183. unset($this->_prefixToPaths[$prefix]);
  184. return true;
  185. }
  186. return false;
  187. }
  188. if ($this->_useStaticRegistry) {
  189. self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
  190. } else {
  191. $this->_prefixToPaths = array();
  192. }
  193. return true;
  194. }
  195. /**
  196. * Remove a prefix (or prefixed-path) from the registry
  197. *
  198. * @param string $prefix
  199. * @param string $path OPTIONAL
  200. * @return Zend_Loader_PluginLoader
  201. */
  202. public function removePrefixPath($prefix, $path = null)
  203. {
  204. $prefix = $this->_formatPrefix($prefix);
  205. if ($this->_useStaticRegistry) {
  206. $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  207. } else {
  208. $registry =& $this->_prefixToPaths;
  209. }
  210. if (!isset($registry[$prefix])) {
  211. require_once 'Zend/Loader/PluginLoader/Exception.php';
  212. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
  213. }
  214. if ($path != null) {
  215. $pos = array_search($path, $registry[$prefix]);
  216. if ($pos === null) {
  217. require_once 'Zend/Loader/PluginLoader/Exception.php';
  218. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
  219. }
  220. unset($registry[$prefix][$pos]);
  221. } else {
  222. unset($registry[$prefix]);
  223. }
  224. return $this;
  225. }
  226. /**
  227. * Normalize plugin name
  228. *
  229. * @param string $name
  230. * @return string
  231. */
  232. protected function _formatName($name)
  233. {
  234. return ucfirst((string) $name);
  235. }
  236. /**
  237. * Whether or not a Plugin by a specific name is loaded
  238. *
  239. * @param string $name
  240. * @return Zend_Loader_PluginLoader
  241. */
  242. public function isLoaded($name)
  243. {
  244. $name = $this->_formatName($name);
  245. if ($this->_useStaticRegistry) {
  246. return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
  247. }
  248. return isset($this->_loadedPlugins[$name]);
  249. }
  250. /**
  251. * Return full class name for a named plugin
  252. *
  253. * @param string $name
  254. * @return string|false False if class not found, class name otherwise
  255. */
  256. public function getClassName($name)
  257. {
  258. $name = $this->_formatName($name);
  259. if ($this->_useStaticRegistry
  260. && isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name])
  261. ) {
  262. return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
  263. } elseif (isset($this->_loadedPlugins[$name])) {
  264. return $this->_loadedPlugins[$name];
  265. }
  266. return false;
  267. }
  268. /**
  269. * Get path to plugin class
  270. *
  271. * @param mixed $name
  272. * @return string|false False if not found
  273. */
  274. public function getClassPath($name)
  275. {
  276. $name = $this->_formatName($name);
  277. if ($this->_useStaticRegistry
  278. && !empty(self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name])
  279. ) {
  280. return self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name];
  281. } elseif (!empty($this->_loadedPluginPaths[$name])) {
  282. return $this->_loadedPluginPaths[$name];
  283. }
  284. if ($this->isLoaded($name)) {
  285. $class = $this->getClassName($name);
  286. $r = new ReflectionClass($class);
  287. $path = $r->getFileName();
  288. if ($this->_useStaticRegistry) {
  289. self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = $path;
  290. } else {
  291. $this->_loadedPluginPaths[$name] = $path;
  292. }
  293. return $path;
  294. }
  295. return false;
  296. }
  297. /**
  298. * Load a plugin via the name provided
  299. *
  300. * @param string $name
  301. * @param bool $throwExceptions Whether or not to throw exceptions if the
  302. * class is not resolved
  303. * @return string|false Class name of loaded class; false if $throwExceptions
  304. * if false and no class found
  305. * @throws Zend_Loader_Exception if class not found
  306. */
  307. public function load($name, $throwExceptions = true)
  308. {
  309. $name = $this->_formatName($name);
  310. if ($this->isLoaded($name)) {
  311. return $this->getClassName($name);
  312. }
  313. if ($this->_useStaticRegistry) {
  314. $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  315. } else {
  316. $registry = $this->_prefixToPaths;
  317. }
  318. $registry = array_reverse($registry, true);
  319. $found = false;
  320. $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
  321. $incFile = self::getIncludeFileCache();
  322. foreach ($registry as $prefix => $paths) {
  323. $className = $prefix . $name;
  324. if (class_exists($className, false)) {
  325. $found = true;
  326. break;
  327. }
  328. $paths = array_reverse($paths, true);
  329. foreach ($paths as $path) {
  330. $loadFile = $path . $classFile;
  331. if (Zend_Loader::isReadable($loadFile)) {
  332. include_once $loadFile;
  333. if (class_exists($className, false)) {
  334. if (null !== $incFile) {
  335. self::_appendIncFile($loadFile);
  336. }
  337. $found = true;
  338. break 2;
  339. }
  340. }
  341. }
  342. }
  343. if (!$found) {
  344. if (!$throwExceptions) {
  345. return false;
  346. }
  347. $message = "Plugin by name '$name' was not found in the registry; used paths:";
  348. foreach ($registry as $prefix => $paths) {
  349. $message .= "\n$prefix: " . implode(PATH_SEPARATOR, $paths);
  350. }
  351. require_once 'Zend/Loader/PluginLoader/Exception.php';
  352. throw new Zend_Loader_PluginLoader_Exception($message);
  353. }
  354. if ($this->_useStaticRegistry) {
  355. self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
  356. self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = (isset($loadFile) ? $loadFile : '');
  357. } else {
  358. $this->_loadedPlugins[$name] = $className;
  359. $this->_loadedPluginPaths[$name] = (isset($loadFile) ? $loadFile : '');
  360. }
  361. return $className;
  362. }
  363. /**
  364. * Set path to class file cache
  365. *
  366. * Specify a path to a file that will add include_once statements for each
  367. * plugin class loaded. This is an opt-in feature for performance purposes.
  368. *
  369. * @param string $file
  370. * @return void
  371. * @throws Zend_Loader_PluginLoader_Exception if file is not writeable or path does not exist
  372. */
  373. public static function setIncludeFileCache($file)
  374. {
  375. if (null === $file) {
  376. self::$_includeFileCache = null;
  377. return;
  378. }
  379. if (!file_exists($file) && !file_exists(dirname($file))) {
  380. require_once 'Zend/Loader/PluginLoader/Exception.php';
  381. throw new Zend_Loader_PluginLoader_Exception('Specified file does not exist and/or directory does not exist (' . $file . ')');
  382. }
  383. if (file_exists($file) && !is_writable($file)) {
  384. require_once 'Zend/Loader/PluginLoader/Exception.php';
  385. throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
  386. }
  387. if (!file_exists($file) && file_exists(dirname($file)) && !is_writable(dirname($file))) {
  388. require_once 'Zend/Loader/PluginLoader/Exception.php';
  389. throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
  390. }
  391. self::$_includeFileCache = $file;
  392. }
  393. /**
  394. * Retrieve class file cache path
  395. *
  396. * @return string|null
  397. */
  398. public static function getIncludeFileCache()
  399. {
  400. return self::$_includeFileCache;
  401. }
  402. /**
  403. * Append an include_once statement to the class file cache
  404. *
  405. * @param string $incFile
  406. * @return void
  407. */
  408. protected static function _appendIncFile($incFile)
  409. {
  410. if (!file_exists(self::$_includeFileCache)) {
  411. $file = '<?php';
  412. } else {
  413. $file = file_get_contents(self::$_includeFileCache);
  414. }
  415. if (!strstr($file, $incFile)) {
  416. $file .= "\ninclude_once '$incFile';";
  417. file_put_contents(self::$_includeFileCache, $file);
  418. }
  419. }
  420. }