Frontcontroller.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2010 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-2010 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. */
  46. public function init()
  47. {
  48. $front = $this->getFrontController();
  49. foreach ($this->getOptions() as $key => $value) {
  50. switch (strtolower($key)) {
  51. case 'controllerdirectory':
  52. if (is_string($value)) {
  53. $front->setControllerDirectory($value);
  54. } elseif (is_array($value)) {
  55. foreach ($value as $module => $directory) {
  56. $front->addControllerDirectory($directory, $module);
  57. }
  58. }
  59. break;
  60. case 'modulecontrollerdirectoryname':
  61. $front->setModuleControllerDirectoryName($value);
  62. break;
  63. case 'moduledirectory':
  64. $front->addModuleDirectory($value);
  65. break;
  66. case 'defaultcontrollername':
  67. $front->setDefaultControllerName($value);
  68. break;
  69. case 'defaultaction':
  70. $front->setDefaultAction($value);
  71. break;
  72. case 'defaultmodule':
  73. $front->setDefaultModule($value);
  74. break;
  75. case 'baseurl':
  76. if (!empty($value)) {
  77. $front->setBaseUrl($value);
  78. }
  79. break;
  80. case 'params':
  81. $front->setParams($value);
  82. break;
  83. case 'plugins':
  84. foreach ((array) $value as $pluginClass) {
  85. $stackIndex = null;
  86. if(is_array($pluginClass)) {
  87. $pluginClass = array_change_key_case($pluginClass, CASE_LOWER);
  88. if(isset($pluginClass['class']))
  89. {
  90. if(isset($pluginClass['stackindex'])) {
  91. $stackIndex = $pluginClass['stackindex'];
  92. }
  93. $pluginClass = $pluginClass['class'];
  94. }
  95. }
  96. $plugin = new $pluginClass();
  97. $front->registerPlugin($plugin, $stackIndex);
  98. }
  99. break;
  100. case 'returnresponse':
  101. $front->returnResponse((bool) $value);
  102. break;
  103. case 'throwexceptions':
  104. $front->throwExceptions((bool) $value);
  105. break;
  106. case 'actionhelperpaths':
  107. if (is_array($value)) {
  108. foreach ($value as $helperPrefix => $helperPath) {
  109. Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
  110. }
  111. }
  112. break;
  113. default:
  114. $front->setParam($key, $value);
  115. break;
  116. }
  117. }
  118. if (null !== ($bootstrap = $this->getBootstrap())) {
  119. $this->getBootstrap()->frontController = $front;
  120. }
  121. return $front;
  122. }
  123. /**
  124. * Retrieve front controller instance
  125. *
  126. * @return Zend_Controller_Front
  127. */
  128. public function getFrontController()
  129. {
  130. if (null === $this->_front) {
  131. $this->_front = Zend_Controller_Front::getInstance();
  132. }
  133. return $this->_front;
  134. }
  135. }