Resource.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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$
  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. $methods = get_class_methods($this);
  188. foreach ($options as $key => $value) {
  189. $method = 'set' . ucfirst($key);
  190. if (in_array($method, $methods)) {
  191. $this->$method($value);
  192. }
  193. }
  194. return $this;
  195. }
  196. /**
  197. * Set namespace that this autoloader handles
  198. *
  199. * @param string $namespace
  200. * @return Zend_Loader_Autoloader_Resource
  201. */
  202. public function setNamespace($namespace)
  203. {
  204. $this->_namespace = rtrim((string) $namespace, '_');
  205. return $this;
  206. }
  207. /**
  208. * Get namespace this autoloader handles
  209. *
  210. * @return string
  211. */
  212. public function getNamespace()
  213. {
  214. return $this->_namespace;
  215. }
  216. /**
  217. * Set base path for this set of resources
  218. *
  219. * @param string $path
  220. * @return Zend_Loader_Autoloader_Resource
  221. */
  222. public function setBasePath($path)
  223. {
  224. $this->_basePath = (string) $path;
  225. return $this;
  226. }
  227. /**
  228. * Get base path to this set of resources
  229. *
  230. * @return string
  231. */
  232. public function getBasePath()
  233. {
  234. return $this->_basePath;
  235. }
  236. /**
  237. * Add resource type
  238. *
  239. * @param string $type identifier for the resource type being loaded
  240. * @param string $path path relative to resource base path containing the resource types
  241. * @param null|string $namespace sub-component namespace to append to base namespace that qualifies this resource type
  242. * @return Zend_Loader_Autoloader_Resource
  243. */
  244. public function addResourceType($type, $path, $namespace = null)
  245. {
  246. $type = strtolower($type);
  247. if (!isset($this->_resourceTypes[$type])) {
  248. if (null === $namespace) {
  249. require_once 'Zend/Loader/Exception.php';
  250. throw new Zend_Loader_Exception('Initial definition of a resource type must include a namespace');
  251. }
  252. $namespaceTopLevel = $this->getNamespace();
  253. $namespace = ucfirst(trim($namespace, '_'));
  254. $this->_resourceTypes[$type] = array(
  255. 'namespace' => empty($namespaceTopLevel) ? $namespace : $namespaceTopLevel . '_' . $namespace,
  256. );
  257. }
  258. if (!is_string($path)) {
  259. require_once 'Zend/Loader/Exception.php';
  260. throw new Zend_Loader_Exception('Invalid path specification provided; must be string');
  261. }
  262. $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/');
  263. $component = $this->_resourceTypes[$type]['namespace'];
  264. $this->_components[$component] = $this->_resourceTypes[$type]['path'];
  265. return $this;
  266. }
  267. /**
  268. * Add multiple resources at once
  269. *
  270. * $types should be an associative array of resource type => specification
  271. * pairs. Each specification should be an associative array containing
  272. * minimally the 'path' key (specifying the path relative to the resource
  273. * base path) and optionally the 'namespace' key (indicating the subcomponent
  274. * namespace to append to the resource namespace).
  275. *
  276. * As an example:
  277. * <code>
  278. * $loader->addResourceTypes(array(
  279. * 'model' => array(
  280. * 'path' => 'models',
  281. * 'namespace' => 'Model',
  282. * ),
  283. * 'form' => array(
  284. * 'path' => 'forms',
  285. * 'namespace' => 'Form',
  286. * ),
  287. * ));
  288. * </code>
  289. *
  290. * @param array $types
  291. * @return Zend_Loader_Autoloader_Resource
  292. */
  293. public function addResourceTypes(array $types)
  294. {
  295. foreach ($types as $type => $spec) {
  296. if (!is_array($spec)) {
  297. require_once 'Zend/Loader/Exception.php';
  298. throw new Zend_Loader_Exception('addResourceTypes() expects an array of arrays');
  299. }
  300. if (!isset($spec['path'])) {
  301. require_once 'Zend/Loader/Exception.php';
  302. throw new Zend_Loader_Exception('addResourceTypes() expects each array to include a paths element');
  303. }
  304. $paths = $spec['path'];
  305. $namespace = null;
  306. if (isset($spec['namespace'])) {
  307. $namespace = $spec['namespace'];
  308. }
  309. $this->addResourceType($type, $paths, $namespace);
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Overwrite existing and set multiple resource types at once
  315. *
  316. * @see Zend_Loader_Autoloader_Resource::addResourceTypes()
  317. * @param array $types
  318. * @return Zend_Loader_Autoloader_Resource
  319. */
  320. public function setResourceTypes(array $types)
  321. {
  322. $this->clearResourceTypes();
  323. return $this->addResourceTypes($types);
  324. }
  325. /**
  326. * Retrieve resource type mappings
  327. *
  328. * @return array
  329. */
  330. public function getResourceTypes()
  331. {
  332. return $this->_resourceTypes;
  333. }
  334. /**
  335. * Is the requested resource type defined?
  336. *
  337. * @param string $type
  338. * @return bool
  339. */
  340. public function hasResourceType($type)
  341. {
  342. return isset($this->_resourceTypes[$type]);
  343. }
  344. /**
  345. * Remove the requested resource type
  346. *
  347. * @param string $type
  348. * @return Zend_Loader_Autoloader_Resource
  349. */
  350. public function removeResourceType($type)
  351. {
  352. if ($this->hasResourceType($type)) {
  353. $namespace = $this->_resourceTypes[$type]['namespace'];
  354. unset($this->_components[$namespace]);
  355. unset($this->_resourceTypes[$type]);
  356. }
  357. return $this;
  358. }
  359. /**
  360. * Clear all resource types
  361. *
  362. * @return Zend_Loader_Autoloader_Resource
  363. */
  364. public function clearResourceTypes()
  365. {
  366. $this->_resourceTypes = array();
  367. $this->_components = array();
  368. return $this;
  369. }
  370. /**
  371. * Set default resource type to use when calling load()
  372. *
  373. * @param string $type
  374. * @return Zend_Loader_Autoloader_Resource
  375. */
  376. public function setDefaultResourceType($type)
  377. {
  378. if ($this->hasResourceType($type)) {
  379. $this->_defaultResourceType = $type;
  380. }
  381. return $this;
  382. }
  383. /**
  384. * Get default resource type to use when calling load()
  385. *
  386. * @return string|null
  387. */
  388. public function getDefaultResourceType()
  389. {
  390. return $this->_defaultResourceType;
  391. }
  392. /**
  393. * Object registry and factory
  394. *
  395. * Loads the requested resource of type $type (or uses the default resource
  396. * type if none provided). If the resource has been loaded previously,
  397. * returns the previous instance; otherwise, instantiates it.
  398. *
  399. * @param string $resource
  400. * @param string $type
  401. * @return object
  402. * @throws Zend_Loader_Exception if resource type not specified or invalid
  403. */
  404. public function load($resource, $type = null)
  405. {
  406. if (null === $type) {
  407. $type = $this->getDefaultResourceType();
  408. if (empty($type)) {
  409. require_once 'Zend/Loader/Exception.php';
  410. throw new Zend_Loader_Exception('No resource type specified');
  411. }
  412. }
  413. if (!$this->hasResourceType($type)) {
  414. require_once 'Zend/Loader/Exception.php';
  415. throw new Zend_Loader_Exception('Invalid resource type specified');
  416. }
  417. $namespace = $this->_resourceTypes[$type]['namespace'];
  418. $class = $namespace . '_' . ucfirst($resource);
  419. if (!isset($this->_resources[$class])) {
  420. $this->_resources[$class] = new $class;
  421. }
  422. return $this->_resources[$class];
  423. }
  424. }