PluginLoader.php 14 KB

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