PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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