BootstrapAbstract.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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_Application
  17. * @subpackage Bootstrap
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Abstract base class for bootstrap classes
  24. *
  25. * @uses Zend_Application_Bootstrap_Bootstrapper
  26. * @uses Zend_Application_Bootstrap_ResourceBootstrapper
  27. * @category Zend
  28. * @package Zend_Application
  29. * @subpackage Bootstrap
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Application_Bootstrap_BootstrapAbstract
  34. implements Zend_Application_Bootstrap_Bootstrapper,
  35. Zend_Application_Bootstrap_ResourceBootstrapper
  36. {
  37. /**
  38. * @var Zend_Application|Zend_Application_Bootstrap_Bootstrapper
  39. */
  40. protected $_application;
  41. /**
  42. * @var array Internal resource methods (resource/method pairs)
  43. */
  44. protected $_classResources;
  45. /**
  46. * @var object Resource container
  47. */
  48. protected $_container;
  49. /**
  50. * @var string
  51. */
  52. protected $_environment;
  53. /**
  54. * @var array
  55. */
  56. protected $_options = array();
  57. /**
  58. * @var Zend_Loader_PluginLoader_Interface
  59. */
  60. protected $_pluginLoader;
  61. /**
  62. * @var array Class-based resource plugins
  63. */
  64. protected $_pluginResources = array();
  65. /**
  66. * @var array Initializers that have been run
  67. */
  68. protected $_run = array();
  69. /**
  70. * @var array Initializers that have been started but not yet completed (circular dependency detection)
  71. */
  72. protected $_started = array();
  73. /**
  74. * Constructor
  75. *
  76. * Sets application object, initializes options, and prepares list of
  77. * initializer methods.
  78. *
  79. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  80. * @return void
  81. * @throws Zend_Application_Bootstrap_Exception When invalid applicaiton is provided
  82. */
  83. public function __construct($application)
  84. {
  85. $this->setApplication($application);
  86. $options = $application->getOptions();
  87. $this->setOptions($options);
  88. }
  89. /**
  90. * Set class state
  91. *
  92. * @param array $options
  93. * @return Zend_Application_Bootstrap_BootstrapAbstract
  94. */
  95. public function setOptions(array $options)
  96. {
  97. $options = array_change_key_case($options, CASE_LOWER);
  98. $methods = get_class_methods($this);
  99. foreach ($methods as $key => $method) {
  100. $methods[$key] = strtolower($method);
  101. }
  102. if (array_key_exists('pluginpaths', $options)) {
  103. $pluginLoader = $this->getPluginLoader();
  104. foreach ($options['pluginpaths'] as $prefix => $path) {
  105. $pluginLoader->addPrefixPath($prefix, $path);
  106. }
  107. unset($options['pluginpaths']);
  108. }
  109. foreach ($options as $key => $value) {
  110. $method = 'set' . strtolower($key);
  111. if (in_array($method, $methods)) {
  112. $this->$method($value);
  113. } elseif ('resources' == $key) {
  114. foreach ($value as $resource => $resourceOptions) {
  115. $this->registerPluginResource($resource, $resourceOptions);
  116. }
  117. }
  118. }
  119. $this->_options = array_merge_recursive($this->_options, $options);
  120. return $this;
  121. }
  122. /**
  123. * Get current options from bootstrap
  124. *
  125. * @return array
  126. */
  127. public function getOptions()
  128. {
  129. return $this->_options;
  130. }
  131. /**
  132. * Is an option present?
  133. *
  134. * @param string $key
  135. * @return bool
  136. */
  137. public function hasOption($key)
  138. {
  139. return array_key_exists($key, $this->_options);
  140. }
  141. /**
  142. * Retrieve a single option
  143. *
  144. * @param string $key
  145. * @return mixed
  146. */
  147. public function getOption($key)
  148. {
  149. if ($this->hasOption($key)) {
  150. return $this->_options[$key];
  151. }
  152. return null;
  153. }
  154. /**
  155. * Get class resources (as resource/method pairs)
  156. *
  157. * Uses get_class_methods() by default, reflection on prior to 5.2.6,
  158. * as a bug prevents the usage of get_class_methods() there.
  159. *
  160. * @return array
  161. */
  162. public function getClassResources()
  163. {
  164. if (null === $this->_classResources) {
  165. if (version_compare(PHP_VERSION, '5.2.6') === -1) {
  166. $class = new ReflectionObject($this);
  167. $classMethods = $class->getMethods();
  168. $methodNames = array();
  169. foreach ($classMethods as $method) {
  170. $methodNames[] = $method->getName();
  171. }
  172. } else {
  173. $methodNames = get_class_methods($this);
  174. }
  175. $this->_classResources = array();
  176. foreach ($methodNames as $method) {
  177. if (5 < strlen($method) && '_init' === substr($method, 0, 5)) {
  178. $this->_classResources[strtolower(substr($method, 5))] = $method;
  179. }
  180. }
  181. }
  182. return $this->_classResources;
  183. }
  184. /**
  185. * Get class resource names
  186. *
  187. * @return array
  188. */
  189. public function getClassResourceNames()
  190. {
  191. $resources = $this->getClassResources();
  192. return array_keys($resources);
  193. }
  194. /**
  195. * Register a new resource plugin
  196. *
  197. * @param string|Zend_Application_Resource_Resource $resource
  198. * @param mixed $options
  199. * @return Zend_Application_Bootstrap_BootstrapAbstract
  200. * @throws Zend_Application_Bootstrap_Exception When invalid resource is provided
  201. */
  202. public function registerPluginResource($resource, $options = null)
  203. {
  204. if (is_string($resource) && class_exists($resource)) {
  205. $options = (array) $options;
  206. $options['bootstrap'] = $this;
  207. $resource = new $resource($options);
  208. }
  209. if ($resource instanceof Zend_Application_Resource_Resource) {
  210. $resource->setBootstrap($this);
  211. $vars = get_object_vars($resource);
  212. if (isset($vars['_explicitType'])) {
  213. $pluginName = strtolower($vars['_explicitType']);
  214. } else {
  215. $className = get_class($resource);
  216. $pluginName = strtolower($className);
  217. $loader = $this->getPluginLoader();
  218. foreach ($loader->getPaths() as $prefix => $paths) {
  219. if (0 === strpos($className, $prefix)) {
  220. $pluginName = substr($className, strlen($prefix));
  221. $pluginName = strtolower(trim($pluginName, '_'));
  222. }
  223. }
  224. }
  225. $this->_pluginResources[$pluginName] = $resource;
  226. return $this;
  227. }
  228. if (!is_string($resource)) {
  229. throw new Zend_Application_Bootstrap_Exception('Invalid resource provided to ' . __METHOD__);
  230. }
  231. $resource = strtolower($resource);
  232. $this->_pluginResources[$resource] = $options;
  233. return $this;
  234. }
  235. /**
  236. * Unregister a resource from the bootstrap
  237. *
  238. * @param string|Zend_Application_Resource_Resource $resource
  239. * @return Zend_Application_Bootstrap_BootstrapAbstract
  240. * @throws Zend_Application_Bootstrap_Exception When unknown resource type is provided
  241. */
  242. public function unregisterPluginResource($resource)
  243. {
  244. if ($resource instanceof Zend_Application_Resource_Resource) {
  245. if ($index = array_search($resource, $this->_pluginResources, true)) {
  246. unset($this->_pluginResources[$index]);
  247. }
  248. return $this;
  249. }
  250. if (!is_string($resource)) {
  251. throw new Zend_Application_Bootstrap_Exception('Unknown resource type provided to ' . __METHOD__);
  252. }
  253. $resource = strtolower($resource);
  254. if (array_key_exists($resource, $this->_pluginResources)) {
  255. unset($this->_pluginResources[$resource]);
  256. }
  257. return $this;
  258. }
  259. /**
  260. * Is the requested plugin resource registered?
  261. *
  262. * @param string $resource
  263. * @return bool
  264. */
  265. public function hasPluginResource($resource)
  266. {
  267. $resource = strtolower($resource);
  268. return array_key_exists($resource, $this->_pluginResources);
  269. }
  270. /**
  271. * Get a registered plugin resource
  272. *
  273. * @param string $resourceName
  274. * @return Zend_Application_Resource_Resource
  275. */
  276. public function getPluginResource($resource)
  277. {
  278. $resource = strtolower($resource);
  279. if (!array_key_exists($resource, $this->_pluginResources)) {
  280. return null;
  281. }
  282. if (!$this->_pluginResources[$resource] instanceof Zend_Application_Resource_Resource) {
  283. $options = (array) $this->_pluginResources[$resource];
  284. $options['bootstrap'] = $this;
  285. $className = $this->getPluginLoader()->load($resource);
  286. $this->_pluginResources[$resource] = new $className($options);
  287. }
  288. $plugin = $this->_pluginResources[$resource];
  289. $plugin->setBootstrap($this);
  290. return $plugin;
  291. }
  292. /**
  293. * Retrieve all plugin resources
  294. *
  295. * @return array
  296. */
  297. public function getPluginResources()
  298. {
  299. $resources = array();
  300. foreach (array_keys($this->_pluginResources) as $resource) {
  301. $resources[$resource] = $this->getPluginResource($resource);
  302. }
  303. return $resources;
  304. }
  305. /**
  306. * Retrieve plugin resource names
  307. *
  308. * @return array
  309. */
  310. public function getPluginResourceNames()
  311. {
  312. return array_keys($this->_pluginResources);
  313. }
  314. /**
  315. * Set plugin loader for loading resources
  316. *
  317. * @param Zend_Loader_PluginLoader_Interface $loader
  318. * @return Zend_Application_Bootstrap_BootstrapAbstract
  319. */
  320. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  321. {
  322. $this->_pluginLoader = $loader;
  323. return $this;
  324. }
  325. /**
  326. * Get the plugin loader for resources
  327. *
  328. * @return Zend_Loader_PluginLoader_Interface
  329. */
  330. public function getPluginLoader()
  331. {
  332. if ($this->_pluginLoader === null) {
  333. $options = array(
  334. 'Zend_Application_Resource' => 'Zend/Application/Resource'
  335. );
  336. $this->_pluginLoader = new Zend_Loader_PluginLoader($options);
  337. }
  338. return $this->_pluginLoader;
  339. }
  340. /**
  341. * Set application/parent bootstrap
  342. *
  343. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  344. * @return Zend_Application_Bootstrap_BootstrapAbstract
  345. */
  346. public function setApplication($application)
  347. {
  348. if (($application instanceof Zend_Application)
  349. || ($application instanceof Zend_Application_Bootstrap_Bootstrapper)
  350. ) {
  351. $this->_application = $application;
  352. } else {
  353. throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor');
  354. }
  355. return $this;
  356. }
  357. /**
  358. * Retrieve parent application instance
  359. *
  360. * @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
  361. */
  362. public function getApplication()
  363. {
  364. return $this->_application;
  365. }
  366. /**
  367. * Retrieve application environment
  368. *
  369. * @return string
  370. */
  371. public function getEnvironment()
  372. {
  373. if (null === $this->_environment) {
  374. $this->_environment = $this->getApplication()->getEnvironment();
  375. }
  376. return $this->_environment;
  377. }
  378. /**
  379. * Set resource container
  380. *
  381. * By default, if a resource callback has a non-null return value, this
  382. * value will be stored in a container using the resource name as the
  383. * key.
  384. *
  385. * Containers must be objects, and must allow setting public properties.
  386. *
  387. * @param object $container
  388. * @return Zend_Application_Bootstrap_BootstrapAbstract
  389. */
  390. public function setContainer($container)
  391. {
  392. if (!is_object($container)) {
  393. throw new Zend_Application_Bootstrap_Exception('Resource containers must be objects');
  394. }
  395. $this->_container = $container;
  396. return $this;
  397. }
  398. /**
  399. * Retrieve resource container
  400. *
  401. * @return object
  402. */
  403. public function getContainer()
  404. {
  405. if (null === $this->_container) {
  406. $this->setContainer(new Zend_Registry());
  407. }
  408. return $this->_container;
  409. }
  410. /**
  411. * Determine if a resource has been stored in the container
  412. *
  413. * During bootstrap resource initialization, you may return a value. If
  414. * you do, it will be stored in the {@link setContainer() container}.
  415. * You can use this method to determine if a value was stored.
  416. *
  417. * @param string $name
  418. * @return bool
  419. */
  420. public function hasResource($name)
  421. {
  422. $resource = strtolower($name);
  423. $container = $this->getContainer();
  424. return isset($container->{$resource});
  425. }
  426. /**
  427. * Retrieve a resource from the container
  428. *
  429. * During bootstrap resource initialization, you may return a value. If
  430. * you do, it will be stored in the {@link setContainer() container}.
  431. * You can use this method to retrieve that value.
  432. *
  433. * If no value was returned, this will return a null value.
  434. *
  435. * @param string $name
  436. * @return null|mixed
  437. */
  438. public function getResource($name)
  439. {
  440. $resource = strtolower($name);
  441. $container = $this->getContainer();
  442. if ($this->hasResource($resource)) {
  443. return $container->{$resource};
  444. }
  445. return null;
  446. }
  447. /**
  448. * Bootstrap individual, all, or multiple resources
  449. *
  450. * Marked as final to prevent issues when subclassing and naming the
  451. * child class 'Bootstrap' (in which case, overriding this method
  452. * would result in it being treated as a constructor).
  453. *
  454. * If you need to override this functionality, override the
  455. * {@link _bootstrap()} method.
  456. *
  457. * @param null|string|array $resource
  458. * @return Zend_Application_Bootstrap_BootstrapAbstract
  459. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  460. */
  461. final public function bootstrap($resource = null)
  462. {
  463. $this->_bootstrap($resource);
  464. return $this;
  465. }
  466. /**
  467. * Overloading: intercept calls to bootstrap<resourcename>() methods
  468. *
  469. * @param string $method
  470. * @param array $args
  471. * @return void
  472. * @throws Zend_Application_Bootstrap_Exception On invalid method name
  473. */
  474. public function __call($method, $args)
  475. {
  476. if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
  477. $resource = substr($method, 9);
  478. return $this->bootstrap($resource);
  479. }
  480. throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
  481. }
  482. /**
  483. * Bootstrap implementation
  484. *
  485. * This method may be overridden to provide custom bootstrapping logic.
  486. * It is the sole method called by {@link bootstrap()}.
  487. *
  488. * @param null|string|array $resource
  489. * @return void
  490. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  491. */
  492. protected function _bootstrap($resource = null)
  493. {
  494. if (null === $resource) {
  495. foreach ($this->getClassResourceNames() as $resource) {
  496. $this->_executeResource($resource);
  497. }
  498. foreach ($this->getPluginResourceNames() as $resource) {
  499. $this->_executeResource($resource);
  500. }
  501. } elseif (is_string($resource)) {
  502. $this->_executeResource($resource);
  503. } elseif (is_array($resource)) {
  504. foreach ($resource as $r) {
  505. $this->_executeResource($r);
  506. }
  507. } else {
  508. throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
  509. }
  510. }
  511. /**
  512. * Execute a resource
  513. *
  514. * Checks to see if the resource has already been run. If not, it searches
  515. * first to see if a local method matches the resource, and executes that.
  516. * If not, it checks to see if a plugin resource matches, and executes that
  517. * if found.
  518. *
  519. * Finally, if not found, it throws an exception.
  520. *
  521. * @param string $resource
  522. * @return void
  523. * @throws Zend_Application_Bootstrap_Exception When resource not found
  524. */
  525. protected function _executeResource($resource)
  526. {
  527. $resource = strtolower($resource);
  528. if (in_array($resource, $this->_run)) {
  529. return;
  530. }
  531. if (isset($this->_started[$resource]) && $this->_started[$resource]) {
  532. throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
  533. }
  534. $classResources = $this->getClassResources();
  535. if (array_key_exists($resource, $classResources)) {
  536. $this->_started[$resource] = true;
  537. $method = $classResources[$resource];
  538. $return = $this->$method();
  539. unset($this->_started[$resource]);
  540. $this->_markRun($resource);
  541. if (null !== $return) {
  542. $this->getContainer()->{$resource} = $return;
  543. }
  544. return;
  545. }
  546. if ($this->hasPluginResource($resource)) {
  547. $this->_started[$resource] = true;
  548. $plugin = $this->getPluginResource($resource);
  549. $return = $plugin->init();
  550. unset($this->_started[$resource]);
  551. $this->_markRun($resource);
  552. if (null !== $return) {
  553. $this->getContainer()->{$resource} = $return;
  554. }
  555. return;
  556. }
  557. throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
  558. }
  559. /**
  560. * Mark a resource as having run
  561. *
  562. * @param string $resource
  563. * @return void
  564. */
  565. protected function _markRun($resource)
  566. {
  567. if (!in_array($resource, $this->_run)) {
  568. $this->_run[] = $resource;
  569. }
  570. }
  571. }