|
1 | 1 | <?php
|
| 2 | +/*****************************************************************************/ |
| 3 | +/* PhpTreeBasedRouter |
| 4 | +Copyright (C) 2015 Alejandro Quiroga |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or |
| 7 | +modify it under the terms of the GNU General Public License |
| 8 | +as published by the Free Software Foundation; either version 2 |
| 9 | +of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program; if not, write to the Free Software |
| 18 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +*******************************************************************************/ |
2 | 20 | namespace MinusFour\Router\RouteLoader;
|
3 | 21 |
|
4 | 22 | use MinusFour\Router\Route;
|
5 | 23 | use MinusFour\Router\Action;
|
| 24 | +use MinusFour\Utils\JsonFileParser; |
6 | 25 | use MinusFour\Router\RouteContainerInterface;
|
| 26 | +use MinusFour\Router\RouteLoader\RouteLoaderInterface; |
| 27 | + |
| 28 | +class JsonRouteLoader implements RouteLoaderInterface { |
| 29 | + private $filenames; |
| 30 | + private $baseDir; |
| 31 | + private $baseRoute; |
7 | 32 |
|
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); |
| 33 | + public function __construct(array $filenames, $baseDir, $baseRoute = ''){ |
| 34 | + $this->filenames = $filenames; |
| 35 | + $this->baseDir = $baseDir; |
| 36 | + $this->baseRoute = $baseRoute; |
15 | 37 | }
|
16 | 38 |
|
17 | 39 | 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']); |
| 40 | + $jsonParser = new JsonFileParser(); |
| 41 | + foreach($this->filenames as $filename){ |
| 42 | + $routes = $jsonParser->parseFile($this->baseDir . $filename); |
| 43 | + //Debugging purposes: |
| 44 | + //echo "Opening: $this->baseDir" . $filename . PHP_EOL; |
| 45 | + foreach($routes as $routeName => $route){ |
| 46 | + $path = $route['path']; |
| 47 | + if(isset($route['include'])){ |
| 48 | + //use RouteLoader |
| 49 | + $newFile = $route['include']; |
| 50 | + //Debugging purposes: |
| 51 | + //echo "Attempting to load $newFile" . PHP_EOL; |
| 52 | + $fullpath = realpath($this->baseDir . $newFile); |
| 53 | + $routeLoader = new JsonRouteLoader([$newFile], dirname($fullpath), $path); |
| 54 | + $routeLoader->loadRoutes($routeContainer); |
24 | 55 | } else {
|
25 |
| - $actionObj = new Action($action['class'], $action['method'], $action['fixedArgs']); |
| 56 | + //Debugging purposes: |
| 57 | + //echo "Route: $this->baseRoute" . $path . PHP_EOL; |
| 58 | + //Possibly Normal Route? |
| 59 | + if(isset($route['actions'])){ |
| 60 | + //Valid route |
| 61 | + $routeObj = new Route($routeName, $this->baseRoute . $path); |
| 62 | + $actions = $route['actions']; |
| 63 | + foreach($actions as $method => $action){ |
| 64 | + if(!isset($action['fixedArgs'])){ |
| 65 | + $actionObj = new Action($action['class'], $action['method']); |
| 66 | + } else { |
| 67 | + $actionObj = new Action($action['class'], $action['method'], $action['fixedArgs']); |
| 68 | + } |
| 69 | + $routeObj->setMethodAction($method, $actionObj); |
| 70 | + } |
| 71 | + $routeContainer->addRoute($routeObj); |
| 72 | + } else { |
| 73 | + //throw new NoActionException |
| 74 | + } |
26 | 75 | }
|
27 |
| - $routeObj->setMethodAction($method, $actionObj); |
28 | 76 | }
|
29 |
| - $routeContainer->addRoute($routeObj); |
30 |
| - } |
| 77 | + } |
31 | 78 | }
|
32 | 79 | }
|
33 | 80 | ?>
|
0 commit comments