Frontcontroller.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Resource
  18. * @copyright Copyright (c) 2005-2015 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_Resource_ResourceAbstract
  24. */
  25. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  26. /**
  27. * Front Controller resource
  28. *
  29. * @category Zend
  30. * @package Zend_Application
  31. * @subpackage Resource
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Application_Resource_Frontcontroller extends Zend_Application_Resource_ResourceAbstract
  36. {
  37. /**
  38. * @var Zend_Controller_Front
  39. */
  40. protected $_front;
  41. /**
  42. * Initialize Front Controller
  43. *
  44. * @return Zend_Controller_Front
  45. * @throws Zend_Application_Exception
  46. */
  47. public function init()
  48. {
  49. $front = $this->getFrontController();
  50. foreach ($this->getOptions() as $key => $value) {
  51. switch (strtolower($key)) {
  52. case 'controllerdirectory':
  53. if (is_string($value)) {
  54. $front->setControllerDirectory($value);
  55. } elseif (is_array($value)) {
  56. foreach ($value as $module => $directory) {
  57. $front->addControllerDirectory($directory, $module);
  58. }
  59. }
  60. break;
  61. case 'modulecontrollerdirectoryname':
  62. $front->setModuleControllerDirectoryName($value);
  63. break;
  64. case 'moduledirectory':
  65. if (is_string($value)) {
  66. $front->addModuleDirectory($value);
  67. } elseif (is_array($value)) {
  68. foreach ($value as $moduleDir) {
  69. $front->addModuleDirectory($moduleDir);
  70. }
  71. }
  72. break;
  73. case 'defaultcontrollername':
  74. $front->setDefaultControllerName($value);
  75. break;
  76. case 'defaultaction':
  77. $front->setDefaultAction($value);
  78. break;
  79. case 'defaultmodule':
  80. $front->setDefaultModule($value);
  81. break;
  82. case 'baseurl':
  83. if (!empty($value)) {
  84. $front->setBaseUrl($value);
  85. }
  86. break;
  87. case 'params':
  88. $front->setParams($value);
  89. break;
  90. case 'plugins':
  91. foreach ((array) $value as $pluginClass) {
  92. $stackIndex = null;
  93. if (is_array($pluginClass)) {
  94. $pluginClass = array_change_key_case($pluginClass, CASE_LOWER);
  95. if (isset($pluginClass['class'])) {
  96. if (isset($pluginClass['stackindex'])) {
  97. $stackIndex = $pluginClass['stackindex'];
  98. }
  99. $pluginClass = $pluginClass['class'];
  100. }
  101. }
  102. $plugin = new $pluginClass();
  103. $front->registerPlugin($plugin, $stackIndex);
  104. }
  105. break;
  106. case 'returnresponse':
  107. $front->returnResponse((bool) $value);
  108. break;
  109. case 'throwexceptions':
  110. $front->throwExceptions((bool) $value);
  111. break;
  112. case 'actionhelperpaths':
  113. if (is_array($value)) {
  114. foreach ($value as $helperPrefix => $helperPath) {
  115. Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
  116. }
  117. }
  118. break;
  119. case 'dispatcher':
  120. if (!isset($value['class'])) {
  121. require_once 'Zend/Application/Exception.php';
  122. throw new Zend_Application_Exception('You must specify both ');
  123. }
  124. if (!isset($value['params'])) {
  125. $value['params'] = array();
  126. }
  127. $dispatchClass = $value['class'];
  128. if (!class_exists($dispatchClass)) {
  129. require_once 'Zend/Application/Exception.php';
  130. throw new Zend_Application_Exception('Dispatcher class not found!');
  131. }
  132. $front->setDispatcher(new $dispatchClass((array)$value['params']));
  133. break;
  134. default:
  135. $front->setParam($key, $value);
  136. break;
  137. }
  138. }
  139. if (null !== ($bootstrap = $this->getBootstrap())) {
  140. $this->getBootstrap()->frontController = $front;
  141. }
  142. return $front;
  143. }
  144. /**
  145. * Retrieve front controller instance
  146. *
  147. * @return Zend_Controller_Front
  148. */
  149. public function getFrontController()
  150. {
  151. if (null === $this->_front) {
  152. $this->_front = Zend_Controller_Front::getInstance();
  153. }
  154. return $this->_front;
  155. }
  156. }