|
|
@@ -22,31 +22,67 @@ class Router
|
|
|
**/
|
|
|
private $rootUrl;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var string $requestUrl
|
|
|
+ * Contains request
|
|
|
+ **/
|
|
|
+ private $requestUrl;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var array ( array ( uri => controller ) ) $routes
|
|
|
+ **/
|
|
|
+ private $routes;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var \Tools\Context $context
|
|
|
+ * /core/tools/Context.php
|
|
|
+ * Contains website's informations
|
|
|
+ **/
|
|
|
+ private $context;
|
|
|
+
|
|
|
/**
|
|
|
* @var string $modulePath
|
|
|
* Contains the module directory
|
|
|
**/
|
|
|
|
|
|
+ /**
|
|
|
+ * @var string $moduleUrl
|
|
|
+ * Contains the module Uri
|
|
|
+ **/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string $themePath
|
|
|
+ * Contains the theme directory
|
|
|
+ **/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string $themeUrl
|
|
|
+ * Contains the theme Uri
|
|
|
+ **/
|
|
|
+
|
|
|
/**
|
|
|
* Create the router, initialize url and path
|
|
|
**/
|
|
|
- public function __construct()
|
|
|
+ public function __construct($context)
|
|
|
{
|
|
|
$pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
|
|
|
$relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
|
|
|
$this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
|
|
|
$this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
|
|
|
+ $this->requestUrl = substr($_SERVER["REQUEST_URI"], count($this->rootUrl) -1);
|
|
|
+ $this->context = $context;
|
|
|
+ $this->routes = array();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * TODO@1 This function SHOULD be disabled by configuration
|
|
|
* 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
|
|
|
**/
|
|
|
public function init()
|
|
|
{
|
|
|
$siteUrl = \Entity\Config::getConfig(null, "siteUrl");
|
|
|
- if ($siteUrl != $_SERVER["HTTP_HOST"])
|
|
|
+ if ($siteUrl != $_SERVER["HTTP_HOST"] && $siteUrl !== null)
|
|
|
{
|
|
|
header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}");
|
|
|
die;
|
|
|
@@ -62,6 +98,11 @@ class Router
|
|
|
public function serveUrl()
|
|
|
{
|
|
|
$this->prepareUrl();
|
|
|
+ $requestParams = explode("/", $this->requestUrl);
|
|
|
+ foreach ($this->routes as $i)
|
|
|
+ {
|
|
|
+ $routeParams = explode("/", $i[0]);
|
|
|
+ }
|
|
|
//TODO@2
|
|
|
}
|
|
|
|
|
|
@@ -71,12 +112,36 @@ class Router
|
|
|
**/
|
|
|
private function prepareUrl()
|
|
|
{
|
|
|
- //TODO@2 add internal route config
|
|
|
+ $this->doRouteAdd("/:item", "\Controller\Product");
|
|
|
+ $this->doRouteAdd("/:category/:item", "\Controller\Product");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add a route to the internal route list
|
|
|
+ * Internal procedure
|
|
|
+ **/
|
|
|
+ private function doRouteAdd($route, $controller)
|
|
|
+ {
|
|
|
+ $this->routes[] = array($route, $controller);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $route Uri to match the controller
|
|
|
+ * Uri can be formatted as '/:param/static'.
|
|
|
+ * expl. '/product/:id/'
|
|
|
+ * @param string $controller Controller class name.
|
|
|
+ * new $controller() MUST return a \Tool\AController instance
|
|
|
+ *
|
|
|
+ * Add a route and a Controller to the list
|
|
|
+ * Can only be called from `routerSetup' hook
|
|
|
+ **/
|
|
|
+ public function routeAdd($route, $controller)
|
|
|
+ {
|
|
|
+ if (!$this->context->hookManager->isInHook("routerSetup"))
|
|
|
+ throw new \Exception("You can only add routes from `routerSetup' hook");
|
|
|
+ $this->doRouteAdd($route, $controller);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * TODO@1 check end of tag to format url and paths
|
|
|
- **/
|
|
|
public function __get($key)
|
|
|
{
|
|
|
switch ($key)
|
|
|
@@ -84,7 +149,11 @@ class Router
|
|
|
case "rootPath": return $this->rootPath; break;
|
|
|
case "rootUrl": return $this->rootUrl; break;
|
|
|
case "modulesPath": return $this->rootPath."content/modules/"; break;
|
|
|
+ case "modulesUrl": return $this->rootUrl."content/modules/"; break;
|
|
|
+ case "themesPath": return $this->rootPath."content/theme/"; break;
|
|
|
+ case "themesUrl": return $this->rootUrl."content/theme/"; break;
|
|
|
}
|
|
|
+ throw new \Exception("Cannot access attribute {$key}");
|
|
|
}
|
|
|
}
|
|
|
|