Resource.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  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-2009 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. Zend_Loader_Autoloader::getInstance()->unshiftAutoloader($this, $namespace);
  82. }
  83. /**
  84. * Overloading: methods
  85. *
  86. * Allow retrieving concrete resource object instances using 'get<Resourcename>()'
  87. * syntax. Example:
  88. * <code>
  89. * $loader = new Zend_Loader_Autoloader_Resource(array(
  90. * 'namespace' => 'Stuff_',
  91. * 'basePath' => '/path/to/some/stuff',
  92. * ))
  93. * $loader->addResourceType('Model', 'models', 'Model');
  94. *
  95. * $foo = $loader->getModel('Foo'); // get instance of Stuff_Model_Foo class
  96. * </code>
  97. *
  98. * @param string $method
  99. * @param array $args
  100. * @return mixed
  101. * @throws Zend_Loader_Exception if method not beginning with 'get' or not matching a valid resource type is called
  102. */
  103. public function __call($method, $args)
  104. {
  105. if ('get' == substr($method, 0, 3)) {
  106. $type = strtolower(substr($method, 3));
  107. if (!$this->hasResourceType($type)) {
  108. require_once 'Zend/Loader/Exception.php';
  109. throw new Zend_Loader_Exception("Invalid resource type $type; cannot load resource");
  110. }
  111. if (empty($args)) {
  112. require_once 'Zend/Loader/Exception.php';
  113. throw new Zend_Loader_Exception("Cannot load resources; no resource specified");
  114. }
  115. $resource = array_shift($args);
  116. return $this->load($resource, $type);
  117. }
  118. require_once 'Zend/Loader/Exception.php';
  119. throw new Zend_Loader_Exception("Method '$method' is not supported");
  120. }
  121. /**
  122. * Helper method to calculate the correct class path
  123. *
  124. * @param string $class
  125. * @return False if not matched other wise the correct path
  126. */
  127. public function getClassPath($class)
  128. {
  129. $segments = explode('_', $class);
  130. $namespaceTopLevel = $this->getNamespace();
  131. $namespace = '';
  132. if (!empty($namespaceTopLevel)) {
  133. $namespace = array_shift($segments);
  134. if ($namespace != $namespaceTopLevel) {
  135. // wrong prefix? we're done
  136. return false;
  137. }
  138. }
  139. if (count($segments) < 2) {
  140. // assumes all resources have a component and class name, minimum
  141. return false;
  142. }
  143. $final = array_pop($segments);
  144. $component = $namespace;
  145. $lastMatch = false;
  146. do {
  147. $segment = array_shift($segments);
  148. $component .= empty($component) ? $segment : '_' . $segment;
  149. if (isset($this->_components[$component])) {
  150. $lastMatch = $component;
  151. }
  152. } while (count($segments));
  153. if (!$lastMatch) {
  154. return false;
  155. }
  156. $final = substr($class, strlen($lastMatch) + 1);
  157. $path = $this->_components[$lastMatch];
  158. return $path . '/' . str_replace('_', '/', $final) . '.php';
  159. }
  160. /**
  161. * Attempt to autoload a class
  162. *
  163. * @param string $class
  164. * @return mixed False if not matched, otherwise result if include operation
  165. */
  166. public function autoload($class)
  167. {
  168. return include $this->getClassPath($class);
  169. }
  170. /**
  171. * Set class state from options
  172. *
  173. * @param array $options
  174. * @return Zend_Loader_Autoloader_Resource
  175. */
  176. public function setOptions(array $options)
  177. {
  178. $methods = get_class_methods($this);
  179. foreach ($options as $key => $value) {
  180. $method = 'set' . ucfirst($key);
  181. if (in_array($method, $methods)) {
  182. $this->$method($value);
  183. }
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Set namespace that this autoloader handles
  189. *
  190. * @param string $namespace
  191. * @return Zend_Loader_Autoloader_Resource
  192. */
  193. public function setNamespace($namespace)
  194. {
  195. $this->_namespace = rtrim((string) $namespace, '_');
  196. return $this;
  197. }
  198. /**
  199. * Get namespace this autoloader handles
  200. *
  201. * @return string
  202. */
  203. public function getNamespace()
  204. {
  205. return $this->_namespace;
  206. }
  207. /**
  208. * Set base path for this set of resources
  209. *
  210. * @param string $path
  211. * @return Zend_Loader_Autoloader_Resource
  212. */
  213. public function setBasePath($path)
  214. {
  215. $this->_basePath = (string) $path;
  216. return $this;
  217. }
  218. /**
  219. * Get base path to this set of resources
  220. *
  221. * @return string
  222. */
  223. public function getBasePath()
  224. {
  225. return $this->_basePath;
  226. }
  227. /**
  228. * Add resource type
  229. *
  230. * @param string $type identifier for the resource type being loaded
  231. * @param string $path path relative to resource base path containing the resource types
  232. * @param null|string $namespace sub-component namespace to append to base namespace that qualifies this resource type
  233. * @return Zend_Loader_Autoloader_Resource
  234. */
  235. public function addResourceType($type, $path, $namespace = null)
  236. {
  237. $type = strtolower($type);
  238. if (!isset($this->_resourceTypes[$type])) {
  239. if (null === $namespace) {
  240. require_once 'Zend/Loader/Exception.php';
  241. throw new Zend_Loader_Exception('Initial definition of a resource type must include a namespace');
  242. }
  243. $namespaceTopLevel = $this->getNamespace();
  244. $namespace = ucfirst(trim($namespace, '_'));
  245. $this->_resourceTypes[$type] = array(
  246. 'namespace' => empty($namespaceTopLevel) ? $namespace : $namespaceTopLevel . '_' . $namespace,
  247. );
  248. }
  249. if (!is_string($path)) {
  250. require_once 'Zend/Loader/Exception.php';
  251. throw new Zend_Loader_Exception('Invalid path specification provided; must be string');
  252. }
  253. $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/');
  254. $component = $this->_resourceTypes[$type]['namespace'];
  255. $this->_components[$component] = $this->_resourceTypes[$type]['path'];
  256. return $this;
  257. }
  258. /**
  259. * Add multiple resources at once
  260. *
  261. * $types should be an associative array of resource type => specification
  262. * pairs. Each specification should be an associative array containing
  263. * minimally the 'path' key (specifying the path relative to the resource
  264. * base path) and optionally the 'namespace' key (indicating the subcomponent
  265. * namespace to append to the resource namespace).
  266. *
  267. * As an example:
  268. * <code>
  269. * $loader->addResourceTypes(array(
  270. * 'model' => array(
  271. * 'path' => 'models',
  272. * 'namespace' => 'Model',
  273. * ),
  274. * 'form' => array(
  275. * 'path' => 'forms',
  276. * 'namespace' => 'Form',
  277. * ),
  278. * ));
  279. * </code>
  280. *
  281. * @param array $types
  282. * @return Zend_Loader_Autoloader_Resource
  283. */
  284. public function addResourceTypes(array $types)
  285. {
  286. foreach ($types as $type => $spec) {
  287. if (!is_array($spec)) {
  288. require_once 'Zend/Loader/Exception.php';
  289. throw new Zend_Loader_Exception('addResourceTypes() expects an array of arrays');
  290. }
  291. if (!isset($spec['path'])) {
  292. require_once 'Zend/Loader/Exception.php';
  293. throw new Zend_Loader_Exception('addResourceTypes() expects each array to include a paths element');
  294. }
  295. $paths = $spec['path'];
  296. $namespace = null;
  297. if (isset($spec['namespace'])) {
  298. $namespace = $spec['namespace'];
  299. }
  300. $this->addResourceType($type, $paths, $namespace);
  301. }
  302. return $this;
  303. }
  304. /**
  305. * Overwrite existing and set multiple resource types at once
  306. *
  307. * @see Zend_Loader_Autoloader_Resource::addResourceTypes()
  308. * @param array $types
  309. * @return Zend_Loader_Autoloader_Resource
  310. */
  311. public function setResourceTypes(array $types)
  312. {
  313. $this->clearResourceTypes();
  314. return $this->addResourceTypes($types);
  315. }
  316. /**
  317. * Retrieve resource type mappings
  318. *
  319. * @return array
  320. */
  321. public function getResourceTypes()
  322. {
  323. return $this->_resourceTypes;
  324. }
  325. /**
  326. * Is the requested resource type defined?
  327. *
  328. * @param string $type
  329. * @return bool
  330. */
  331. public function hasResourceType($type)
  332. {
  333. return isset($this->_resourceTypes[$type]);
  334. }
  335. /**
  336. * Remove the requested resource type
  337. *
  338. * @param string $type
  339. * @return Zend_Loader_Autoloader_Resource
  340. */
  341. public function removeResourceType($type)
  342. {
  343. if ($this->hasResourceType($type)) {
  344. $namespace = $this->_resourceTypes[$type]['namespace'];
  345. unset($this->_components[$namespace]);
  346. unset($this->_resourceTypes[$type]);
  347. }
  348. return $this;
  349. }
  350. /**
  351. * Clear all resource types
  352. *
  353. * @return Zend_Loader_Autoloader_Resource
  354. */
  355. public function clearResourceTypes()
  356. {
  357. $this->_resourceTypes = array();
  358. $this->_components = array();
  359. return $this;
  360. }
  361. /**
  362. * Set default resource type to use when calling load()
  363. *
  364. * @param string $type
  365. * @return Zend_Loader_Autoloader_Resource
  366. */
  367. public function setDefaultResourceType($type)
  368. {
  369. if ($this->hasResourceType($type)) {
  370. $this->_defaultResourceType = $type;
  371. }
  372. return $this;
  373. }
  374. /**
  375. * Get default resource type to use when calling load()
  376. *
  377. * @return string|null
  378. */
  379. public function getDefaultResourceType()
  380. {
  381. return $this->_defaultResourceType;
  382. }
  383. /**
  384. * Object registry and factory
  385. *
  386. * Loads the requested resource of type $type (or uses the default resource
  387. * type if none provided). If the resource has been loaded previously,
  388. * returns the previous instance; otherwise, instantiates it.
  389. *
  390. * @param string $resource
  391. * @param string $type
  392. * @return object
  393. * @throws Zend_Loader_Exception if resource type not specified or invalid
  394. */
  395. public function load($resource, $type = null)
  396. {
  397. if (null === $type) {
  398. $type = $this->getDefaultResourceType();
  399. if (empty($type)) {
  400. require_once 'Zend/Loader/Exception.php';
  401. throw new Zend_Loader_Exception('No resource type specified');
  402. }
  403. }
  404. if (!$this->hasResourceType($type)) {
  405. require_once 'Zend/Loader/Exception.php';
  406. throw new Zend_Loader_Exception('Invalid resource type specified');
  407. }
  408. $namespace = $this->_resourceTypes[$type]['namespace'];
  409. $class = $namespace . '_' . ucfirst($resource);
  410. if (!isset($this->_resources[$class])) {
  411. $this->_resources[$class] = new $class;
  412. }
  413. return $this->_resources[$class];
  414. }
  415. }