Bootstrap.php 4.5 KB

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