Skip to content

Commit c12417e

Browse files
committed
First implementation of url building
1 parent b439b5c commit c12417e

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/Router/RouteContainerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ interface RouteContainerInterface {
2424
public function addRoute(RouteInterface $route);
2525

2626
public function matchRouteMethod($method, $path);
27+
28+
public function getRouteByName($name);
2729
}
2830
?>

src/Router/RouteLoader/JsonRouteLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class JsonRouteLoader implements RouteLoaderInterface {
3232

3333
public function __construct(array $filenames, $baseDir, RouteFactory $routeFactory = null){
3434
$this->filenames = $filenames;
35-
$this->baseDir = $baseDir;
35+
$this->baseDir = $baseDir;
3636
$this->routeFactory = $routeFactory == null ? new RouteFactory() : $routeFactory;
3737
}
3838

@@ -76,11 +76,11 @@ public function loadRoutes(RouteContainerInterface $routeContainer){
7676
}
7777
$routeContainer->addRoute($routeObj);
7878
} else {
79-
//throw new NoActionException
79+
//throw new NoActionsInJsonException
8080
}
8181
}
8282
}
83-
}
84-
}
83+
}
84+
}
8585
}
8686
?>

src/Router/RouterAbstract.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ public function __construct(RouteContainerInterface $routeContainer){
2626
$this->routeContainer = $routeContainer;
2727
}
2828

29-
public function buildUrl($routeName, $params = null){
30-
//TODO
29+
public function buildUrl($routeName, $params = array()){
30+
$route = $this->routeContainer->getRouteByName($routeName);
31+
//Get the path:
32+
$routePath = $route->getPath();
33+
//Building the url
34+
foreach($params as $paramName => $paramValue){
35+
$paramExpression = "/$paramName:[^\/]+/";
36+
$routePath = preg_replace($paramExpression, $paramValue, $routePath);
37+
}
38+
return $routePath;
3139
}
3240

3341
}

src/Router/TreeRouteContainer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class TreeRouteContainer implements RouteContainerInterface {
2626

2727
private $dataStructure;
28+
private $routeNameHolder;
2829

2930
public function __construct(TreeInterface $tree = null) {
3031
if($tree == null){
@@ -35,9 +36,19 @@ public function __construct(TreeInterface $tree = null) {
3536
}
3637

3738
public function addRoute(RouteInterface $route){
39+
$routeName = $route->getName();
40+
$this->routeNameHolder[$routeName] = &$route;
3841
$this->dataStructure->addNode($route->getPath(), $route);
3942
}
4043

44+
public function getRouteByName($name){
45+
if(isset($this->routeNameHolder[$name])){
46+
return $this->routeNameHolder[$name];
47+
} else {
48+
throw new RouteNotFoundException("Route by name '$name' was not found.");
49+
}
50+
}
51+
4152
public function matchRouteMethod($method, $path){
4253
try {
4354
$result = $this->dataStructure->traverse($path);

0 commit comments

Comments
 (0)