BootstrapAbstract.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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-2010 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-2010 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. * Flattened (lowercase) option keys used for lookups
  55. *
  56. * @var array
  57. */
  58. protected $_optionKeys = array();
  59. /**
  60. * @var array
  61. */
  62. protected $_options = array();
  63. /**
  64. * @var Zend_Loader_PluginLoader_Interface
  65. */
  66. protected $_pluginLoader;
  67. /**
  68. * @var array Class-based resource plugins
  69. */
  70. protected $_pluginResources = array();
  71. /**
  72. * @var array Initializers that have been run
  73. */
  74. protected $_run = array();
  75. /**
  76. * @var array Initializers that have been started but not yet completed (circular dependency detection)
  77. */
  78. protected $_started = array();
  79. /**
  80. * Constructor
  81. *
  82. * Sets application object, initializes options, and prepares list of
  83. * initializer methods.
  84. *
  85. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  86. * @return void
  87. * @throws Zend_Application_Bootstrap_Exception When invalid application is provided
  88. */
  89. public function __construct($application)
  90. {
  91. $this->setApplication($application);
  92. $options = $application->getOptions();
  93. $this->setOptions($options);
  94. }
  95. /**
  96. * Set class state
  97. *
  98. * @param array $options
  99. * @return Zend_Application_Bootstrap_BootstrapAbstract
  100. */
  101. public function setOptions(array $options)
  102. {
  103. $this->_options = $this->mergeOptions($this->_options, $options);
  104. $options = array_change_key_case($options, CASE_LOWER);
  105. $this->_optionKeys = array_merge($this->_optionKeys, array_keys($options));
  106. $methods = get_class_methods($this);
  107. foreach ($methods as $key => $method) {
  108. $methods[$key] = strtolower($method);
  109. }
  110. if (array_key_exists('pluginpaths', $options)) {
  111. $pluginLoader = $this->getPluginLoader();
  112. foreach ($options['pluginpaths'] as $prefix => $path) {
  113. $pluginLoader->addPrefixPath($prefix, $path);
  114. }
  115. unset($options['pluginpaths']);
  116. }
  117. foreach ($options as $key => $value) {
  118. $method = 'set' . strtolower($key);
  119. if (in_array($method, $methods)) {
  120. $this->$method($value);
  121. } elseif ('resources' == $key) {
  122. foreach ($value as $resource => $resourceOptions) {
  123. $this->registerPluginResource($resource, $resourceOptions);
  124. }
  125. }
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Get current options from bootstrap
  131. *
  132. * @return array
  133. */
  134. public function getOptions()
  135. {
  136. return $this->_options;
  137. }
  138. /**
  139. * Is an option present?
  140. *
  141. * @param string $key
  142. * @return bool
  143. */
  144. public function hasOption($key)
  145. {
  146. return in_array(strtolower($key), $this->_optionKeys);
  147. }
  148. /**
  149. * Retrieve a single option
  150. *
  151. * @param string $key
  152. * @return mixed
  153. */
  154. public function getOption($key)
  155. {
  156. if ($this->hasOption($key)) {
  157. $options = $this->getOptions();
  158. $options = array_change_key_case($options, CASE_LOWER);
  159. return $options[strtolower($key)];
  160. }
  161. return null;
  162. }
  163. /**
  164. * Merge options recursively
  165. *
  166. * @param array $array1
  167. * @param mixed $array2
  168. * @return array
  169. */
  170. public function mergeOptions(array $array1, $array2 = null)
  171. {
  172. if (is_array($array2)) {
  173. foreach ($array2 as $key => $val) {
  174. if (is_array($array2[$key])) {
  175. $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
  176. ? $this->mergeOptions($array1[$key], $array2[$key])
  177. : $array2[$key];
  178. } else {
  179. $array1[$key] = $val;
  180. }
  181. }
  182. }
  183. return $array1;
  184. }
  185. /**
  186. * Get class resources (as resource/method pairs)
  187. *
  188. * Uses get_class_methods() by default, reflection on prior to 5.2.6,
  189. * as a bug prevents the usage of get_class_methods() there.
  190. *
  191. * @return array
  192. */
  193. public function getClassResources()
  194. {
  195. if (null === $this->_classResources) {
  196. if (version_compare(PHP_VERSION, '5.2.6') === -1) {
  197. $class = new ReflectionObject($this);
  198. $classMethods = $class->getMethods();
  199. $methodNames = array();
  200. foreach ($classMethods as $method) {
  201. $methodNames[] = $method->getName();
  202. }
  203. } else {
  204. $methodNames = get_class_methods($this);
  205. }
  206. $this->_classResources = array();
  207. foreach ($methodNames as $method) {
  208. if (5 < strlen($method) && '_init' === substr($method, 0, 5)) {
  209. $this->_classResources[strtolower(substr($method, 5))] = $method;
  210. }
  211. }
  212. }
  213. return $this->_classResources;
  214. }
  215. /**
  216. * Get class resource names
  217. *
  218. * @return array
  219. */
  220. public function getClassResourceNames()
  221. {
  222. $resources = $this->getClassResources();
  223. return array_keys($resources);
  224. }
  225. /**
  226. * Register a new resource plugin
  227. *
  228. * @param string|Zend_Application_Resource_Resource $resource
  229. * @param mixed $options
  230. * @return Zend_Application_Bootstrap_BootstrapAbstract
  231. * @throws Zend_Application_Bootstrap_Exception When invalid resource is provided
  232. */
  233. public function registerPluginResource($resource, $options = null)
  234. {
  235. if ($resource instanceof Zend_Application_Resource_Resource) {
  236. $resource->setBootstrap($this);
  237. $pluginName = $this->_resolvePluginResourceName($resource);
  238. $this->_pluginResources[$pluginName] = $resource;
  239. return $this;
  240. }
  241. if (!is_string($resource)) {
  242. throw new Zend_Application_Bootstrap_Exception('Invalid resource provided to ' . __METHOD__);
  243. }
  244. $this->_pluginResources[$resource] = $options;
  245. return $this;
  246. }
  247. /**
  248. * Unregister a resource from the bootstrap
  249. *
  250. * @param string|Zend_Application_Resource_Resource $resource
  251. * @return Zend_Application_Bootstrap_BootstrapAbstract
  252. * @throws Zend_Application_Bootstrap_Exception When unknown resource type is provided
  253. */
  254. public function unregisterPluginResource($resource)
  255. {
  256. if ($resource instanceof Zend_Application_Resource_Resource) {
  257. if ($index = array_search($resource, $this->_pluginResources, true)) {
  258. unset($this->_pluginResources[$index]);
  259. }
  260. return $this;
  261. }
  262. if (!is_string($resource)) {
  263. throw new Zend_Application_Bootstrap_Exception('Unknown resource type provided to ' . __METHOD__);
  264. }
  265. $resource = strtolower($resource);
  266. if (array_key_exists($resource, $this->_pluginResources)) {
  267. unset($this->_pluginResources[$resource]);
  268. }
  269. return $this;
  270. }
  271. /**
  272. * Is the requested plugin resource registered?
  273. *
  274. * @param string $resource
  275. * @return bool
  276. */
  277. public function hasPluginResource($resource)
  278. {
  279. return (null !== $this->getPluginResource($resource));
  280. }
  281. /**
  282. * Get a registered plugin resource
  283. *
  284. * @param string $resourceName
  285. * @return Zend_Application_Resource_Resource
  286. */
  287. public function getPluginResource($resource)
  288. {
  289. if (array_key_exists(strtolower($resource), $this->_pluginResources)) {
  290. $resource = strtolower($resource);
  291. if (!$this->_pluginResources[$resource] instanceof Zend_Application_Resource_Resource) {
  292. $resourceName = $this->_loadPluginResource($resource, $this->_pluginResources[$resource]);
  293. if (!$resourceName) {
  294. throw new Zend_Application_Bootstrap_Exception(sprintf('Unable to resolve plugin "%s"; no corresponding plugin with that name', $resource));
  295. }
  296. $resource = $resourceName;
  297. }
  298. return $this->_pluginResources[$resource];
  299. }
  300. foreach ($this->_pluginResources as $plugin => $spec) {
  301. if ($spec instanceof Zend_Application_Resource_Resource) {
  302. $pluginName = $this->_resolvePluginResourceName($spec);
  303. if (0 === strcasecmp($resource, $pluginName)) {
  304. unset($this->_pluginResources[$plugin]);
  305. $this->_pluginResources[$pluginName] = $spec;
  306. return $spec;
  307. }
  308. continue;
  309. }
  310. if (false !== $pluginName = $this->_loadPluginResource($plugin, $spec)) {
  311. if (0 === strcasecmp($resource, $pluginName)) {
  312. return $this->_pluginResources[$pluginName];
  313. }
  314. continue;
  315. }
  316. if (class_exists($plugin)) { //@SEE ZF-7550
  317. $spec = (array) $spec;
  318. $spec['bootstrap'] = $this;
  319. $instance = new $plugin($spec);
  320. $pluginName = $this->_resolvePluginResourceName($instance);
  321. unset($this->_pluginResources[$plugin]);
  322. $this->_pluginResources[$pluginName] = $instance;
  323. if (0 === strcasecmp($resource, $pluginName)) {
  324. return $instance;
  325. }
  326. }
  327. }
  328. return null;
  329. }
  330. /**
  331. * Retrieve all plugin resources
  332. *
  333. * @return array
  334. */
  335. public function getPluginResources()
  336. {
  337. foreach (array_keys($this->_pluginResources) as $resource) {
  338. $this->getPluginResource($resource);
  339. }
  340. return $this->_pluginResources;
  341. }
  342. /**
  343. * Retrieve plugin resource names
  344. *
  345. * @return array
  346. */
  347. public function getPluginResourceNames()
  348. {
  349. $this->getPluginResources();
  350. return array_keys($this->_pluginResources);
  351. }
  352. /**
  353. * Set plugin loader for loading resources
  354. *
  355. * @param Zend_Loader_PluginLoader_Interface $loader
  356. * @return Zend_Application_Bootstrap_BootstrapAbstract
  357. */
  358. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  359. {
  360. $this->_pluginLoader = $loader;
  361. return $this;
  362. }
  363. /**
  364. * Get the plugin loader for resources
  365. *
  366. * @return Zend_Loader_PluginLoader_Interface
  367. */
  368. public function getPluginLoader()
  369. {
  370. if ($this->_pluginLoader === null) {
  371. $options = array(
  372. 'Zend_Application_Resource' => 'Zend/Application/Resource'
  373. );
  374. $this->_pluginLoader = new Zend_Loader_PluginLoader($options);
  375. }
  376. return $this->_pluginLoader;
  377. }
  378. /**
  379. * Set application/parent bootstrap
  380. *
  381. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  382. * @return Zend_Application_Bootstrap_BootstrapAbstract
  383. */
  384. public function setApplication($application)
  385. {
  386. if (($application instanceof Zend_Application)
  387. || ($application instanceof Zend_Application_Bootstrap_Bootstrapper)
  388. ) {
  389. if ($application === $this) {
  390. throw new Zend_Application_Bootstrap_Exception('Cannot set application to same object; creates recursion');
  391. }
  392. $this->_application = $application;
  393. } else {
  394. throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor (received "' . get_class($application) . '" instance)');
  395. }
  396. return $this;
  397. }
  398. /**
  399. * Retrieve parent application instance
  400. *
  401. * @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
  402. */
  403. public function getApplication()
  404. {
  405. return $this->_application;
  406. }
  407. /**
  408. * Retrieve application environment
  409. *
  410. * @return string
  411. */
  412. public function getEnvironment()
  413. {
  414. if (null === $this->_environment) {
  415. $this->_environment = $this->getApplication()->getEnvironment();
  416. }
  417. return $this->_environment;
  418. }
  419. /**
  420. * Set resource container
  421. *
  422. * By default, if a resource callback has a non-null return value, this
  423. * value will be stored in a container using the resource name as the
  424. * key.
  425. *
  426. * Containers must be objects, and must allow setting public properties.
  427. *
  428. * @param object $container
  429. * @return Zend_Application_Bootstrap_BootstrapAbstract
  430. */
  431. public function setContainer($container)
  432. {
  433. if (!is_object($container)) {
  434. throw new Zend_Application_Bootstrap_Exception('Resource containers must be objects');
  435. }
  436. $this->_container = $container;
  437. return $this;
  438. }
  439. /**
  440. * Retrieve resource container
  441. *
  442. * @return object
  443. */
  444. public function getContainer()
  445. {
  446. if (null === $this->_container) {
  447. $this->setContainer(new Zend_Registry());
  448. }
  449. return $this->_container;
  450. }
  451. /**
  452. * Determine if a resource has been stored in the container
  453. *
  454. * During bootstrap resource initialization, you may return a value. If
  455. * you do, it will be stored in the {@link setContainer() container}.
  456. * You can use this method to determine if a value was stored.
  457. *
  458. * @param string $name
  459. * @return bool
  460. */
  461. public function hasResource($name)
  462. {
  463. $resource = strtolower($name);
  464. $container = $this->getContainer();
  465. return isset($container->{$resource});
  466. }
  467. /**
  468. * Retrieve a resource from the container
  469. *
  470. * During bootstrap resource initialization, you may return a value. If
  471. * you do, it will be stored in the {@link setContainer() container}.
  472. * You can use this method to retrieve that value.
  473. *
  474. * If no value was returned, this will return a null value.
  475. *
  476. * @param string $name
  477. * @return null|mixed
  478. */
  479. public function getResource($name)
  480. {
  481. $resource = strtolower($name);
  482. $container = $this->getContainer();
  483. if ($this->hasResource($resource)) {
  484. return $container->{$resource};
  485. }
  486. return null;
  487. }
  488. /**
  489. * Implement PHP's magic to retrieve a ressource
  490. * in the bootstrap
  491. *
  492. * @param string $prop
  493. * @return null|mixed
  494. */
  495. public function __get($prop)
  496. {
  497. return $this->getResource($prop);
  498. }
  499. /**
  500. * Implement PHP's magic to ask for the
  501. * existence of a ressource in the bootstrap
  502. *
  503. * @param string $prop
  504. * @return bool
  505. */
  506. public function __isset($prop)
  507. {
  508. return $this->hasResource($prop);
  509. }
  510. /**
  511. * Bootstrap individual, all, or multiple resources
  512. *
  513. * Marked as final to prevent issues when subclassing and naming the
  514. * child class 'Bootstrap' (in which case, overriding this method
  515. * would result in it being treated as a constructor).
  516. *
  517. * If you need to override this functionality, override the
  518. * {@link _bootstrap()} method.
  519. *
  520. * @param null|string|array $resource
  521. * @return Zend_Application_Bootstrap_BootstrapAbstract
  522. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  523. */
  524. final public function bootstrap($resource = null)
  525. {
  526. $this->_bootstrap($resource);
  527. return $this;
  528. }
  529. /**
  530. * Overloading: intercept calls to bootstrap<resourcename>() methods
  531. *
  532. * @param string $method
  533. * @param array $args
  534. * @return void
  535. * @throws Zend_Application_Bootstrap_Exception On invalid method name
  536. */
  537. public function __call($method, $args)
  538. {
  539. if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
  540. $resource = substr($method, 9);
  541. return $this->bootstrap($resource);
  542. }
  543. throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
  544. }
  545. /**
  546. * Bootstrap implementation
  547. *
  548. * This method may be overridden to provide custom bootstrapping logic.
  549. * It is the sole method called by {@link bootstrap()}.
  550. *
  551. * @param null|string|array $resource
  552. * @return void
  553. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  554. */
  555. protected function _bootstrap($resource = null)
  556. {
  557. if (null === $resource) {
  558. foreach ($this->getClassResourceNames() as $resource) {
  559. $this->_executeResource($resource);
  560. }
  561. foreach ($this->getPluginResourceNames() as $resource) {
  562. $this->_executeResource($resource);
  563. }
  564. } elseif (is_string($resource)) {
  565. $this->_executeResource($resource);
  566. } elseif (is_array($resource)) {
  567. foreach ($resource as $r) {
  568. $this->_executeResource($r);
  569. }
  570. } else {
  571. throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
  572. }
  573. }
  574. /**
  575. * Execute a resource
  576. *
  577. * Checks to see if the resource has already been run. If not, it searches
  578. * first to see if a local method matches the resource, and executes that.
  579. * If not, it checks to see if a plugin resource matches, and executes that
  580. * if found.
  581. *
  582. * Finally, if not found, it throws an exception.
  583. *
  584. * @param string $resource
  585. * @return void
  586. * @throws Zend_Application_Bootstrap_Exception When resource not found
  587. */
  588. protected function _executeResource($resource)
  589. {
  590. $resourceName = strtolower($resource);
  591. if (in_array($resourceName, $this->_run)) {
  592. return;
  593. }
  594. if (isset($this->_started[$resourceName]) && $this->_started[$resourceName]) {
  595. throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
  596. }
  597. $classResources = $this->getClassResources();
  598. if (array_key_exists($resourceName, $classResources)) {
  599. $this->_started[$resourceName] = true;
  600. $method = $classResources[$resourceName];
  601. $return = $this->$method();
  602. unset($this->_started[$resourceName]);
  603. $this->_markRun($resourceName);
  604. if (null !== $return) {
  605. $this->getContainer()->{$resourceName} = $return;
  606. }
  607. return;
  608. }
  609. if ($this->hasPluginResource($resource)) {
  610. $this->_started[$resourceName] = true;
  611. $plugin = $this->getPluginResource($resource);
  612. $return = $plugin->init();
  613. unset($this->_started[$resourceName]);
  614. $this->_markRun($resourceName);
  615. if (null !== $return) {
  616. $this->getContainer()->{$resourceName} = $return;
  617. }
  618. return;
  619. }
  620. throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
  621. }
  622. /**
  623. * Load a plugin resource
  624. *
  625. * @param string $resource
  626. * @param array|object|null $options
  627. * @return string|false
  628. */
  629. protected function _loadPluginResource($resource, $options)
  630. {
  631. $options = (array) $options;
  632. $options['bootstrap'] = $this;
  633. $className = $this->getPluginLoader()->load(strtolower($resource), false);
  634. if (!$className) {
  635. return false;
  636. }
  637. $instance = new $className($options);
  638. unset($this->_pluginResources[$resource]);
  639. if (isset($instance->_explicitType)) {
  640. $resource = $instance->_explicitType;
  641. }
  642. $resource = strtolower($resource);
  643. $this->_pluginResources[$resource] = $instance;
  644. return $resource;
  645. }
  646. /**
  647. * Mark a resource as having run
  648. *
  649. * @param string $resource
  650. * @return void
  651. */
  652. protected function _markRun($resource)
  653. {
  654. if (!in_array($resource, $this->_run)) {
  655. $this->_run[] = $resource;
  656. }
  657. }
  658. /**
  659. * Resolve a plugin resource name
  660. *
  661. * Uses, in order of preference
  662. * - $_explicitType property of resource
  663. * - Short name of resource (if a matching prefix path is found)
  664. * - class name (if none of the above are true)
  665. *
  666. * The name is then cast to lowercase.
  667. *
  668. * @param Zend_Application_Resource_Resource $resource
  669. * @return string
  670. */
  671. protected function _resolvePluginResourceName($resource)
  672. {
  673. if (isset($resource->_explicitType)) {
  674. $pluginName = $resource->_explicitType;
  675. } else {
  676. $className = get_class($resource);
  677. $pluginName = $className;
  678. $loader = $this->getPluginLoader();
  679. foreach ($loader->getPaths() as $prefix => $paths) {
  680. if (0 === strpos($className, $prefix)) {
  681. $pluginName = substr($className, strlen($prefix));
  682. $pluginName = trim($pluginName, '_');
  683. break;
  684. }
  685. }
  686. }
  687. $pluginName = strtolower($pluginName);
  688. return $pluginName;
  689. }
  690. }