2
0

BootstrapAbstract.php 22 KB

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