PluginLoader.php 15 KB

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