Skip to content

Commit ef1006d

Browse files
committed
Add JsonRouteLoader and fix README.md
1 parent 96df524 commit ef1006d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ which at the moment is unfinished.
3434

3535
Start by including the autoloader and setting up the classes:
3636
```php
37-
include 'src/Autload.php'
37+
include 'Autoload.php'
3838

3939
use MinusFour\Router\Action;
4040
use MinusFour\Router\Route;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace MinusFour\Router\RouteLoader;
3+
4+
use MinusFour\Router\Route;
5+
use MinusFour\Router\Action;
6+
use MinusFour\Router\RouteContainerInterface;
7+
8+
class JsonRouteLoader {
9+
protected $routes;
10+
11+
public function __construct($fileName){
12+
$fileContent = file_get_contents($fileName);
13+
//Decode Json
14+
$this->routes = json_decode($fileContent, true);
15+
}
16+
17+
public function loadRoutes(RouteContainerInterface $routeContainer){
18+
foreach($this->routes as $routeName => $route){
19+
$routeObj = new Route($routeName, $route['path']);
20+
$actions = $route['actions'];
21+
foreach($actions as $method => $action){
22+
if(!isset($action['fixedArgs'])){
23+
$actionObj = new Action($action['class'], $action['method']);
24+
} else {
25+
$actionObj = new Action($action['class'], $action['method'], $action['fixedArgs']);
26+
}
27+
$routeObj->setMethodAction($method, $actionObj);
28+
}
29+
$routeContainer->addRoute($routeObj);
30+
}
31+
}
32+
}
33+
?>

0 commit comments

Comments
 (0)