PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Loader/Autoloader/Resource.php

https://bitbucket.org/nblaudez/maerdo
PHP | 467 lines | 229 code | 38 blank | 200 comment | 32 complexity | 7fcf885f2bb3c545a7e72b568c21937e 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Resource.php 23568 2010-12-20 08:13:20Z mjh_ca $
  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-2010 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_shift($segments);
  135. if ($namespace != $namespaceTopLevel) {
  136. // wrong prefix? we're done
  137. return false;
  138. }
  139. }
  140. if (count($segments) < 2) {
  141. // assumes all resources have a component and class name, minimum
  142. return false;
  143. }
  144. $final = array_pop($segments);
  145. $component = $namespace;
  146. $lastMatch = false;
  147. do {
  148. $segment = array_shift($segments);
  149. $component .= empty($component) ? $segment : '_' . $segment;
  150. if (isset($this->_components[$component])) {
  151. $lastMatch = $component;
  152. }
  153. } while (count($segments));
  154. if (!$lastMatch) {
  155. return false;
  156. }
  157. $final = substr($class, strlen($lastMatch) + 1);
  158. $path = $this->_components[$lastMatch];
  159. $classPath = $path . '/' . str_replace('_', '/', $final) . '.php';
  160. if (Zend_Loader::isReadable($classPath)) {
  161. return $classPath;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Attempt to autoload a class
  167. *
  168. * @param string $class
  169. * @return mixed False if not matched, otherwise result if include operation
  170. */
  171. public function autoload($class)
  172. {
  173. $classPath = $this->getClassPath($class);
  174. if (false !== $classPath) {
  175. return include $classPath;
  176. }
  177. return false;
  178. }
  179. /**
  180. * Set class state from options
  181. *
  182. * @param array $options
  183. * @return Zend_Loader_Autoloader_Resource
  184. */
  185. public function setOptions(array $options)
  186. {
  187. // Set namespace first, see ZF-10836
  188. if (isset($options['namespace'])) {
  189. $this->setNamespace($options['namespace']);
  190. unset($options['namespace']);
  191. }
  192. $methods = get_class_methods($this);
  193. foreach ($options as $key => $value) {
  194. $method = 'set' . ucfirst($key);
  195. if (in_array($method, $methods)) {
  196. $this->$method($value);
  197. }
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Set namespace that this autoloader handles
  203. *
  204. * @param string $namespace
  205. * @return Zend_Loader_Autoloader_Resource
  206. */
  207. public function setNamespace($namespace)
  208. {
  209. $this->_namespace = rtrim((string) $namespace, '_');
  210. return $this;
  211. }
  212. /**
  213. * Get namespace this autoloader handles
  214. *
  215. * @return string
  216. */
  217. public function getNamespace()
  218. {
  219. return $this->_namespace;
  220. }
  221. /**
  222. * Set base path for this set of resources
  223. *
  224. * @param string $path
  225. * @return Zend_Loader_Autoloader_Resource
  226. */
  227. public function setBasePath($path)
  228. {
  229. $this->_basePath = (string) $path;
  230. return $this;
  231. }
  232. /**
  233. * Get base path to this set of resources
  234. *
  235. * @return string
  236. */
  237. public function getBasePath()
  238. {
  239. return $this->_basePath;
  240. }
  241. /**
  242. * Add resource type
  243. *
  244. * @param string $type identifier for the resource type being loaded
  245. * @param string $path path relative to resource base path containing the resource types
  246. * @param null|string $namespace sub-component namespace to append to base namespace that qualifies this resource type
  247. * @return Zend_Loader_Autoloader_Resource
  248. */
  249. public function addResourceType($type, $path, $namespace = null)
  250. {
  251. $type = strtolower($type);
  252. if (!isset($this->_resourceTypes[$type])) {
  253. if (null === $namespace) {
  254. require_once 'Zend/Loader/Exception.php';
  255. throw new Zend_Loader_Exception('Initial definition of a resource type must include a namespace');
  256. }
  257. $namespaceTopLevel = $this->getNamespace();
  258. $namespace = ucfirst(trim($namespace, '_'));
  259. $this->_resourceTypes[$type] = array(
  260. 'namespace' => empty($namespaceTopLevel) ? $namespace : $namespaceTopLevel . '_' . $namespace,
  261. );
  262. }
  263. if (!is_string($path)) {
  264. require_once 'Zend/Loader/Exception.php';
  265. throw new Zend_Loader_Exception('Invalid path specification provided; must be string');
  266. }
  267. $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/');
  268. $component = $this->_resourceTypes[$type]['namespace'];
  269. $this->_components[$component] = $this->_resourceTypes[$type]['path'];
  270. return $this;
  271. }
  272. /**
  273. * Add multiple resources at once
  274. *
  275. * $types should be an associative array of resource type => specification
  276. * pairs. Each specification should be an associative array containing
  277. * minimally the 'path' key (specifying the path relative to the resource
  278. * base path) and optionally the 'namespace' key (indicating the subcomponent
  279. * namespace to append to the resource namespace).
  280. *
  281. * As an example:
  282. * <code>
  283. * $loader->addResourceTypes(array(
  284. * 'model' => array(
  285. * 'path' => 'models',
  286. * 'namespace' => 'Model',
  287. * ),
  288. * 'form' => array(
  289. * 'path' => 'forms',
  290. * 'namespace' => 'Form',
  291. * ),
  292. * ));
  293. * </code>
  294. *
  295. * @param array $types
  296. * @return Zend_Loader_Autoloader_Resource
  297. */
  298. public function addResourceTypes(array $types)
  299. {
  300. foreach ($types as $type => $spec) {
  301. if (!is_array($spec)) {
  302. require_once 'Zend/Loader/Exception.php';
  303. throw new Zend_Loader_Exception('addResourceTypes() expects an array of arrays');
  304. }
  305. if (!isset($spec['path'])) {
  306. require_once 'Zend/Loader/Exception.php';
  307. throw new Zend_Loader_Exception('addResourceTypes() expects each array to include a paths element');
  308. }
  309. $paths = $spec['path'];
  310. $namespace = null;
  311. if (isset($spec['namespace'])) {
  312. $namespace = $spec['namespace'];
  313. }
  314. $this->addResourceType($type, $paths, $namespace);
  315. }
  316. return $this;
  317. }
  318. /**
  319. * Overwrite existing and set multiple resource types at once
  320. *
  321. * @see Zend_Loader_Autoloader_Resource::addResourceTypes()
  322. * @param array $types
  323. * @return Zend_Loader_Autoloader_Resource
  324. */
  325. public function setResourceTypes(array $types)
  326. {
  327. $this->clearResourceTypes();
  328. return $this->addResourceTypes($types);
  329. }
  330. /**
  331. * Retrieve resource type mappings
  332. *
  333. * @return array
  334. */
  335. public function getResourceTypes()
  336. {
  337. return $this->_resourceTypes;
  338. }
  339. /**
  340. * Is the requested resource type defined?
  341. *
  342. * @param string $type
  343. * @return bool
  344. */
  345. public function hasResourceType($type)
  346. {
  347. return isset($this->_resourceTypes[$type]);
  348. }
  349. /**
  350. * Remove the requested resource type
  351. *
  352. * @param string $type
  353. * @return Zend_Loader_Autoloader_Resource
  354. */
  355. public function removeResourceType($type)
  356. {
  357. if ($this->hasResourceType($type)) {
  358. $namespace = $this->_resourceTypes[$type]['namespace'];
  359. unset($this->_components[$namespace]);
  360. unset($this->_resourceTypes[$type]);
  361. }
  362. return $this;
  363. }
  364. /**
  365. * Clear all resource types
  366. *
  367. * @return Zend_Loader_Autoloader_Resource
  368. */
  369. public function clearResourceTypes()
  370. {
  371. $this->_resourceTypes = array();
  372. $this->_components = array();
  373. return $this;
  374. }
  375. /**
  376. * Set default resource type to use when calling load()
  377. *
  378. * @param string $type
  379. * @return Zend_Loader_Autoloader_Resource
  380. */
  381. public function setDefaultResourceType($type)
  382. {
  383. if ($this->hasResourceType($type)) {
  384. $this->_defaultResourceType = $type;
  385. }
  386. return $this;
  387. }
  388. /**
  389. * Get default resource type to use when calling load()
  390. *
  391. * @return string|null
  392. */
  393. public function getDefaultResourceType()
  394. {
  395. return $this->_defaultResourceType;
  396. }
  397. /**
  398. * Object registry and factory
  399. *
  400. * Loads the requested resource of type $type (or uses the default resource
  401. * type if none provided). If the resource has been loaded previously,
  402. * returns the previous instance; otherwise, instantiates it.
  403. *
  404. * @param string $resource
  405. * @param string $type
  406. * @return object
  407. * @throws Zend_Loader_Exception if resource type not specified or invalid
  408. */
  409. public function load($resource, $type = null)
  410. {
  411. if (null === $type) {
  412. $type = $this->getDefaultResourceType();
  413. if (empty($type)) {
  414. require_once 'Zend/Loader/Exception.php';
  415. throw new Zend_Loader_Exception('No resource type specified');
  416. }
  417. }
  418. if (!$this->hasResourceType($type)) {
  419. require_once 'Zend/Loader/Exception.php';
  420. throw new Zend_Loader_Exception('Invalid resource type specified');
  421. }
  422. $namespace = $this->_resourceTypes[$type]['namespace'];
  423. $class = $namespace . '_' . ucfirst($resource);
  424. if (!isset($this->_resources[$class])) {
  425. $this->_resources[$class] = new $class;
  426. }
  427. return $this->_resources[$class];
  428. }
  429. }