Bootstrap.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Module
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Application_Bootstrap_Bootstrap
  24. */
  25. require_once 'Zend/Application/Bootstrap/Bootstrap.php';
  26. /**
  27. * Base bootstrap class for modules
  28. *
  29. * @uses Zend_Loader_Autoloader_Resource
  30. * @uses Zend_Application_Bootstrap_Bootstrap
  31. * @package Zend_Application
  32. * @subpackage Module
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Application_Module_Bootstrap
  37. extends Zend_Application_Bootstrap_Bootstrap
  38. {
  39. /**
  40. * @var Zend_Loader_Autoloader_Resource
  41. */
  42. protected $_resourceLoader;
  43. /**
  44. * Constructor
  45. *
  46. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  47. * @return void
  48. */
  49. public function __construct($application)
  50. {
  51. $this->setApplication($application);
  52. // Use same plugin loader as parent bootstrap
  53. if ($application instanceof Zend_Application_Bootstrap_ResourceBootstrapper) {
  54. $this->setPluginLoader($application->getPluginLoader());
  55. }
  56. $key = strtolower($this->getModuleName());
  57. if ($application->hasOption($key)) {
  58. // Don't run via setOptions() to prevent duplicate initialization
  59. $this->setOptions($application->getOption($key));
  60. }
  61. if ($application->hasOption('resourceloader')) {
  62. $this->setOptions(array(
  63. 'resourceloader' => $application->getOption('resourceloader')
  64. ));
  65. }
  66. $this->initResourceLoader();
  67. // ZF-6545: ensure front controller resource is loaded
  68. if (!$this->hasPluginResource('FrontController')) {
  69. $this->registerPluginResource('FrontController');
  70. }
  71. // ZF-6545: prevent recursive registration of modules
  72. if ($this->hasPluginResource('Modules')) {
  73. $this->unregisterPluginResource('Modules');
  74. }
  75. }
  76. /**
  77. * Set module resource loader
  78. *
  79. * @param Zend_Loader_Autoloader_Resource $loader
  80. * @return Zend_Application_Module_Bootstrap
  81. */
  82. public function setResourceLoader(Zend_Loader_Autoloader_Resource $loader)
  83. {
  84. $this->_resourceLoader = $loader;
  85. return $this;
  86. }
  87. /**
  88. * Retrieve module resource loader
  89. *
  90. * @return Zend_Loader_Autoloader_Resource
  91. */
  92. public function getResourceLoader()
  93. {
  94. if (null === $this->_resourceLoader) {
  95. $r = new ReflectionClass($this);
  96. $path = $r->getFileName();
  97. $this->setResourceLoader(new Zend_Application_Module_Autoloader(array(
  98. 'namespace' => $this->getModuleName(),
  99. 'basePath' => dirname($path),
  100. )));
  101. }
  102. return $this->_resourceLoader;
  103. }
  104. /**
  105. * Ensure resource loader is loaded
  106. *
  107. * @return void
  108. */
  109. public function initResourceLoader()
  110. {
  111. $this->getResourceLoader();
  112. }
  113. /**
  114. * Retrieve module name
  115. *
  116. * @return string
  117. */
  118. public function getModuleName()
  119. {
  120. if (empty($this->_moduleName)) {
  121. $class = get_class($this);
  122. if (preg_match('/^([a-z][a-z0-9]*)_/i', $class, $matches)) {
  123. $prefix = $matches[1];
  124. } else {
  125. $prefix = $class;
  126. }
  127. $this->_moduleName = $prefix;
  128. }
  129. return $this->_moduleName;
  130. }
  131. }