BootstrapAbstract.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 applicaiton 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($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. }
  315. if (class_exists($plugin)) { //@SEE ZF-7550
  316. $spec = (array) $spec;
  317. $spec['bootstrap'] = $this;
  318. $instance = new $plugin($spec);
  319. $pluginName = $this->_resolvePluginResourceName($instance);
  320. unset($this->_pluginResources[$plugin]);
  321. $this->_pluginResources[$pluginName] = $instance;
  322. if (0 === strcasecmp($resource, $pluginName)) {
  323. return $instance;
  324. }
  325. }
  326. }
  327. return null;
  328. }
  329. /**
  330. * Retrieve all plugin resources
  331. *
  332. * @return array
  333. */
  334. public function getPluginResources()
  335. {
  336. foreach (array_keys($this->_pluginResources) as $resource) {
  337. $this->getPluginResource($resource);
  338. }
  339. return $this->_pluginResources;
  340. }
  341. /**
  342. * Retrieve plugin resource names
  343. *
  344. * @return array
  345. */
  346. public function getPluginResourceNames()
  347. {
  348. $this->getPluginResources();
  349. return array_keys($this->_pluginResources);
  350. }
  351. /**
  352. * Set plugin loader for loading resources
  353. *
  354. * @param Zend_Loader_PluginLoader_Interface $loader
  355. * @return Zend_Application_Bootstrap_BootstrapAbstract
  356. */
  357. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  358. {
  359. $this->_pluginLoader = $loader;
  360. return $this;
  361. }
  362. /**
  363. * Get the plugin loader for resources
  364. *
  365. * @return Zend_Loader_PluginLoader_Interface
  366. */
  367. public function getPluginLoader()
  368. {
  369. if ($this->_pluginLoader === null) {
  370. $options = array(
  371. 'Zend_Application_Resource' => 'Zend/Application/Resource'
  372. );
  373. $this->_pluginLoader = new Zend_Loader_PluginLoader($options);
  374. }
  375. return $this->_pluginLoader;
  376. }
  377. /**
  378. * Set application/parent bootstrap
  379. *
  380. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  381. * @return Zend_Application_Bootstrap_BootstrapAbstract
  382. */
  383. public function setApplication($application)
  384. {
  385. if (($application instanceof Zend_Application)
  386. || ($application instanceof Zend_Application_Bootstrap_Bootstrapper)
  387. ) {
  388. if ($application === $this) {
  389. throw new Zend_Application_Bootstrap_Exception('Cannot set application to same object; creates recursion');
  390. }
  391. $this->_application = $application;
  392. } else {
  393. throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor (received "' . get_class($application) . '" instance)');
  394. }
  395. return $this;
  396. }
  397. /**
  398. * Retrieve parent application instance
  399. *
  400. * @return Zend_Application|Zend_Application_Bootstrap_Bootstrapper
  401. */
  402. public function getApplication()
  403. {
  404. return $this->_application;
  405. }
  406. /**
  407. * Retrieve application environment
  408. *
  409. * @return string
  410. */
  411. public function getEnvironment()
  412. {
  413. if (null === $this->_environment) {
  414. $this->_environment = $this->getApplication()->getEnvironment();
  415. }
  416. return $this->_environment;
  417. }
  418. /**
  419. * Set resource container
  420. *
  421. * By default, if a resource callback has a non-null return value, this
  422. * value will be stored in a container using the resource name as the
  423. * key.
  424. *
  425. * Containers must be objects, and must allow setting public properties.
  426. *
  427. * @param object $container
  428. * @return Zend_Application_Bootstrap_BootstrapAbstract
  429. */
  430. public function setContainer($container)
  431. {
  432. if (!is_object($container)) {
  433. throw new Zend_Application_Bootstrap_Exception('Resource containers must be objects');
  434. }
  435. $this->_container = $container;
  436. return $this;
  437. }
  438. /**
  439. * Retrieve resource container
  440. *
  441. * @return object
  442. */
  443. public function getContainer()
  444. {
  445. if (null === $this->_container) {
  446. $this->setContainer(new Zend_Registry());
  447. }
  448. return $this->_container;
  449. }
  450. /**
  451. * Determine if a resource has been stored in the container
  452. *
  453. * During bootstrap resource initialization, you may return a value. If
  454. * you do, it will be stored in the {@link setContainer() container}.
  455. * You can use this method to determine if a value was stored.
  456. *
  457. * @param string $name
  458. * @return bool
  459. */
  460. public function hasResource($name)
  461. {
  462. $resource = strtolower($name);
  463. $container = $this->getContainer();
  464. return isset($container->{$resource});
  465. }
  466. /**
  467. * Retrieve a resource from the container
  468. *
  469. * During bootstrap resource initialization, you may return a value. If
  470. * you do, it will be stored in the {@link setContainer() container}.
  471. * You can use this method to retrieve that value.
  472. *
  473. * If no value was returned, this will return a null value.
  474. *
  475. * @param string $name
  476. * @return null|mixed
  477. */
  478. public function getResource($name)
  479. {
  480. $resource = strtolower($name);
  481. $container = $this->getContainer();
  482. if ($this->hasResource($resource)) {
  483. return $container->{$resource};
  484. }
  485. return null;
  486. }
  487. /**
  488. * Implement PHP's magic to retrieve a ressource
  489. * in the bootstrap
  490. *
  491. * @param string $prop
  492. * @return null|mixed
  493. */
  494. public function __get($prop)
  495. {
  496. return $this->getResource($prop);
  497. }
  498. /**
  499. * Implement PHP's magic to ask for the
  500. * existence of a ressource in the bootstrap
  501. *
  502. * @param string $prop
  503. * @return bool
  504. */
  505. public function __isset($prop)
  506. {
  507. return $this->hasResource($prop);
  508. }
  509. /**
  510. * Bootstrap individual, all, or multiple resources
  511. *
  512. * Marked as final to prevent issues when subclassing and naming the
  513. * child class 'Bootstrap' (in which case, overriding this method
  514. * would result in it being treated as a constructor).
  515. *
  516. * If you need to override this functionality, override the
  517. * {@link _bootstrap()} method.
  518. *
  519. * @param null|string|array $resource
  520. * @return Zend_Application_Bootstrap_BootstrapAbstract
  521. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  522. */
  523. final public function bootstrap($resource = null)
  524. {
  525. $this->_bootstrap($resource);
  526. return $this;
  527. }
  528. /**
  529. * Overloading: intercept calls to bootstrap<resourcename>() methods
  530. *
  531. * @param string $method
  532. * @param array $args
  533. * @return void
  534. * @throws Zend_Application_Bootstrap_Exception On invalid method name
  535. */
  536. public function __call($method, $args)
  537. {
  538. if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
  539. $resource = substr($method, 9);
  540. return $this->bootstrap($resource);
  541. }
  542. throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
  543. }
  544. /**
  545. * Bootstrap implementation
  546. *
  547. * This method may be overridden to provide custom bootstrapping logic.
  548. * It is the sole method called by {@link bootstrap()}.
  549. *
  550. * @param null|string|array $resource
  551. * @return void
  552. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  553. */
  554. protected function _bootstrap($resource = null)
  555. {
  556. if (null === $resource) {
  557. foreach ($this->getClassResourceNames() as $resource) {
  558. $this->_executeResource($resource);
  559. }
  560. foreach ($this->getPluginResourceNames() as $resource) {
  561. $this->_executeResource($resource);
  562. }
  563. } elseif (is_string($resource)) {
  564. $this->_executeResource($resource);
  565. } elseif (is_array($resource)) {
  566. foreach ($resource as $r) {
  567. $this->_executeResource($r);
  568. }
  569. } else {
  570. throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
  571. }
  572. }
  573. /**
  574. * Execute a resource
  575. *
  576. * Checks to see if the resource has already been run. If not, it searches
  577. * first to see if a local method matches the resource, and executes that.
  578. * If not, it checks to see if a plugin resource matches, and executes that
  579. * if found.
  580. *
  581. * Finally, if not found, it throws an exception.
  582. *
  583. * @param string $resource
  584. * @return void
  585. * @throws Zend_Application_Bootstrap_Exception When resource not found
  586. */
  587. protected function _executeResource($resource)
  588. {
  589. $resourceName = strtolower($resource);
  590. if (in_array($resourceName, $this->_run)) {
  591. return;
  592. }
  593. if (isset($this->_started[$resourceName]) && $this->_started[$resourceName]) {
  594. throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
  595. }
  596. $classResources = $this->getClassResources();
  597. if (array_key_exists($resourceName, $classResources)) {
  598. $this->_started[$resourceName] = true;
  599. $method = $classResources[$resourceName];
  600. $return = $this->$method();
  601. unset($this->_started[$resourceName]);
  602. $this->_markRun($resourceName);
  603. if (null !== $return) {
  604. $this->getContainer()->{$resourceName} = $return;
  605. }
  606. return;
  607. }
  608. if ($this->hasPluginResource($resource)) {
  609. $this->_started[$resourceName] = true;
  610. $plugin = $this->getPluginResource($resource);
  611. $return = $plugin->init();
  612. unset($this->_started[$resourceName]);
  613. $this->_markRun($resourceName);
  614. if (null !== $return) {
  615. $this->getContainer()->{$resourceName} = $return;
  616. }
  617. return;
  618. }
  619. throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
  620. }
  621. /**
  622. * Load a plugin resource
  623. *
  624. * @param string $resource
  625. * @param array|object|null $options
  626. * @return string|false
  627. */
  628. protected function _loadPluginResource($resource, $options)
  629. {
  630. $options = (array) $options;
  631. $options['bootstrap'] = $this;
  632. $className = $this->getPluginLoader()->load(strtolower($resource), false);
  633. if (!$className) {
  634. return false;
  635. }
  636. $instance = new $className($options);
  637. unset($this->_pluginResources[$resource]);
  638. if (isset($instance->_explicitType)) {
  639. $resource = $instance->_explicitType;
  640. }
  641. $resource = strtolower($resource);
  642. $this->_pluginResources[$resource] = $instance;
  643. return $resource;
  644. }
  645. /**
  646. * Mark a resource as having run
  647. *
  648. * @param string $resource
  649. * @return void
  650. */
  651. protected function _markRun($resource)
  652. {
  653. if (!in_array($resource, $this->_run)) {
  654. $this->_run[] = $resource;
  655. }
  656. }
  657. /**
  658. * Resolve a plugin resource name
  659. *
  660. * Uses, in order of preference
  661. * - $_explicitType property of resource
  662. * - Short name of resource (if a matching prefix path is found)
  663. * - class name (if none of the above are true)
  664. *
  665. * The name is then cast to lowercase.
  666. *
  667. * @param Zend_Application_Resource_Resource $resource
  668. * @return string
  669. */
  670. protected function _resolvePluginResourceName($resource)
  671. {
  672. if (isset($resource->_explicitType)) {
  673. $pluginName = $resource->_explicitType;
  674. } else {
  675. $className = get_class($resource);
  676. $pluginName = $className;
  677. $loader = $this->getPluginLoader();
  678. foreach ($loader->getPaths() as $prefix => $paths) {
  679. if (0 === strpos($className, $prefix)) {
  680. $pluginName = substr($className, strlen($prefix));
  681. $pluginName = trim($pluginName, '_');
  682. break;
  683. }
  684. }
  685. }
  686. $pluginName = strtolower($pluginName);
  687. return $pluginName;
  688. }
  689. }