rootPath **/ private $rootPath; /** * @var string $rootPath * Contains the application's root path (ex: /srv/http/myshop/) with trailing slash * Can be accessed read-only via $instance->rootUrl **/ 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($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(); } /** * 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"] && $siteUrl !== null) { header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}"); die; } } /** * @return \Controller\AController controller * /core/controller/AController.php * Match request to a controller * return FALSE on failure (eg. 404) **/ public function serveUrl() { $this->prepareUrl(); $requestParams = explode("/", $this->requestUrl); foreach ($this->routes as $i) { $routeParams = explode("/", $i[0]); } //TODO@2 } /** * Append local routes to router * Will load CMS pages, categories page, products page, cart pages, etc. **/ private function prepareUrl() { $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); } public function __get($key) { switch ($key) { 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}"); } }