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 
76  public function __construct($server, $context)
77  {
78  $pos = strrpos($server["SCRIPT_NAME"], "/");
79  $relativePath = (($pos === FALSE) ? "" : substr($server["SCRIPT_NAME"], 0, $pos));
80  $this->rootPath = $server["DOCUMENT_ROOT"] . $relativePath . "/";
81  $this->rootUrl = $server["REQUEST_SCHEME"] . "://" . $server["HTTP_HOST"] . $relativePath ."/";
82  $this->requestUrl = substr($server["REQUEST_URI"], count($this->rootUrl) -1);
83  $this->context = $context;
84  $this->routes = array();
85  }
86 
92  public function init($server)
93  {
94  $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
95  if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
96  {
97  header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
98  die;
99  }
100  }
101 
108  public function serveUrl()
109  {
110  //TODO trigger hook GET, POST
111  $this->prepareUrl();
112  $requestParams = explode("/", $this->requestUrl);
113  foreach ($this->routes as $i)
114  {
115  $routeParams = explode("/", $i[0]);
116  $p = $this->routeMatch($requestParams, $routeParams);
117  if (!$p)
118  continue;
119  $controller = $this->createController($i[1], $p);
120  if ($controller)
121  return $controller;
122  }
123  return false;
124  }
125 
132  private function createController($className, $params)
133  {
134  if (!class_exists($className))
135  return false;
136  try
137  {
138  $this->routeParams = $params;
139  $result = new $className($this->context, $params);
140  }
141  catch (\Exception\Error404 $e)
142  {
143  return false;
144  }
145  if (!($result instanceof \Tools\AController))
146  return false;
147  return $result;
148  }
149 
156  private function routeMatch($request, $route)
157  {
158  $i = count($request);
159  $params = array();
160  if ($i != count($route))
161  return false;
162  while ($i)
163  {
164  $i--;
165  if ($route[$i] == '' && $request[$i] == '')
166  continue;
167  if ($route[$i] == '' || $request[$i] == '')
168  return false;
169  if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
170  return false;
171  if ($route[$i][0] == ':')
172  $params[$route[$i]] = $request[$i];
173  $params[$i -1] = $request[$i];
174  }
175  return array_reverse($params);
176  }
177 
182  private function prepareUrl()
183  {
184  $fetcher = new \Entity\Cms();
185  $pages = $fetcher->selects(null, array("order"));
186  foreach ($pages as $i)
187  $this->doRouteAdd($i->shurl, $i->controller);
188  }
189 
194  private function doRouteAdd($route, $controller)
195  {
196  $this->routes[] = array($route, $controller);
197  }
198 
209  public function routeAdd($route, $controller)
210  {
211  if (!$this->context->hookManager->isInHook("routerSetup"))
212  throw new \Exception("You can only add routes from `routerSetup' hook");
213  $this->doRouteAdd($route, $controller);
214  }
215 
219  public function __get($key)
220  {
221  switch ($key)
222  {
223  case "rootPath": return $this->rootPath; break;
224  case "rootUrl": return $this->rootUrl; break;
225  case "modulesPath": return $this->rootPath."content/modules/"; break;
226  case "modulesUrl": return $this->rootUrl."content/modules/"; break;
227  case "themesPath": return $this->rootPath."content/theme/"; break;
228  case "themesUrl": return $this->rootUrl."content/theme/"; break;
229  }
230  throw new \Exception("Cannot access attribute {$key}");
231  }
232 }
233 
doRouteAdd($route, $controller)
Definition: Router.php:194
__get($key)
Definition: Router.php:219
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:23
__construct($server, $context)
Definition: Router.php:76
createController($className, $params)
Definition: Router.php:132
init($server)
Definition: Router.php:92
routeAdd($route, $controller)
Definition: Router.php:209
routeMatch($request, $route)
Definition: Router.php:156