Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit 8894114

Browse files
author
Jefersson Nathan
committed
#44 — Guard controller and action name when instantiate the Route object
1 parent 2d05727 commit 8894114

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/Route.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,27 @@ class Route
7979
*/
8080
private $action;
8181

82+
/**
83+
* @var string
84+
*/
85+
private $controller;
86+
8287
/**
8388
* @param $resource
8489
* @param array $config
8590
*/
8691
public function __construct($resource, array $config)
8792
{
88-
// @todo get action and controller when create the object instance
89-
$this->url = $resource;
90-
$this->config = $config;
91-
$this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
92-
$this->target = isset($config['target']) ? $config['target'] : null;
93-
$this->name = isset($config['name']) ? $config['name'] : null;
93+
$this->url = $resource;
94+
$this->config = $config;
95+
96+
list($controller, $action) = explode('::', $this->config['_controller']);
97+
98+
$this->controller = $controller;
99+
$this->action = $action;
100+
$this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
101+
$this->target = isset($config['target']) ? $config['target'] : null;
102+
$this->name = isset($config['name']) ? $config['name'] : null;
94103
}
95104

96105
public function setContainer(ContainerInterface $container)
@@ -177,35 +186,32 @@ public function setParameters(array $parameters)
177186

178187
public function dispatch()
179188
{
180-
list($controller, $action) = explode('::', $this->config['_controller']);
181-
182-
$this->action = !$action && trim($action) !== '' ? $action : null;
183-
184189
if ($this->parametersByName) {
185190
$this->parameters = array($this->parameters);
186191
}
187192

193+
$controller = $this->controller;
194+
188195
if ($this->container && $this->container->has($controller)) {
189196
$instance = $this->container->get($controller);
190197
call_user_func_array(
191-
// @todo action seems to be inconsistent
192-
array($instance, $this->action),
198+
array($instance, $this->getAction()),
193199
$this->parameters
194200
);
195201

196202
return;
197203
}
198204

199-
if (!is_null($this->action)) {
200-
$instance = new $action[0];
201-
call_user_func_array(array($instance, $this->action), $this->parameters);
205+
if (!is_null($this->getAction())) {
206+
$instance = new $controller;
207+
call_user_func_array(array($instance, $this->getAction()), $this->parameters);
202208
} else {
203-
$instance = new $action[0]($this->parameters);
209+
$instance = new $controller($this->parameters);
204210
}
205211
}
206212

207213
public function getAction()
208214
{
209-
return $this->action;
215+
return '' !== trim($this->action) ? $this->action : null;
210216
}
211217
}

0 commit comments

Comments
 (0)