PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Loader/Autoloader/Resource.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 472 lines | 234 code | 38 blank | 200 comment | 33 complexity | 7244b30d51bd2150f2709f7ec607742a 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 Autoloader
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Resource.php 23860 2011-04-14 17:03:28Z matthew $
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Loader_Autoloader_Interface */
  23. require_once 'Zend/Loader/Autoloader/Interface.php';
  24. /**
  25. * Resource loader
  26. *
  27. * @uses Zend_Loader_Autoloader_Interface
  28. * @package Zend_Loader
  29. * @subpackage Autoloader
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Loader_Autoloader_Resource implements Zend_Loader_Autoloader_Interface
  34. {
  35. /**
  36. * @var string Base path to resource classes
  37. */
  38. protected $_basePath;
  39. /**
  40. * @var array Components handled within this resource
  41. */
  42. protected $_components = array();
  43. /**
  44. * @var string Default resource/component to use when using object registry
  45. */
  46. protected $_defaultResourceType;
  47. /**
  48. * @var string Namespace of classes within this resource
  49. */
  50. protected $_namespace;
  51. /**
  52. * @var array Available resource types handled by this resource autoloader
  53. */
  54. protected $_resourceTypes = array();
  55. /**
  56. * Constructor
  57. *
  58. * @param array|Zend_Config $options Configuration options for resource autoloader
  59. * @return void
  60. */
  61. public function __construct($options)
  62. {
  63. if ($options instanceof Zend_Config) {
  64. $options = $options->toArray();
  65. }
  66. if (!is_array($options)) {
  67. require_once 'Zend/Loader/Exception.php';
  68. throw new Zend_Loader_Exception('Options must be passed to resource loader constructor');
  69. }
  70. $this->setOptions($options);
  71. $namespace = $this->getNamespace();
  72. if ((null === $namespace)
  73. || (null === $this->getBasePath())
  74. ) {
  75. require_once 'Zend/Loader/Exception.php';
  76. throw new Zend_Loader_Exception('Resource loader requires both a namespace and a base path for initialization');
  77. }
  78. if (!empty($namespace)) {
  79. $namespace .= '_';
  80. }
  81. require_once 'Zend/Loader/Autoloader.php';
  82. Zend_Loader_Autoloader::getInstance()->unshiftAutoloader($this, $namespace);
  83. }
  84. /**
  85. * Overloading: methods
  86. *
  87. * Allow retrieving concrete resource object instances using 'get<Resourcename>()'
  88. * syntax. Example:
  89. * <code>
  90. * $loader = new Zend_Loader_Autoloader_Resource(array(
  91. * 'namespace' => 'Stuff_',
  92. * 'basePath' => '/path/to/some/stuff',
  93. * ))
  94. * $loader->addResourceType('Model', 'models', 'Model');
  95. *
  96. * $foo = $loader->getModel('Foo'); // get instance of Stuff_Model_Foo class
  97. * </code>
  98. *
  99. * @param string $method
  100. * @param array $args
  101. * @return mixed
  102. * @throws Zend_Loader_Exception if method not beginning with 'get' or not matching a valid resource type is called
  103. */
  104. public function __call($method, $args)
  105. {
  106. if ('get' == substr($method, 0, 3)) {
  107. $type = strtolower(substr($method, 3));
  108. if (!$this->hasResourceType($type)) {
  109. require_once 'Zend/Loader/Exception.php';
  110. throw new Zend_Loader_Exception("Invalid resource type $type; cannot load resource");
  111. }
  112. if (empty($args)) {
  113. require_once 'Zend/Loader/Exception.php';
  114. throw new Zend_Loader_Exception("Cannot load resources; no resource specified");
  115. }
  116. $resource = array_shift($args);
  117. return $this->load($resource, $type);
  118. }
  119. require_once 'Zend/Loader/Exception.php';
  120. throw new Zend_Loader_Exception("Method '$method' is not supported");
  121. }
  122. /**
  123. * Helper method to calculate the correct class path
  124. *
  125. * @param string $class
  126. * @return False if not matched other wise the correct path
  127. */
  128. public function getClassPath($class)
  129. {
  130. $segments = explode('_', $class);
  131. $namespaceTopLevel = $this->getNamespace();
  132. $namespace = '';
  133. if (!empty($namespaceTopLevel)) {
  134. $namespace = array();
  135. $topLevelSegments = count(explode('_', $namespaceTopLevel));
  136. for ($i = 0; $i < $topLevelSegments; $i++) {
  137. $namespace[] = array_shift($segments);
  138. }
  139. $namespace = implode('_', $namespace);
  140. if ($namespace != $namespaceTopLevel) {
  141. // wrong prefix? we're done
  142. return false;
  143. }
  144. }
  145. if (count($segments) < 2) {
  146. // assumes all resources have a component and class name, minimum
  147. return false;
  148. }
  149. $final = array_pop($segments);
  150. $component = $namespace;
  151. $lastMatch = false;
  152. do {
  153. $segment = array_shift($segments);
  154. $component .= empty($component) ? $segment : '_' . $segment;
  155. if (isset($this->_components[$component])) {
  156. $lastMatch = $component;
  157. }
  158. } while (count($segments));
  159. if (!$lastMatch) {
  160. return false;
  161. }
  162. $final = substr($class, strlen($lastMatch) + 1);
  163. $path = $this->_components[$lastMatch];
  164. $classPath = $path . '/' . str_replace('_', '/', $final) . '.php';
  165. if (Zend_Loader::isReadable($classPath)) {
  166. return $classPath;
  167. }
  168. return false;
  169. }
  170. /**
  171. * Attempt to autoload a class
  172. *
  173. * @param string $class
  174. * @return mixed False if not matched, otherwise result if include operation
  175. */
  176. public function autoload($class)
  177. {
  178. $classPath = $this->getClassPath($class);
  179. if (false !== $classPath) {
  180. return include $classPath;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Set class state from options
  186. *
  187. * @param array $options
  188. * @return Zend_Loader_Autoloader_Resource
  189. */
  190. public function setOptions(array $options)
  191. {
  192. // Set namespace first, see ZF-10836
  193. if (isset($options['namespace'])) {
  194. $this->setNamespace($options['namespace']);
  195. unset($options['namespace']);
  196. }
  197. $methods = get_class_methods($this);
  198. foreach ($options as $key => $value) {
  199. $method = 'set' . ucfirst($key);
  200. if (in_array($method, $methods)) {
  201. $this->$method($value);
  202. }
  203. }
  204. return $this;
  205. }
  206. /**
  207. * Set namespace that this autoloader handles
  208. *
  209. * @param string $namespace
  210. * @return Zend_Loader_Autoloader_Resource
  211. */
  212. public function setNamespace($namespace)
  213. {
  214. $this->_namespace = rtrim((string) $namespace, '_');
  215. return $this;
  216. }
  217. /**
  218. * Get namespace this autoloader handles
  219. *
  220. * @return string
  221. */
  222. public function getNamespace()
  223. {
  224. return $this->_namespace;
  225. }
  226. /**
  227. * Set base path for this set of resources
  228. *
  229. * @param string $path
  230. * @return Zend_Loader_Autoloader_Resource
  231. */
  232. public function setBasePath($path)
  233. {
  234. $this->_basePath = (string) $path;
  235. return $this;
  236. }
  237. /**
  238. * Get base path to this set of resources
  239. *
  240. * @return string
  241. */
  242. public function getBasePath()
  243. {
  244. return $this->_basePath;
  245. }
  246. /**
  247. * Add resource type
  248. *
  249. * @param string $type identifier for the resource type being loaded
  250. * @param string $path path relative to resource base path containing the resource types
  251. * @param null|string $namespace sub-component namespace to append to base namespace that qualifies this resource type
  252. * @return Zend_Loader_Autoloader_Resource
  253. */
  254. public function addResourceType($type, $path, $namespace = null)
  255. {
  256. $type = strtolower($type);
  257. if (!isset($this->_resourceTypes[$type])) {
  258. if (null === $namespace) {
  259. require_once 'Zend/Loader/Exception.php';
  260. throw new Zend_Loader_Exception('Initial definition of a resource type must include a namespace');
  261. }
  262. $namespaceTopLevel = $this->getNamespace();
  263. $namespace = ucfirst(trim($namespace, '_'));
  264. $this->_resourceTypes[$type] = array(
  265. 'namespace' => empty($namespaceTopLevel) ? $namespace : $namespaceTopLevel . '_' . $namespace,
  266. );
  267. }
  268. if (!is_string($path)) {
  269. require_once 'Zend/Loader/Exception.php';
  270. throw new Zend_Loader_Exception('Invalid path specification provided; must be string');
  271. }
  272. $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/');
  273. $component = $this->_resourceTypes[$type]['namespace'];
  274. $this->_components[$component] = $this->_resourceTypes[$type]['path'];
  275. return $this;
  276. }
  277. /**
  278. * Add multiple resources at once
  279. *
  280. * $types should be an associative array of resource type => specification
  281. * pairs. Each specification should be an associative array containing
  282. * minimally the 'path' key (specifying the path relative to the resource
  283. * base path) and optionally the 'namespace' key (indicating the subcomponent
  284. * namespace to append to the resource namespace).
  285. *
  286. * As an example:
  287. * <code>
  288. * $loader->addResourceTypes(array(
  289. * 'model' => array(
  290. * 'path' => 'models',
  291. * 'namespace' => 'Model',
  292. * ),
  293. * 'form' => array(
  294. * 'path' => 'forms',
  295. * 'namespace' => 'Form',
  296. * ),
  297. * ));
  298. * </code>
  299. *
  300. * @param array $types
  301. * @return Zend_Loader_Autoloader_Resource
  302. */
  303. public function addResourceTypes(array $types)
  304. {
  305. foreach ($types as $type => $spec) {
  306. if (!is_array($spec)) {
  307. require_once 'Zend/Loader/Exception.php';
  308. throw new Zend_Loader_Exception('addResourceTypes() expects an array of arrays');
  309. }
  310. if (!isset($spec['path'])) {
  311. require_once 'Zend/Loader/Exception.php';
  312. throw new Zend_Loader_Exception('addResourceTypes() expects each array to include a paths element');
  313. }
  314. $paths = $spec['path'];
  315. $namespace = null;
  316. if (isset($spec['namespace'])) {
  317. $namespace = $spec['namespace'];
  318. }
  319. $this->addResourceType($type, $paths, $namespace);
  320. }
  321. return $this;
  322. }
  323. /**
  324. * Overwrite existing and set multiple resource types at once
  325. *
  326. * @see Zend_Loader_Autoloader_Resource::addResourceTypes()
  327. * @param array $types
  328. * @return Zend_Loader_Autoloader_Resource
  329. */
  330. public function setResourceTypes(array $types)
  331. {
  332. $this->clearResourceTypes();
  333. return $this->addResourceTypes($types);
  334. }
  335. /**
  336. * Retrieve resource type mappings
  337. *
  338. * @return array
  339. */
  340. public function getResourceTypes()
  341. {
  342. return $this->_resourceTypes;
  343. }
  344. /**
  345. * Is the requested resource type defined?
  346. *
  347. * @param string $type
  348. * @return bool
  349. */
  350. public function hasResourceType($type)
  351. {
  352. return isset($this->_resourceTypes[$type]);
  353. }
  354. /**
  355. * Remove the requested resource type
  356. *
  357. * @param string $type
  358. * @return Zend_Loader_Autoloader_Resource
  359. */
  360. public function removeResourceType($type)
  361. {
  362. if ($this->hasResourceType($type)) {
  363. $namespace = $this->_resourceTypes[$type]['namespace'];
  364. unset($this->_components[$namespace]);
  365. unset($this->_resourceTypes[$type]);
  366. }
  367. return $this;
  368. }
  369. /**
  370. * Clear all resource types
  371. *
  372. * @return Zend_Loader_Autoloader_Resource
  373. */
  374. public function clearResourceTypes()
  375. {
  376. $this->_resourceTypes = array();
  377. $this->_components = array();
  378. return $this;
  379. }
  380. /**
  381. * Set default resource type to use when calling load()
  382. *
  383. * @param string $type
  384. * @return Zend_Loader_Autoloader_Resource
  385. */
  386. public function setDefaultResourceType($type)
  387. {
  388. if ($this->hasResourceType($type)) {
  389. $this->_defaultResourceType = $type;
  390. }
  391. return $this;
  392. }
  393. /**
  394. * Get default resource type to use when calling load()
  395. *
  396. * @return string|null
  397. */
  398. public function getDefaultResourceType()
  399. {
  400. return $this->_defaultResourceType;
  401. }
  402. /**
  403. * Object registry and factory
  404. *
  405. * Loads the requested resource of type $type (or uses the default resource
  406. * type if none provided). If the resource has been loaded previously,
  407. * returns the previous instance; otherwise, instantiates it.
  408. *
  409. * @param string $resource
  410. * @param string $type
  411. * @return object
  412. * @throws Zend_Loader_Exception if resource type not specified or invalid
  413. */
  414. public function load($resource, $type = null)
  415. {
  416. if (null === $type) {
  417. $type = $this->getDefaultResourceType();
  418. if (empty($type)) {
  419. require_once 'Zend/Loader/Exception.php';
  420. throw new Zend_Loader_Exception('No resource type specified');
  421. }
  422. }
  423. if (!$this->hasResourceType($type)) {
  424. require_once 'Zend/Loader/Exception.php';
  425. throw new Zend_Loader_Exception('Invalid resource type specified');
  426. }
  427. $namespace = $this->_resourceTypes[$type]['namespace'];
  428. $class = $namespace . '_' . ucfirst($resource);
  429. if (!isset($this->_resources[$class])) {
  430. $this->_resources[$class] = new $class;
  431. }
  432. return $this->_resources[$class];
  433. }
  434. }