BootstrapAbstract.php 23 KB

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