ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
Public Member Functions | Private Member Functions | Private Attributes
Router Class Reference

Public Member Functions

 __construct ($server, $context)
 
 init ($server)
 
 serveUrl ()
 
 routeAdd ($route, $controller)
 
 overrideUrl ($type, $value)
 
 __get ($key)
 

Private Member Functions

 createController ($className, $params)
 
 routeMatch ($request, $route)
 
 prepareUrl ()
 
 doRouteAdd ($route, $controller)
 

Private Attributes

 $rootPath
 
 $rootUrl
 
 $requestUrl
 
 $routes
 
 $context
 
 $routeParams
 
 $overridden
 

Detailed Description

Will manager new connections to the server and try to match the requests and the controllers

Definition at line 9 of file Router.php.

Constructor & Destructor Documentation

__construct (   $server,
  $context 
)

Create the router, initialize url and path

Definition at line 82 of file Router.php.

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  }

Member Function Documentation

__get (   $key)

Getter

Definition at line 245 of file Router.php.

246  {
247  if (isset($this->overridden) && in_array($key, array("modulesPath")))
248  return $this->overridden[$key];
249  switch ($key)
250  {
251  case "rootPath": return $this->rootPath; break;
252  case "rootUrl": return $this->rootUrl; break;
253  case "modulesPath": return $this->rootPath."content/modules/"; break;
254  case "modulesUrl": return $this->rootUrl."content/modules/"; break;
255  case "themesPath": return $this->rootPath."content/theme/"; break;
256  case "themesUrl": return $this->rootUrl."content/theme/"; break;
257  }
258  throw new \Exception("Cannot access attribute {$key}");
259  }
createController (   $className,
  $params 
)
private

Create and return Controller for the given route

Parameters
$className
array$routeParameters
Returns
on succes, false otherwise

Definition at line 141 of file Router.php.

142  {
143  if (!class_exists($className))
144  return false;
145  $this->routeParams = $params;
146  $result = null;
147  try
148  {
149  $result = new $className($this->context, $params);
150  if (!($result instanceof \Tools\AController))
151  return false;
152  }
153  catch (\Exception\Error404 $e)
154  {
155  return false;
156  }
157  return $result;
158  }
doRouteAdd (   $route,
  $controller 
)
private

Add a route to the internal route list Internal procedure

Definition at line 204 of file Router.php.

205  {
206  $this->routes[] = array($route, $controller);
207  }
init (   $server)

Called after database initialization Check the site url and redirect user if the HOST does not match If the site url is not defined in database, do not redirect

Definition at line 99 of file Router.php.

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  }
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:44
overrideUrl (   $type,
  $value 
)

Override url This SHOULD not be called for security purpose

Parameters
string$typethe url type to override
string$valuethe new value Will fail if called from a controller
Returns
boolean false on failure

Definition at line 234 of file Router.php.

235  {
236  if (!$this->context->isTestingEnvironment())
237  return false;
238  $this->overridden[$type] = $value;
239  return true;
240  }
prepareUrl ( )
private

Append local routes to router Will load CMS pages, categories page, products page, cart pages, etc.

Definition at line 192 of file Router.php.

193  {
194  $fetcher = new \Entity\Cms();
195  $pages = $fetcher->selects(null, array("order"));
196  foreach ($pages as $i)
197  $this->doRouteAdd($i->shurl, $i->controller);
198  }
doRouteAdd($route, $controller)
Definition: Router.php:204
routeAdd (   $route,
  $controller 
)
Parameters
string$routeUri to match the controller Uri can be formatted as '/:param/static'. expl. '/product/:id/'
string$controllerController class name. new $controller() MUST return a instance

Add a route and a Controller to the list Can only be called from `routerSetup' hook

Definition at line 219 of file Router.php.

220  {
221  if (!$this->context->hookManager->isInHook("routerSetup"))
222  throw new \Exception("You can only add routes from `routerSetup' hook");
223  $this->doRouteAdd($route, $controller);
224  }
doRouteAdd($route, $controller)
Definition: Router.php:204
routeMatch (   $request,
  $route 
)
private

Check if the request match route

Parameters
array$requestUser request
array$routeRoute to check
Returns
array on success, false on failure

Definition at line 166 of file Router.php.

167  {
168  $i = count($request);
169  $params = array();
170  if ($i != count($route))
171  return false;
172  while ($i)
173  {
174  $i--;
175  if ($route[$i] == '' && $request[$i] == '')
176  continue;
177  if ($route[$i] == '' || $request[$i] == '')
178  return false;
179  if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
180  return false;
181  if ($route[$i][0] == ':')
182  $params[$route[$i]] = $request[$i];
183  $params[$i -1] = $request[$i];
184  }
185  return array_reverse($params);
186  }
serveUrl ( )
Returns
controller /core/controller/AController.php Match request to a controller return FALSE on failure (eg. 404)

Definition at line 118 of file Router.php.

119  {
120  $this->prepareUrl();
121  $requestParams = explode("/", $this->requestUrl);
122  foreach ($this->routes as $i)
123  {
124  $routeParams = explode("/", $i[0]);
125  $p = $this->routeMatch($requestParams, $routeParams);
126  if ($p === false)
127  continue;
128  $controller = $this->createController($i[1], $p);
129  if ($controller)
130  return $controller;
131  }
132  return false;
133  }
createController($className, $params)
Definition: Router.php:141
routeMatch($request, $route)
Definition: Router.php:166

Field Documentation

Tools Context $context
private

/core/tools/Context.php Contains website's informations

Definition at line 41 of file Router.php.

array $overridden
private

contains url and paths to rewrite

Definition at line 57 of file Router.php.

string $requestUrl
private

Contains request

Definition at line 29 of file Router.php.

string $rootPath
private

Contains the application's root path (ex: http://myshop.com/) with trailing slash Can be accessed read-only via $instance->rootPath

Contains the application's root path (ex: /srv/http/myshop/) with trailing slash Can be accessed read-only via $instance->rootUrl

Definition at line 16 of file Router.php.

$rootUrl
private

Definition at line 23 of file Router.php.

$routeParams
private

Definition at line 51 of file Router.php.

$routes
private

Definition at line 34 of file Router.php.


The documentation for this class was generated from the following file: