Skip to content

Commit

Permalink
Merge pull request #6 from ShcherbaAlex/master
Browse files Browse the repository at this point in the history
Fix problems with class variables overlapping
  • Loading branch information
oleksandr-diudiun authored Jul 8, 2020
2 parents ef5f7be + 0ed82a8 commit 90fd6ed
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 85 deletions.
4 changes: 2 additions & 2 deletions examples/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$Router->loadRoutes($routes);

# Routing
$Router->matchRoute("http://example.com/post/", "POST");
$Router = $Router->getHandledRouter("http://example.com/post/", "POST");
# Run your Controller|Service|Component
$ServiceName = $Router->getService();
$ActionName = $Router->getAction();
Expand All @@ -40,4 +40,4 @@
}

$Respose->sendHeaders();
$Respose->sendContent();
$Respose->sendContent();
172 changes: 89 additions & 83 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,93 @@
* @author Oleksandr Diudiun
*/
class Router {
private $routes;
private $params;
private $actionName;
private $serviceName;
private $errorMessage;
private $errorServiceName;

function __construct() {}

public function loadRoutes(array $routes) {
if (empty($routes)) {
throw new RoutingException(_("Empty routes"), 1);
}

$this->routes = $routes;
}

public function matchRoute(string $rawUrl, string $method): array{
$url = parse_url($rawUrl);
if (!isset($url['path']) || is_string($url['path']) === false) {
throw new RoutingException(_("Router can't parse request."), 400);
}

if (!is_array($this->routes)) {
throw new RoutingException(_("Can't find any routes rules."), 501);
}
$params = [];
foreach ($this->routes as $regExpString => $route) {
$routes = 0;
$result = preg_match("/^" . str_replace("/", "\/", $regExpString) . "$/u", $url['path'], $matches);
if ($result === 0) {
continue;
} elseif ($result === false) {
throw new RoutingException(_("Can't parse route RegExp. ") . $regExpString, 501);
} elseif ($result === 1) {
if (isset($route[$method])) {
$this->serviceName = $route[$method][0];
$this->actionName = $route[$method][1];

$paramsCount = count($route[$method]) - 2;
if ($paramsCount > 0) {
for ($i = 0; $i < $paramsCount; $i++) {
$params[$route[$method][$i + 2]] = $matches[$i + 1];
}
} else {
$params = array();
}

$routes++;
break;
}
}
}

if ($routes === 0) {
throw new RoutingException(_("Not found route: ") . $method . " " . $rawUrl, 404);
}

if ($routes > 1) {
throw new RoutingException(_("Conflict. Multiple routes found."), 501);
}
return $params;
}

public function getParams(): ? array{
return $this->params;
}

public function getService() : string {
return $this->serviceName;
}

public function getAction(): ? string {
return $this->actionName;
}

public function getErrorMessage() : ? string {
return $this->errorMessage;
}

public function getErrorServiceName() : string {
return $this->errorServiceName;
}
protected static $routes;

private $params;
private $actionName;
private $serviceName;
private $errorMessage;
private $errorServiceName;

private function __clone() {}
private function __wakeup() {}
private function __construct(string $serviceName = null, string $actionName = null, array $params = []) {
$this->serviceName = $serviceName;
$this->actionName = $actionName;
$this->params = $params;
}

public static function loadRoutes(array $routes, string $environment) {
if (empty($routes)) {
throw new RoutingException(_("Empty routes"), 501);
}
self::$routes[$environment] = $routes;
}

public static function getHandledRouter(string $rawUrl, string $method, string $environment): ? self{
$url = parse_url($rawUrl);
$routeData = [];

if (!isset($url['path']) || is_string($url['path']) === false) {
throw new RoutingException(_("Router can't parse request."), 400);
}

if (!empty(self::$routes[$environment]) && !is_array(self::$routes[$environment])) {
throw new RoutingException(_("Can't find any routes rules."), 501);
}

$routesCount = 0;
foreach (self::$routes[$environment] as $regExpString => $route) {
$result = preg_match("/^" . str_replace("/", "\/", $regExpString) . "$/u", $url['path'], $matches);
if ($result === 0) {
continue;
} elseif ($result === false) {
throw new RoutingException(_("Can't parse route RegExp. ") . $regExpString, 501);
} elseif ($result === 1 && isset($route[$method])) {
$routeData['serviceName'] = $route[$method][0];
$routeData['actionName'] = $route[$method][1];

$paramsCount = count($route[$method]) - 2;
if ($paramsCount > 0) {
for ($i = 0; $i < $paramsCount; $i++) {
$routeData['params'][$route[$method][$i + 2]] = $matches[$i + 1];
}
} else {
$routeData['params'] = array();
}

$routesCount++;
break;
}
}

if ($routesCount > 1) {
throw new RoutingException(_("Conflict. Multiple routes found."), 501);
}
if ($routesCount === 0) {
throw new RoutingException(_("Route not found."), 501);
}

return new self($routeData['serviceName'], $routeData['actionName'], $routeData['params']);
}

public function getParams(): ? array{
return $this->params;
}

public function getService(): ? string{
return $this->serviceName;
}

public function getAction(): ? string{
return $this->actionName;
}

public function getErrorServiceName(): ? string{
return $this->errorServiceName;
}

public function getErrorMessage(): ? string {
return $this->errorMessage;
}
}

0 comments on commit 90fd6ed

Please sign in to comment.