Skip to content

Commit 91eb8e3

Browse files
committed
Alter JsonRouteLoader class, delege parsing to other classes
1 parent ef1006d commit 91eb8e3

File tree

4 files changed

+152
-17
lines changed

4 files changed

+152
-17
lines changed
Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,80 @@
11
<?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+
*******************************************************************************/
220
namespace MinusFour\Router\RouteLoader;
321

422
use MinusFour\Router\Route;
523
use MinusFour\Router\Action;
24+
use MinusFour\Utils\JsonFileParser;
625
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;
732

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;
1537
}
1638

1739
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);
2455
} 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+
}
2675
}
27-
$routeObj->setMethodAction($method, $actionObj);
2876
}
29-
$routeContainer->addRoute($routeObj);
30-
}
77+
}
3178
}
3279
}
3380
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
*******************************************************************************/
20+
namespace MinusFour\Router\RouteLoader;
21+
22+
use MinusFour\Router\RouteContainerInterface;
23+
24+
interface RouteLoaderInterface {
25+
26+
public function loadRoutes(RouteContainerInterface $routerContainer);
27+
}
28+
?>

src/Utils/JsonFileParser.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
*******************************************************************************/
20+
namespace MinusFour\Utils;
21+
22+
class JsonFileParser {
23+
24+
public function parseFile($filename){
25+
$fileContent = file_get_contents($filename);
26+
$parsedFile = json_decode($fileContent, true);
27+
if(json_last_error()){
28+
throw new JsonParseException($filename);
29+
}
30+
return $parsedFile;
31+
}
32+
}
33+
?>

src/Utils/JsonParseException.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
*******************************************************************************/
20+
namespace MinusFour\Utils;
21+
22+
class JsonParseException extends \Exception {
23+
public function __construct($filename){
24+
parent::__construct("Error parsing $filename with error message: " . json_last_error_msg());
25+
}
26+
}
27+
?>

0 commit comments

Comments
 (0)