00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Core_Controller_Varien_Front extends Varien_Object
00029 {
00030 protected $_defaults = array();
00031
00032
00033
00034
00035
00036
00037 protected $_routers = array();
00038
00039 protected $_urlCache = array();
00040
00041 const XML_STORE_ROUTERS_PATH = 'web/routers';
00042
00043 public function setDefault($key, $value=null)
00044 {
00045 if (is_array($key)) {
00046 $this->_defaults = $key;
00047 } else {
00048 $this->_defaults[$key] = $value;
00049 }
00050 return $this;
00051 }
00052
00053 public function getDefault($key=null)
00054 {
00055 if (is_null($key)) {
00056 return $this->_defaults;
00057 } elseif (isset($this->_defaults[$key])) {
00058 return $this->_defaults[$key];
00059 }
00060 return false;
00061 }
00062
00063
00064
00065
00066
00067
00068 public function getRequest()
00069 {
00070 return Mage::app()->getRequest();
00071 }
00072
00073
00074
00075
00076
00077
00078 public function getResponse()
00079 {
00080 return Mage::app()->getResponse();
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 public function addRouter($name, Mage_Core_Controller_Varien_Router_Abstract $router)
00091 {
00092 $router->setFront($this);
00093 $this->_routers[$name] = $router;
00094 return $this;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 public function getRouter($name)
00104 {
00105 if (isset($this->_routers[$name])) {
00106 return $this->_routers[$name];
00107 }
00108 return false;
00109 }
00110
00111 public function init()
00112 {
00113 Mage::dispatchEvent('controller_front_init_before', array('front'=>$this));
00114
00115 $routersInfo = Mage::app()->getStore()->getConfig(self::XML_STORE_ROUTERS_PATH);
00116
00117 Varien_Profiler::start('mage::app::init_front_controller::collect_routers');
00118 foreach ($routersInfo as $routerCode => $routerInfo) {
00119 if (isset($routerInfo['disabled']) && $routerInfo['disabled']) {
00120 continue;
00121 }
00122 if (isset($routerInfo['class'])) {
00123 $router = new $routerInfo['class'];
00124 if (isset($routerInfo['area'])) {
00125 $router->collectRoutes($routerInfo['area'], $routerCode);
00126 }
00127 $this->addRouter($routerCode, $router);
00128 }
00129 }
00130 Varien_Profiler::stop('mage::app::init_front_controller::collect_routers');
00131
00132 Mage::dispatchEvent('controller_front_init_routers', array('front'=>$this));
00133
00134
00135 $default = new Mage_Core_Controller_Varien_Router_Default();
00136 $this->addRouter('default', $default);
00137
00138 return $this;
00139 }
00140
00141 public function dispatch()
00142 {
00143 $request = $this->getRequest();
00144 $request->setPathInfo()->setDispatched(false);
00145
00146 Varien_Profiler::start('mage::dispatch::db_url_rewrite');
00147 Mage::getModel('core/url_rewrite')->rewrite();
00148 Varien_Profiler::stop('mage::dispatch::db_url_rewrite');
00149
00150 Varien_Profiler::start('mage::dispatch::config_url_rewrite');
00151 $this->rewrite();
00152 Varien_Profiler::stop('mage::dispatch::config_url_rewrite');
00153
00154 Varien_Profiler::start('mage::dispatch::routers_match');
00155 $i = 0;
00156 while (!$request->isDispatched() && $i++<100) {
00157 foreach ($this->_routers as $router) {
00158 if ($router->match($this->getRequest())) {
00159 break;
00160 }
00161 }
00162 }
00163 Varien_Profiler::stop('mage::dispatch::routers_match');
00164 if ($i>100) {
00165 Mage::throwException('Front controller reached 100 router match iterations');
00166 }
00167
00168 Varien_Profiler::start('mage::app::dispatch::send_response');
00169 $this->getResponse()->sendResponse();
00170 Varien_Profiler::stop('mage::app::dispatch::send_response');
00171
00172 return $this;
00173 }
00174
00175 public function getRouterByRoute($routeName)
00176 {
00177
00178 if (empty($routeName)) {
00179 $router = $this->getRouter('standard');
00180 } elseif ($this->getRouter('admin')->getFrontNameByRoute($routeName)) {
00181
00182 $router = $this->getRouter('admin');
00183 } elseif ($this->getRouter('standard')->getFrontNameByRoute($routeName)) {
00184
00185 $router = $this->getRouter('standard');
00186 } elseif ($router = $this->getRouter($routeName)) {
00187
00188 } else {
00189
00190 $router = $this->getRouter('default');
00191 }
00192
00193 return $router;
00194 }
00195
00196 public function getRouterByFrontName($frontName)
00197 {
00198
00199 if (empty($frontName)) {
00200 $router = $this->getRouter('standard');
00201 } elseif ($this->getRouter('admin')->getRouteByFrontName($frontName)) {
00202
00203 $router = $this->getRouter('admin');
00204 } elseif ($this->getRouter('standard')->getRouteByFrontName($frontName)) {
00205
00206 $router = $this->getRouter('standard');
00207 } elseif ($router = $this->getRouter($frontName)) {
00208
00209 } else {
00210
00211 $router = $this->getRouter('default');
00212 }
00213
00214 return $router;
00215 }
00216
00217 public function rewrite()
00218 {
00219 $request = $this->getRequest();
00220 $config = Mage::getConfig()->getNode('global/rewrite');
00221 if (!$config) {
00222 return;
00223 }
00224 foreach ($config->children() as $rewrite) {
00225 $from = (string)$rewrite->from;
00226 $to = (string)$rewrite->to;
00227 if (empty($from) || empty($to)) {
00228 continue;
00229 }
00230 $pathInfo = preg_replace($from, $to, $request->getPathInfo());
00231 $request->setPathInfo($pathInfo);
00232 }
00233 }
00234 }