PluginLoader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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-2014 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-2014 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. $prefix = rtrim($prefix, $nsSeparator) . $nsSeparator;
  118. //if $nsSeprator == "\" and the prefix ends in "_\" remove trailing \
  119. //https://github.com/zendframework/zf1/issues/152
  120. if(($nsSeparator == "\\") && (substr($prefix,-2) == "_\\")) {
  121. $prefix = substr($prefix, 0, -1);
  122. }
  123. return $prefix;
  124. }
  125. /**
  126. * Add prefixed paths to the registry of paths
  127. *
  128. * @param string $prefix
  129. * @param string $path
  130. * @return Zend_Loader_PluginLoader
  131. */
  132. public function addPrefixPath($prefix, $path)
  133. {
  134. if (!is_string($prefix) || !is_string($path)) {
  135. require_once 'Zend/Loader/PluginLoader/Exception.php';
  136. throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
  137. }
  138. $prefix = $this->_formatPrefix($prefix);
  139. $path = rtrim($path, '/\\') . '/';
  140. if ($this->_useStaticRegistry) {
  141. self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
  142. } else {
  143. if (!isset($this->_prefixToPaths[$prefix])) {
  144. $this->_prefixToPaths[$prefix] = array();
  145. }
  146. if (!in_array($path, $this->_prefixToPaths[$prefix])) {
  147. $this->_prefixToPaths[$prefix][] = $path;
  148. }
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get path stack
  154. *
  155. * @param string $prefix
  156. * @return false|array False if prefix does not exist, array otherwise
  157. */
  158. public function getPaths($prefix = null)
  159. {
  160. if ((null !== $prefix) && is_string($prefix)) {
  161. $prefix = $this->_formatPrefix($prefix);
  162. if ($this->_useStaticRegistry) {
  163. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  164. return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
  165. }
  166. return false;
  167. }
  168. if (isset($this->_prefixToPaths[$prefix])) {
  169. return $this->_prefixToPaths[$prefix];
  170. }
  171. return false;
  172. }
  173. if ($this->_useStaticRegistry) {
  174. return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  175. }
  176. return $this->_prefixToPaths;
  177. }
  178. /**
  179. * Clear path stack
  180. *
  181. * @param string $prefix
  182. * @return bool False only if $prefix does not exist
  183. */
  184. public function clearPaths($prefix = null)
  185. {
  186. if ((null !== $prefix) && is_string($prefix)) {
  187. $prefix = $this->_formatPrefix($prefix);
  188. if ($this->_useStaticRegistry) {
  189. if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
  190. unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
  191. return true;
  192. }
  193. return false;
  194. }
  195. if (isset($this->_prefixToPaths[$prefix])) {
  196. unset($this->_prefixToPaths[$prefix]);
  197. return true;
  198. }
  199. return false;
  200. }
  201. if ($this->_useStaticRegistry) {
  202. self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
  203. } else {
  204. $this->_prefixToPaths = array();
  205. }
  206. return true;
  207. }
  208. /**
  209. * Remove a prefix (or prefixed-path) from the registry
  210. *
  211. * @param string $prefix
  212. * @param string $path OPTIONAL
  213. * @return Zend_Loader_PluginLoader
  214. */
  215. public function removePrefixPath($prefix, $path = null)
  216. {
  217. $prefix = $this->_formatPrefix($prefix);
  218. if ($this->_useStaticRegistry) {
  219. $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  220. } else {
  221. $registry =& $this->_prefixToPaths;
  222. }
  223. if (!isset($registry[$prefix])) {
  224. require_once 'Zend/Loader/PluginLoader/Exception.php';
  225. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
  226. }
  227. if ($path != null) {
  228. $pos = array_search($path, $registry[$prefix]);
  229. if (false === $pos) {
  230. require_once 'Zend/Loader/PluginLoader/Exception.php';
  231. throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
  232. }
  233. unset($registry[$prefix][$pos]);
  234. } else {
  235. unset($registry[$prefix]);
  236. }
  237. return $this;
  238. }
  239. /**
  240. * Normalize plugin name
  241. *
  242. * @param string $name
  243. * @return string
  244. */
  245. protected function _formatName($name)
  246. {
  247. return ucfirst((string) $name);
  248. }
  249. /**
  250. * Whether or not a Plugin by a specific name is loaded
  251. *
  252. * @param string $name
  253. * @return Zend_Loader_PluginLoader
  254. */
  255. public function isLoaded($name)
  256. {
  257. $name = $this->_formatName($name);
  258. if ($this->_useStaticRegistry) {
  259. return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
  260. }
  261. return isset($this->_loadedPlugins[$name]);
  262. }
  263. /**
  264. * Return full class name for a named plugin
  265. *
  266. * @param string $name
  267. * @return string|false False if class not found, class name otherwise
  268. */
  269. public function getClassName($name)
  270. {
  271. $name = $this->_formatName($name);
  272. if ($this->_useStaticRegistry
  273. && isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name])
  274. ) {
  275. return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
  276. } elseif (isset($this->_loadedPlugins[$name])) {
  277. return $this->_loadedPlugins[$name];
  278. }
  279. return false;
  280. }
  281. /**
  282. * Get path to plugin class
  283. *
  284. * @param mixed $name
  285. * @return string|false False if not found
  286. */
  287. public function getClassPath($name)
  288. {
  289. $name = $this->_formatName($name);
  290. if ($this->_useStaticRegistry
  291. && !empty(self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name])
  292. ) {
  293. return self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name];
  294. } elseif (!empty($this->_loadedPluginPaths[$name])) {
  295. return $this->_loadedPluginPaths[$name];
  296. }
  297. if ($this->isLoaded($name)) {
  298. $class = $this->getClassName($name);
  299. $r = new ReflectionClass($class);
  300. $path = $r->getFileName();
  301. if ($this->_useStaticRegistry) {
  302. self::$_staticLoadedPluginPaths[$this->_useStaticRegistry][$name] = $path;
  303. } else {
  304. $this->_loadedPluginPaths[$name] = $path;
  305. }
  306. return $path;
  307. }
  308. return false;
  309. }
  310. /**
  311. * Load a plugin via the name provided
  312. *
  313. * @param string $name
  314. * @param bool $throwExceptions Whether or not to throw exceptions if the
  315. * class is not resolved
  316. * @return string|false Class name of loaded class; false if $throwExceptions
  317. * if false and no class found
  318. * @throws Zend_Loader_Exception if class not found
  319. */
  320. public function load($name, $throwExceptions = true)
  321. {
  322. $name = $this->_formatName($name);
  323. if ($this->isLoaded($name)) {
  324. return $this->getClassName($name);
  325. }
  326. if ($this->_useStaticRegistry) {
  327. $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
  328. } else {
  329. $registry = $this->_prefixToPaths;
  330. }
  331. $registry = array_reverse($registry, true);
  332. $found = false;
  333. if (false !== strpos($name, '\\')) {
  334. $classFile = str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php';
  335. } else {
  336. $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
  337. }
  338. $incFile = self::getIncludeFileCache();
  339. foreach ($registry as $prefix => $paths) {
  340. $className = $prefix . $name;
  341. if (class_exists($className, false)) {
  342. $found = true;
  343. break;
  344. }
  345. $paths = array_reverse($paths, true);
  346. foreach ($paths as $path) {
  347. $loadFile = $path . $classFile;
  348. if (Zend_Loader::isReadable($loadFile)) {
  349. include_once $loadFile;
  350. if (class_exists($className, false)) {
  351. if (null !== $incFile) {
  352. self::_appendIncFile($loadFile);
  353. }
  354. $found = true;
  355. break 2;
  356. }
  357. }
  358. }
  359. }
  360. if (!$found) {
  361. if (!$throwExceptions) {
  362. return false;
  363. }
  364. $message = "Plugin by name '$name' was not found in the registry; used paths:";
  365. foreach ($registry as $prefix => $paths) {
  366. $message .= "\n$prefix: " . implode(PATH_SEPARATOR, $paths);
  367. }
  368. require_once 'Zend/Loader/PluginLoader/Exception.php';
  369. throw new Zend_Loader_PluginLoader_Exception($message);
  370. }
  371. if ($this->_useStaticRegistry) {
  372. self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
  373. } else {
  374. $this->_loadedPlugins[$name] = $className;
  375. }
  376. return $className;
  377. }
  378. /**
  379. * Set path to class file cache
  380. *
  381. * Specify a path to a file that will add include_once statements for each
  382. * plugin class loaded. This is an opt-in feature for performance purposes.
  383. *
  384. * @param string $file
  385. * @return void
  386. * @throws Zend_Loader_PluginLoader_Exception if file is not writeable or path does not exist
  387. */
  388. public static function setIncludeFileCache($file)
  389. {
  390. if (null === $file) {
  391. self::$_includeFileCache = null;
  392. return;
  393. }
  394. if (!file_exists($file) && !file_exists(dirname($file))) {
  395. require_once 'Zend/Loader/PluginLoader/Exception.php';
  396. throw new Zend_Loader_PluginLoader_Exception('Specified file does not exist and/or directory does not exist (' . $file . ')');
  397. }
  398. if (file_exists($file) && !is_writable($file)) {
  399. require_once 'Zend/Loader/PluginLoader/Exception.php';
  400. throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
  401. }
  402. if (!file_exists($file) && file_exists(dirname($file)) && !is_writable(dirname($file))) {
  403. require_once 'Zend/Loader/PluginLoader/Exception.php';
  404. throw new Zend_Loader_PluginLoader_Exception('Specified file is not writeable (' . $file . ')');
  405. }
  406. self::$_includeFileCache = $file;
  407. }
  408. /**
  409. * Retrieve class file cache path
  410. *
  411. * @return string|null
  412. */
  413. public static function getIncludeFileCache()
  414. {
  415. return self::$_includeFileCache;
  416. }
  417. /**
  418. * Append an include_once statement to the class file cache
  419. *
  420. * @param string $incFile
  421. * @return void
  422. */
  423. protected static function _appendIncFile($incFile)
  424. {
  425. static $h = null;
  426. if (null === $h) {
  427. $h = fopen(self::$_includeFileCache, 'ab');
  428. if (!flock($h, LOCK_EX | LOCK_NB, $wb) || $wb) {
  429. $h = false;
  430. }
  431. }
  432. if (false !== $h) {
  433. $line = "<?php include_once '$incFile'?>\n";
  434. fwrite($h, $line, strlen($line));
  435. }
  436. }
  437. }