Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1691cd2

Browse files
author
Fady Michel
committed
Router implements RouterInterface
1 parent 9bf0a5a commit 1691cd2

File tree

7 files changed

+259
-81
lines changed

7 files changed

+259
-81
lines changed

‎composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "FadymichelR/Php-Routing",
2+
"name": "webbym/php-routing",
33
"description": "a simple routing php",
44
"type": "package",
55
"autoload": {
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.2"
19+
"php": ">=7.2",
20+
"psr/http-message": "~1.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "~7.1"

‎composer.lock

Lines changed: 54 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Route.php

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
/**
66
* Class Route
7-
* @package Fady\Routing
7+
* @package Webbym\Routing
88
*/
99
class Route
1010
{
11+
/**
12+
* @var string
13+
*/
14+
protected $name;
15+
1116
/**
1217
* @var string
1318
*/
@@ -47,10 +52,11 @@ class Route
4752
* @param array $requirements
4853
* @param array $varsNames
4954
*/
50-
public function __construct(string $path, string $controller, string $action, array $requirements = [])
55+
public function __construct(string $name, string$path, string $controller, string $action, array $requirements = [])
5156
{
5257
$this->setPath($path)
5358
->setController($controller)
59+
->setName($name)
5460
->setAction($action)
5561
->setRequirements($requirements);
5662
}
@@ -61,7 +67,12 @@ public function __construct(string $path, string $controller, string $action, ar
6167
*/
6268
public function hasVars()
6369
{
64-
return !empty($this->varsNames);
70+
if (empty($this->varsNames)) {
71+
72+
preg_match_all('/{[^}]*}/', $this->path, $matches);
73+
$this->setVarsNames(reset($matches));
74+
}
75+
return !empty($this->getVarsNames());
6576
}
6677

6778
/**
@@ -70,39 +81,62 @@ public function hasVars()
7081
*/
7182
public function match(string $path)
7283
{
73-
if ('/' !== substr($path, -1)) {
74-
$path = $path.'/';
84+
$path = $this->trimPath($path);
85+
if (preg_match('#^'.$this->generateRegex().'$#sD', $path, $matches)) {
86+
87+
return array_filter($matches, function ($key) {
88+
return is_string($key);
89+
}, ARRAY_FILTER_USE_KEY);
7590
}
76-
preg_match_all('/{[^}]*}/', $this->path, $matches);
77-
$variables = reset($matches);
91+
92+
return null;
93+
}
94+
95+
96+
/**
97+
* @return string
98+
*/
99+
private function generateRegex() {
78100

79101
$regex = $this->path;
80-
if (!empty($variables)) {
81-
foreach ($variables as $variable) {
102+
if ($this->hasVars()) {
103+
104+
foreach ($this->getVarsNames() as $variable) {
82105
$varName = trim($variable,'{\}');
83106
$regex = str_replace($variable, '(?P<'.$varName.'>[^/]++)', $regex);
84-
$this->addVarName($varName);
85107
}
86108
}
87109

88-
if (preg_match('#^'.$regex.'$#sD', $path, $matches)) {
89-
return array_filter($matches, function ($key) {
90-
return is_string($key);
91-
}, ARRAY_FILTER_USE_KEY);
92-
}
110+
return $regex;
111+
}
93112

94-
return null;
113+
/**
114+
* @return string
115+
*/
116+
public function getName(): string
117+
{
118+
return $this->name;
95119
}
96120

121+
/**
122+
* @param string $name
123+
* @return Route
124+
*/
125+
public function setName(string $name): Route
126+
{
127+
$this->name = $name;
128+
return $this;
129+
}
130+
131+
97132
/**
98133
* @param string $action
99134
* @return Route
100135
*/
101136
public function setAction(string $action)
102137
{
103-
if (is_string($action)) {
104-
$this->action = $action;
105-
}
138+
139+
$this->action = $action;
106140
return $this;
107141
}
108142

@@ -112,12 +146,21 @@ public function setAction(string $action)
112146
*/
113147
public function setController(string $controller)
114148
{
115-
if (is_string($controller)) {
116-
$this->controller = $controller;
117-
}
149+
150+
$this->controller = $controller;
151+
118152
return $this;
119153
}
120154

155+
/**
156+
* @return string
157+
*/
158+
public function getPath(): string
159+
{
160+
return $this->path;
161+
}
162+
163+
121164
/**
122165
* @param string $path
123166
* @return Route
@@ -227,6 +270,7 @@ public function setRequirements(array $requirements = []): Route
227270
*/
228271
private function trimPath(string $path)
229272
{
230-
return '/'.rtrim(ltrim(trim($path), '/'), '/').'/';
273+
274+
return '/'.rtrim(ltrim(trim($path), '/'), '/');
231275
}
232276
}

‎src/Router.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
34
namespace Webbym\Routing;
5+
use Psr\Http\Message\ServerRequestInterface;
46

57
/**
68
* Class Router
79
* @package Fady\Routing
810
*/
9-
class Router
11+
class Routerimplements RouterInterface
1012
{
1113
const NO_ROUTE = 1;
1214

@@ -40,10 +42,10 @@ public function __construct(RouteBuilderInterface $routeBuilder = null)
4042
* @param Route $route
4143
* @return Router
4244
*/
43-
public function addRoute(Route $route)
45+
public function addRoute(Route $route) : RouterInterface
4446
{
4547
if (!in_array($route, $this->routes)) {
46-
$this->routes[] = $route;
48+
$this->routes[$route->getName()] = $route;
4749
}
4850
return $this;
4951
}
@@ -53,14 +55,14 @@ public function addRoute(Route $route)
5355
* @return Route|mixed
5456
* @throws \Exception
5557
*/
56-
public function getRoute($path)
58+
public function match(ServerRequestInterface$serverRequest) : Route
5759
{
5860
/**
5961
* @var Route $route
6062
*/
6163
foreach ($this->routes as $route) {
6264

63-
$varsValues = $route->match($path);
65+
$varsValues = $route->match($serverRequest->getUri()->getPath());
6466
if (!is_null($varsValues)) {
6567

6668
if ($route->hasVars()) {
@@ -78,4 +80,51 @@ public function getRoute($path)
7880

7981
throw new \Exception('Aucune route ne correspond à l\'URL', self::NO_ROUTE);
8082
}
83+
84+
85+
/**
86+
* @param string $name
87+
* @param array $parameters
88+
* @param int $referenceType
89+
* @return string
90+
* @throws \Exception
91+
*/
92+
public function generateUri(string $name, array $parameters = [], $referenceType = self::ABSOLUTE_PATH): string
93+
{
94+
if (!array_key_exists($name , $this->routes)) {
95+
96+
throw new \Exception(sprintf('%s name route doesnt exist', $name));
97+
}
98+
99+
/**
100+
* @var Route $route
101+
*/
102+
$route = $this->routes[$name];
103+
$uri = $route->getPath();
104+
105+
if ($route->hasVars()) {
106+
if (empty($parameters)) {
107+
throw new \Exception(
108+
sprintf(
109+
'%s route need parameters: %s',
110+
$name,
111+
implode(',',$route->getVarsNames())
112+
)
113+
);
114+
}
115+
116+
foreach ($route->getVarsNames() as $variable) {
117+
$varName = trim($variable,'{\}');
118+
if (!array_key_exists($varName , $parameters)) {
119+
throw new \Exception(sprintf('%s not found in parameters to generate url', $varName));
120+
}
121+
$uri = str_replace($variable, $parameters[$varName], $uri);
122+
}
123+
124+
}
125+
126+
return $uri;
127+
128+
}
129+
81130
}

‎src/RouterInterface.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Webbym\Routing;
4+
5+
use Psr\Http\Message\ServerRequestInterface;
6+
7+
/**
8+
* Interface RouterInterface
9+
*/
10+
interface RouterInterface
11+
{
12+
/**
13+
* Generates an absolute URL, e.g. "http://example.com/dir/file".
14+
*/
15+
const ABSOLUTE_URL = 0;
16+
17+
/**
18+
* Generates an absolute path, e.g. "/dir/file".
19+
*/
20+
const ABSOLUTE_PATH = 1;
21+
22+
/**
23+
* @param Route $route
24+
* @return RouterInterface
25+
*/
26+
public function addRoute(Route $route) : RouterInterface;
27+
28+
/**
29+
* @param ServerRequestInterface $serverRequest
30+
* @return null|Route
31+
* @throws \Exception if no found route.
32+
*/
33+
public function match(ServerRequestInterface $serverRequest) : Route;
34+
35+
/**
36+
* @param string $name
37+
* @param array $substitutions
38+
* @param array $options
39+
* @return string
40+
* @throws \Exception if unable to generate the given URI.
41+
*/
42+
public function generateUri(string $name, array $parameters = [], $referenceType = self::ABSOLUTE_PATH) : string;
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /