<?php
 namespace Engine\Mvc\Router; class Router { protected $routes = []; public function addRoute($alias, $path, $controller=null, $R=null) { $this->routes[] = [ 'alias' => $alias, 'path' => $path, 'controller' => $controller, 'R' => $R, '_type' => 'auto', ]; return $this; } public function addRouteComplex($alias, $path, $controller=null, $R=null) { $this->routes[] = [ 'alias' => $alias, 'path' => $path, 'controller' => $controller, 'R' => $R, '_type' => 'complex', ]; return $this; } public function matchAlias($value) { $route = $this->matchAlias_exact($value); if ($route) { return $route; } $route = $this->matchAlias_regex($value); if ($route) { return $route; } throw new \Exception("Could not match a route with `alias`=`{$value}`."); } private function matchAlias_exact($value) { foreach ($this->routes as $route) { if ($route['_type']!='complex' && $route['alias']==$value) { return $this->buildRouteObject($route); } } } private function matchAlias_regex($alias) { foreach ($this->routes as $route) { $routeAlias = str_replace("\{any\}", "(?P<any>.*)", preg_quote($route['alias'], "/")); $matches = []; if (preg_match("/{$routeAlias}/", (string)$alias, $matches)) { $path = str_replace("{any}", $matches['any'], $route['path']); return $this->buildRouteObject($route, $alias, $path, null, $route['R']); } } } public function matchPath($value) { $route = $this->matchPath_exact($value); if ($route) { return $route; } $route = $this->matchPath_regex($value); if ($route) { return $route; } throw new \Exception("Could not match a route with `path`=`{$value}`."); } private function matchPath_exact($value) { foreach ($this->routes as $route) { if ($route['_type']!='complex' && $route['path']==$value) { return $this->buildRouteObject($route); } } } private function matchPath_regex($path) { foreach ($this->routes as $route) { $routePath = str_replace("\{any\}", "(?P<any>.*)", preg_quote($route['path'], "/")); $matches = []; if (preg_match("/{$routePath}/", $path, $matches)) { $alias = str_replace("{any}", $matches['any'], $route['alias']); return $this->buildRouteObject($route, $alias, $path, null, $route['R']); } } } private function buildRouteObject($route, $alias=null, $path=null, $controller=null, $R=null) { $routeObj = new \Engine\Mvc\Router\Route($route); $routeObj->buildAlias = $alias; $routeObj->buildPath = $path; $routeObj->buildController = $controller; $routeObj->R = $R; return $routeObj; } } 