BootstrapAbstract.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 = $this->_pluginResources[$resource];
  284. $className = $this->getPluginLoader()->load($resource);
  285. $this->_pluginResources[$resource] = new $className($options);
  286. }
  287. $plugin = $this->_pluginResources[$resource];
  288. $plugin->setBootstrap($this);
  289. return $plugin;
  290. }
  291. /**
  292. * Retrieve all plugin resources
  293. *
  294. * @return array
  295. */
  296. public function getPluginResources()
  297. {
  298. $resources = array();
  299. foreach (array_keys($this->_pluginResources) as $resource) {
  300. $resources[$resource] = $this->getPluginResource($resource);
  301. }
  302. return $resources;
  303. }
  304. /**
  305. * Retrieve plugin resource names
  306. *
  307. * @return array
  308. */
  309. public function getPluginResourceNames()
  310. {
  311. return array_keys($this->_pluginResources);
  312. }
  313. /**
  314. * Set plugin loader for loading resources
  315. *
  316. * @param Zend_Loader_PluginLoader_Interface $loader
  317. * @return Zend_Application_Bootstrap_BootstrapAbstract
  318. */
  319. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  320. {
  321. $this->_pluginLoader = $loader;
  322. return $this;
  323. }
  324. /**
  325. * Get the plugin loader for resources
  326. *
  327. * @return Zend_Loader_PluginLoader_Interface
  328. */
  329. public function getPluginLoader()
  330. {
  331. if ($this->_pluginLoader === null) {
  332. $options = array(
  333. 'Zend_Application_Resource' => 'Zend/Application/Resource'
  334. );
  335. $this->_pluginLoader = new Zend_Loader_PluginLoader($options);
  336. }
  337. return $this->_pluginLoader;
  338. }
  339. /**
  340. * Set application/parent bootstrap
  341. *
  342. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  343. * @return Zend_Application_Bootstrap_BootstrapAbstract
  344. */
  345. public function setApplication($application)
  346. {
  347. if (($application instanceof Zend_Application)
  348. || ($application instanceof Zend_Application_Bootstrap_Bootstrapper)
  349. ) {
  350. $this->_application = $application;
  351. } else {
  352. throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor');
  353. }
  354. return $this;
  355. }
  356. /**
  357. * Retrieve parent application instance
  358. *
  359. * @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
  360. */
  361. public function getApplication()
  362. {
  363. return $this->_application;
  364. }
  365. /**
  366. * Retrieve application environment
  367. *
  368. * @return string
  369. */
  370. public function getEnvironment()
  371. {
  372. if (null === $this->_environment) {
  373. $this->_environment = $this->getApplication()->getEnvironment();
  374. }
  375. return $this->_environment;
  376. }
  377. /**
  378. * Set resource container
  379. *
  380. * By default, if a resource callback has a non-null return value, this
  381. * value will be stored in a container using the resource name as the
  382. * key.
  383. *
  384. * Containers must be objects, and must allow setting public properties.
  385. *
  386. * @param object $container
  387. * @return Zend_Application_Bootstrap_BootstrapAbstract
  388. */
  389. public function setContainer($container)
  390. {
  391. if (!is_object($container)) {
  392. throw new Zend_Application_Bootstrap_Exception('Resource containers must be objects');
  393. }
  394. $this->_container = $container;
  395. return $this;
  396. }
  397. /**
  398. * Retrieve resource container
  399. *
  400. * @return object
  401. */
  402. public function getContainer()
  403. {
  404. if (null === $this->_container) {
  405. $this->setContainer(new Zend_Registry());
  406. }
  407. return $this->_container;
  408. }
  409. /**
  410. * Determine if a resource has been stored in the container
  411. *
  412. * During bootstrap resource initialization, you may return a value. If
  413. * you do, it will be stored in the {@link setContainer() container}.
  414. * You can use this method to determine if a value was stored.
  415. *
  416. * @param string $name
  417. * @return bool
  418. */
  419. public function hasResource($name)
  420. {
  421. $resource = strtolower($name);
  422. $container = $this->getContainer();
  423. return isset($container->{$resource});
  424. }
  425. /**
  426. * Retrieve a resource from the container
  427. *
  428. * During bootstrap resource initialization, you may return a value. If
  429. * you do, it will be stored in the {@link setContainer() container}.
  430. * You can use this method to retrieve that value.
  431. *
  432. * If no value was returned, this will return a null value.
  433. *
  434. * @param string $name
  435. * @return null|mixed
  436. */
  437. public function getResource($name)
  438. {
  439. $resource = strtolower($name);
  440. $container = $this->getContainer();
  441. if ($this->hasResource($resource)) {
  442. return $container->{$resource};
  443. }
  444. return null;
  445. }
  446. /**
  447. * Bootstrap individual, all, or multiple resources
  448. *
  449. * Marked as final to prevent issues when subclassing and naming the
  450. * child class 'Bootstrap' (in which case, overriding this method
  451. * would result in it being treated as a constructor).
  452. *
  453. * If you need to override this functionality, override the
  454. * {@link _bootstrap()} method.
  455. *
  456. * @param null|string|array $resource
  457. * @return Zend_Application_Bootstrap_BootstrapAbstract
  458. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  459. */
  460. final public function bootstrap($resource = null)
  461. {
  462. $this->_bootstrap($resource);
  463. return $this;
  464. }
  465. /**
  466. * Overloading: intercept calls to bootstrap<resourcename>() methods
  467. *
  468. * @param string $method
  469. * @param array $args
  470. * @return void
  471. * @throws Zend_Application_Bootstrap_Exception On invalid method name
  472. */
  473. public function __call($method, $args)
  474. {
  475. if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
  476. $resource = substr($method, 9);
  477. return $this->bootstrap($resource);
  478. }
  479. throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
  480. }
  481. /**
  482. * Bootstrap implementation
  483. *
  484. * This method may be overridden to provide custom bootstrapping logic.
  485. * It is the sole method called by {@link bootstrap()}.
  486. *
  487. * @param null|string|array $resource
  488. * @return void
  489. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  490. */
  491. protected function _bootstrap($resource = null)
  492. {
  493. if (null === $resource) {
  494. foreach ($this->getClassResourceNames() as $resource) {
  495. $this->_executeResource($resource);
  496. }
  497. foreach ($this->getPluginResourceNames() as $resource) {
  498. $this->_executeResource($resource);
  499. }
  500. } elseif (is_string($resource)) {
  501. $this->_executeResource($resource);
  502. } elseif (is_array($resource)) {
  503. foreach ($resource as $r) {
  504. $this->_executeResource($r);
  505. }
  506. } else {
  507. throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
  508. }
  509. }
  510. /**
  511. * Execute a resource
  512. *
  513. * Checks to see if the resource has already been run. If not, it searches
  514. * first to see if a local method matches the resource, and executes that.
  515. * If not, it checks to see if a plugin resource matches, and executes that
  516. * if found.
  517. *
  518. * Finally, if not found, it throws an exception.
  519. *
  520. * @param string $resource
  521. * @return void
  522. * @throws Zend_Application_Bootstrap_Exception When resource not found
  523. */
  524. protected function _executeResource($resource)
  525. {
  526. $resource = strtolower($resource);
  527. if (in_array($resource, $this->_run)) {
  528. return;
  529. }
  530. if (isset($this->_started[$resource]) && $this->_started[$resource]) {
  531. throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
  532. }
  533. $classResources = $this->getClassResources();
  534. if (array_key_exists($resource, $classResources)) {
  535. $this->_started[$resource] = true;
  536. $method = $classResources[$resource];
  537. $return = $this->$method();
  538. unset($this->_started[$resource]);
  539. $this->_markRun($resource);
  540. if (null !== $return) {
  541. $this->getContainer()->{$resource} = $return;
  542. }
  543. return;
  544. }
  545. if ($this->hasPluginResource($resource)) {
  546. $this->_started[$resource] = true;
  547. $plugin = $this->getPluginResource($resource);
  548. $return = $plugin->init();
  549. unset($this->_started[$resource]);
  550. $this->_markRun($resource);
  551. if (null !== $return) {
  552. $this->getContainer()->{$resource} = $return;
  553. }
  554. return;
  555. }
  556. throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
  557. }
  558. /**
  559. * Mark a resource as having run
  560. *
  561. * @param string $resource
  562. * @return void
  563. */
  564. protected function _markRun($resource)
  565. {
  566. if (!in_array($resource, $this->_run)) {
  567. $this->_run[] = $resource;
  568. }
  569. }
  570. }