BootstrapAbstract.php 21 KB

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