BootstrapAbstract.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 = $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)) {
  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. * Bootstrap individual, all, or multiple resources
  485. *
  486. * Marked as final to prevent issues when subclassing and naming the
  487. * child class 'Bootstrap' (in which case, overriding this method
  488. * would result in it being treated as a constructor).
  489. *
  490. * If you need to override this functionality, override the
  491. * {@link _bootstrap()} method.
  492. *
  493. * @param null|string|array $resource
  494. * @return Zend_Application_Bootstrap_BootstrapAbstract
  495. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  496. */
  497. final public function bootstrap($resource = null)
  498. {
  499. $this->_bootstrap($resource);
  500. return $this;
  501. }
  502. /**
  503. * Overloading: intercept calls to bootstrap<resourcename>() methods
  504. *
  505. * @param string $method
  506. * @param array $args
  507. * @return void
  508. * @throws Zend_Application_Bootstrap_Exception On invalid method name
  509. */
  510. public function __call($method, $args)
  511. {
  512. if (9 < strlen($method) && 'bootstrap' === substr($method, 0, 9)) {
  513. $resource = substr($method, 9);
  514. return $this->bootstrap($resource);
  515. }
  516. throw new Zend_Application_Bootstrap_Exception('Invalid method "' . $method . '"');
  517. }
  518. /**
  519. * Bootstrap implementation
  520. *
  521. * This method may be overridden to provide custom bootstrapping logic.
  522. * It is the sole method called by {@link bootstrap()}.
  523. *
  524. * @param null|string|array $resource
  525. * @return void
  526. * @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
  527. */
  528. protected function _bootstrap($resource = null)
  529. {
  530. if (null === $resource) {
  531. foreach ($this->getClassResourceNames() as $resource) {
  532. $this->_executeResource($resource);
  533. }
  534. foreach ($this->getPluginResourceNames() as $resource) {
  535. $this->_executeResource($resource);
  536. }
  537. } elseif (is_string($resource)) {
  538. $this->_executeResource($resource);
  539. } elseif (is_array($resource)) {
  540. foreach ($resource as $r) {
  541. $this->_executeResource($r);
  542. }
  543. } else {
  544. throw new Zend_Application_Bootstrap_Exception('Invalid argument passed to ' . __METHOD__);
  545. }
  546. }
  547. /**
  548. * Execute a resource
  549. *
  550. * Checks to see if the resource has already been run. If not, it searches
  551. * first to see if a local method matches the resource, and executes that.
  552. * If not, it checks to see if a plugin resource matches, and executes that
  553. * if found.
  554. *
  555. * Finally, if not found, it throws an exception.
  556. *
  557. * @param string $resource
  558. * @return void
  559. * @throws Zend_Application_Bootstrap_Exception When resource not found
  560. */
  561. protected function _executeResource($resource)
  562. {
  563. $resource = strtolower($resource);
  564. if (in_array($resource, $this->_run)) {
  565. return;
  566. }
  567. if (isset($this->_started[$resource]) && $this->_started[$resource]) {
  568. throw new Zend_Application_Bootstrap_Exception('Circular resource dependency detected');
  569. }
  570. $classResources = $this->getClassResources();
  571. if (array_key_exists($resource, $classResources)) {
  572. $this->_started[$resource] = true;
  573. $method = $classResources[$resource];
  574. $return = $this->$method();
  575. unset($this->_started[$resource]);
  576. $this->_markRun($resource);
  577. if (null !== $return) {
  578. $this->getContainer()->{$resource} = $return;
  579. }
  580. return;
  581. }
  582. if ($this->hasPluginResource($resource)) {
  583. $this->_started[$resource] = true;
  584. $plugin = $this->getPluginResource($resource);
  585. $return = $plugin->init();
  586. unset($this->_started[$resource]);
  587. $this->_markRun($resource);
  588. if (null !== $return) {
  589. $this->getContainer()->{$resource} = $return;
  590. }
  591. return;
  592. }
  593. throw new Zend_Application_Bootstrap_Exception('Resource matching "' . $resource . '" not found');
  594. }
  595. /**
  596. * Load a plugin resource
  597. *
  598. * @param string $resource
  599. * @param array|object|null $options
  600. * @return string|false
  601. */
  602. protected function _loadPluginResource($resource, $options)
  603. {
  604. $options = (array) $options;
  605. $options['bootstrap'] = $this;
  606. $className = $this->getPluginLoader()->load(strtolower($resource), false);
  607. if (!$className) {
  608. return false;
  609. }
  610. $instance = new $className($options);
  611. unset($this->_pluginResources[$resource]);
  612. if (isset($instance->_explicitType)) {
  613. $resource = $instance->_explicitType;
  614. }
  615. $resource = strtolower($resource);
  616. $this->_pluginResources[$resource] = $instance;
  617. return $resource;
  618. }
  619. /**
  620. * Mark a resource as having run
  621. *
  622. * @param string $resource
  623. * @return void
  624. */
  625. protected function _markRun($resource)
  626. {
  627. if (!in_array($resource, $this->_run)) {
  628. $this->_run[] = $resource;
  629. }
  630. }
  631. /**
  632. * Resolve a plugin resource name
  633. *
  634. * Uses, in order of preference
  635. * - $_explicitType property of resource
  636. * - Short name of resource (if a matching prefix path is found)
  637. * - class name (if none of the above are true)
  638. *
  639. * The name is then cast to lowercase.
  640. *
  641. * @param Zend_Application_Resource_Resource $resource
  642. * @return string
  643. */
  644. protected function _resolvePluginResourceName($resource)
  645. {
  646. if (isset($resource->_explicitType)) {
  647. $pluginName = $resource->_explicitType;
  648. } else {
  649. $className = get_class($resource);
  650. $pluginName = $className;
  651. $loader = $this->getPluginLoader();
  652. foreach ($loader->getPaths() as $prefix => $paths) {
  653. if (0 === strpos($className, $prefix)) {
  654. $pluginName = substr($className, strlen($prefix));
  655. $pluginName = trim($pluginName, '_');
  656. }
  657. }
  658. }
  659. $pluginName = strtolower($pluginName);
  660. return $pluginName;
  661. }
  662. }