Frontcontroller.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-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_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-2014 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. {
  97. if(isset($pluginClass['stackindex'])) {
  98. $stackIndex = $pluginClass['stackindex'];
  99. }
  100. $pluginClass = $pluginClass['class'];
  101. }
  102. }
  103. $plugin = new $pluginClass();
  104. $front->registerPlugin($plugin, $stackIndex);
  105. }
  106. break;
  107. case 'returnresponse':
  108. $front->returnResponse((bool) $value);
  109. break;
  110. case 'throwexceptions':
  111. $front->throwExceptions((bool) $value);
  112. break;
  113. case 'actionhelperpaths':
  114. if (is_array($value)) {
  115. foreach ($value as $helperPrefix => $helperPath) {
  116. Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
  117. }
  118. }
  119. break;
  120. case 'dispatcher':
  121. if(!isset($value['class'])) {
  122. require_once 'Zend/Application/Exception.php';
  123. throw new Zend_Application_Exception('You must specify both ');
  124. }
  125. if (!isset($value['params'])) {
  126. $value['params'] = array();
  127. }
  128. $dispatchClass = $value['class'];
  129. if(!class_exists($dispatchClass)) {
  130. require_once 'Zend/Application/Exception.php';
  131. throw new Zend_Application_Exception('Dispatcher class not found!');
  132. }
  133. $front->setDispatcher(new $dispatchClass((array)$value['params']));
  134. break;
  135. default:
  136. $front->setParam($key, $value);
  137. break;
  138. }
  139. }
  140. if (null !== ($bootstrap = $this->getBootstrap())) {
  141. $this->getBootstrap()->frontController = $front;
  142. }
  143. return $front;
  144. }
  145. /**
  146. * Retrieve front controller instance
  147. *
  148. * @return Zend_Controller_Front
  149. */
  150. public function getFrontController()
  151. {
  152. if (null === $this->_front) {
  153. $this->_front = Zend_Controller_Front::getInstance();
  154. }
  155. return $this->_front;
  156. }
  157. }