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

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/Zend/Loader/Autoloader/Resource.php

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