BootstrapAbstract.php 22 KB

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