Skip to content

Commit 376b059

Browse files
committed
Add Factory for Action class, adapt it on JsonRouteLoader
1 parent df61baa commit 376b059

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

src/Router/ActionFactory.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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;
21+
22+
class ActionFactory implements ActionFactoryInterface {
23+
24+
public function createAction($class, $method, $fixedArgs = array()){
25+
return new Action($class, $method, $fixedArgs);
26+
}
27+
28+
}
29+
?>

src/Router/ActionFactoryInterface.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\Router;
21+
22+
interface ActionFactoryInterface {
23+
24+
public function createAction($class, $method, $fixedArgs);
25+
26+
}
27+
?>

src/Router/RouteLoader/JsonRouteLoader.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
namespace MinusFour\Router\RouteLoader;
2121

2222
use MinusFour\Router\RouteFactory;
23-
use MinusFour\Router\Action;
24-
use MinusFour\Utils\JsonFileParser;
23+
use MinusFour\Router\ActionFactory;
24+
use MinusFour\Router\ActionInterface;
2525
use MinusFour\Router\RouteContainerInterface;
26-
use MinusFour\Router\RouteLoader\RouteLoaderInterface;
26+
use MinusFour\Utils\JsonFileParser;
2727

2828
class JsonRouteLoader implements RouteLoaderInterface {
2929
private $filenames;
3030
private $baseDir;
3131
private $routeFactory;
32+
private $actionFactory;
3233

33-
public function __construct(array $filenames, $baseDir, RouteFactory $routeFactory = null){
34+
public function __construct(array $filenames, $baseDir, RouteFactory $routeFactory = null, ActionFactoryInterface $actionFactory = null){
3435
$this->filenames = $filenames;
3536
$this->baseDir = $baseDir;
3637
$this->routeFactory = $routeFactory == null ? new RouteFactory() : $routeFactory;
38+
$this->actionFactory = $actionFactory == null ? new ActionFactory() : $actionFactory;
3739
}
3840

3941
public function loadRoutes(RouteContainerInterface $routeContainer){
@@ -54,7 +56,7 @@ public function loadRoutes(RouteContainerInterface $routeContainer){
5456
$routePath = $this->routeFactory->getCurrentPath();
5557
//Move to new base point
5658
$this->routeFactory->addToPath($path);
57-
$routeLoader = new JsonRouteLoader([$newFile], dirname($fullpath), $this->routeFactory);
59+
$routeLoader = new JsonRouteLoader([$newFile], dirname($fullpath), $this->routeFactory, $this->actionFactory);
5860
$routeLoader->loadRoutes($routeContainer);
5961
//Reset route path
6062
$this->routeFactory->setPath($routePath);
@@ -68,9 +70,9 @@ public function loadRoutes(RouteContainerInterface $routeContainer){
6870
$actions = $route['actions'];
6971
foreach($actions as $method => $action){
7072
if(!isset($action['fixedArgs'])){
71-
$actionObj = new Action($action['class'], $action['method']);
73+
$actionObj = $this->actionFactory->createAction($action['class'], $action['method']);
7274
} else {
73-
$actionObj = new Action($action['class'], $action['method'], $action['fixedArgs']);
75+
$actionObj = $this->actionFactory->createAction($action['class'], $action['method'], $action['fixedArgs']);
7476
}
7577
$routeObj->setMethodAction($method, $actionObj);
7678
}

0 commit comments

Comments
 (0)