BootstrapAbstract.php 18 KB

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