ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
Router.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Tools;
4 
9 class Router
10 {
16  private $rootPath;
17 
23  private $rootUrl;
24 
29  private $requestUrl;
30 
34  private $routes;
35 
41  private $context;
42 
51  private $routeParams;
52 
57  private $overridden;
58 
82  public function __construct($server, $context)
83  {
84  $pos = strrpos($server["SCRIPT_NAME"], "/");
85  $relativePath = (($pos === FALSE) ? "" : substr($server["SCRIPT_NAME"], 0, $pos));
86  $this->rootPath = $server["DOCUMENT_ROOT"] . $relativePath . "/";
87  $this->rootUrl = $server["REQUEST_SCHEME"] . "://" . $server["HTTP_HOST"] . $relativePath ."/";
88  $this->requestUrl = substr($server["REQUEST_URI"], count($this->rootUrl) -1);
89  $this->context = $context;
90  $this->routes = array();
91  $this->overridden = null;
92  }
93 
99  public function init($server)
100  {
101  $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
102  // @codeCoverageIgnoreStart
103  // This code is tested under another process
104  if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
105  {
106  header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
107  die;
108  }
109  // @codeCoverageIgnoreEnd
110  }
111 
118  public function serveUrl()
119  {
120  //TODO trigger hook GET, POST
121  $this->prepareUrl();
122  $requestParams = explode("/", $this->requestUrl);
123  foreach ($this->routes as $i)
124  {
125  $routeParams = explode("/", $i[0]);
126  $p = $this->routeMatch($requestParams, $routeParams);
127  if ($p === false)
128  continue;
129  $controller = $this->createController($i[1], $p);
130  if ($controller)
131  return $controller;
132  }
133  return false;
134  }
135 
142  private function createController($className, $params)
143  {
144  if (!class_exists($className))
145  return false;
146  $this->routeParams = $params;
147  $result = null;
148  try
149  {
150  $result = new $className($this->context, $params);
151  if (!($result instanceof \Tools\AController))
152  return false;
153  }
154  catch (\Exception\Error404 $e)
155  {
156  return false;
157  }
158  return $result;
159  }
160 
167  private function routeMatch($request, $route)
168  {
169  $i = count($request);
170  $params = array();
171  if ($i != count($route))
172  return false;
173  while ($i)
174  {
175  $i--;
176  if ($route[$i] == '' && $request[$i] == '')
177  continue;
178  if ($route[$i] == '' || $request[$i] == '')
179  return false;
180  if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
181  return false;
182  if ($route[$i][0] == ':')
183  $params[$route[$i]] = $request[$i];
184  $params[$i -1] = $request[$i];
185  }
186  return array_reverse($params);
187  }
188 
193  private function prepareUrl()
194  {
195  $fetcher = new \Entity\Cms();
196  $pages = $fetcher->selects(null, array("order"));
197  foreach ($pages as $i)
198  $this->doRouteAdd($i->shurl, $i->controller);
199  }
200 
205  private function doRouteAdd($route, $controller)
206  {
207  $this->routes[] = array($route, $controller);
208  }
209 
220  public function routeAdd($route, $controller)
221  {
222  if (!$this->context->hookManager->isInHook("routerSetup"))
223  throw new \Exception("You can only add routes from `routerSetup' hook");
224  $this->doRouteAdd($route, $controller);
225  }
226 
235  public function overrideUrl($type, $value)
236  {
237  if (!$this->context->isTestingEnvironment())
238  return false;
239  $this->overridden[$type] = $value;
240  return true;
241  }
242 
246  public function __get($key)
247  {
248  if (isset($this->overridden) && in_array($key, array("modulesPath")))
249  return $this->overridden[$key];
250  switch ($key)
251  {
252  case "rootPath": return $this->rootPath; break;
253  case "rootUrl": return $this->rootUrl; break;
254  case "modulesPath": return $this->rootPath."content/modules/"; break;
255  case "modulesUrl": return $this->rootUrl."content/modules/"; break;
256  case "themesPath": return $this->rootPath."content/theme/"; break;
257  case "themesUrl": return $this->rootUrl."content/theme/"; break;
258  }
259  throw new \Exception("Cannot access attribute {$key}");
260  }
261 }
262 
doRouteAdd($route, $controller)
Definition: Router.php:205
__get($key)
Definition: Router.php:246
overrideUrl($type, $value)
Definition: Router.php:235
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:43
__construct($server, $context)
Definition: Router.php:82
createController($className, $params)
Definition: Router.php:142
init($server)
Definition: Router.php:99
routeAdd($route, $controller)
Definition: Router.php:220
routeMatch($request, $route)
Definition: Router.php:167