Bootstrap.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-2012 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. * Concrete base class for bootstrap classes
  24. *
  25. * Registers and utilizes Zend_Controller_Front by default.
  26. *
  27. * @uses Zend_Application_Bootstrap_Bootstrap
  28. * @category Zend
  29. * @package Zend_Application
  30. * @subpackage Bootstrap
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Application_Bootstrap_Bootstrap
  35. extends Zend_Application_Bootstrap_BootstrapAbstract
  36. {
  37. /**
  38. * Application resource namespace
  39. * @var false|string
  40. */
  41. protected $_appNamespace = false;
  42. /**
  43. * Application resource autoloader
  44. * @var Zend_Loader_Autoloader_Resource
  45. */
  46. protected $_resourceLoader;
  47. /**
  48. * Constructor
  49. *
  50. * Ensure FrontController resource is registered
  51. *
  52. * @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
  53. */
  54. public function __construct($application)
  55. {
  56. parent::__construct($application);
  57. if ($application->hasOption('resourceloader')) {
  58. $this->setOptions(array(
  59. 'resourceloader' => $application->getOption('resourceloader')
  60. ));
  61. }
  62. $this->getResourceLoader();
  63. if (!$this->hasPluginResource('FrontController')) {
  64. $this->registerPluginResource('FrontController');
  65. }
  66. }
  67. /**
  68. * Run the application
  69. *
  70. * Checks to see that we have a default controller directory. If not, an
  71. * exception is thrown.
  72. *
  73. * If so, it registers the bootstrap with the 'bootstrap' parameter of
  74. * the front controller, and dispatches the front controller.
  75. *
  76. * @return mixed
  77. * @throws Zend_Application_Bootstrap_Exception
  78. */
  79. public function run()
  80. {
  81. $front = $this->getResource('FrontController');
  82. $default = $front->getDefaultModule();
  83. if (null === $front->getControllerDirectory($default)) {
  84. throw new Zend_Application_Bootstrap_Exception(
  85. 'No default controller directory registered with front controller'
  86. );
  87. }
  88. $front->setParam('bootstrap', $this);
  89. $response = $front->dispatch();
  90. if ($front->returnResponse()) {
  91. return $response;
  92. }
  93. }
  94. /**
  95. * Set module resource loader
  96. *
  97. * @param Zend_Loader_Autoloader_Resource $loader
  98. * @return Zend_Application_Module_Bootstrap
  99. */
  100. public function setResourceLoader(Zend_Loader_Autoloader_Resource $loader)
  101. {
  102. $this->_resourceLoader = $loader;
  103. return $this;
  104. }
  105. /**
  106. * Retrieve module resource loader
  107. *
  108. * @return Zend_Loader_Autoloader_Resource
  109. */
  110. public function getResourceLoader()
  111. {
  112. if ((null === $this->_resourceLoader)
  113. && (false !== ($namespace = $this->getAppNamespace()))
  114. ) {
  115. $r = new ReflectionClass($this);
  116. $path = $r->getFileName();
  117. $this->setResourceLoader(new Zend_Application_Module_Autoloader(array(
  118. 'namespace' => $namespace,
  119. 'basePath' => dirname($path),
  120. )));
  121. }
  122. return $this->_resourceLoader;
  123. }
  124. /**
  125. * Get application namespace (used for module autoloading)
  126. *
  127. * @return string
  128. */
  129. public function getAppNamespace()
  130. {
  131. return $this->_appNamespace;
  132. }
  133. /**
  134. * Set application namespace (for module autoloading)
  135. *
  136. * @param string
  137. * @return Zend_Application_Bootstrap_Bootstrap
  138. */
  139. public function setAppNamespace($value)
  140. {
  141. $this->_appNamespace = (string) $value;
  142. return $this;
  143. }
  144. }