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

Commit 4d8918a

Browse files
author
Jefersson Nathan
committed
#44 — Work with container Interop interface for DI
1 parent cce97c2 commit 4d8918a

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/Route.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Fig\Http\Message\RequestMethodInterface;
2121
use Exception;
22+
use Interop\Container\ContainerInterface;
2223

2324
class Route
2425
{
@@ -75,12 +76,24 @@ class Route
7576
*/
7677
private $config;
7778

79+
/**
80+
* @var ContainerInterface
81+
*/
82+
private $container;
83+
84+
/**
85+
* @var string
86+
*/
87+
private $action;
88+
7889
/**
7990
* @param $resource
8091
* @param array $config
8192
*/
8293
public function __construct($resource, array $config)
8394
{
95+
96+
// @todo get action and controller when create the object instance
8497
$this->url = $resource;
8598
$this->config = $config;
8699
$this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
@@ -89,6 +102,11 @@ public function __construct($resource, array $config)
89102
$this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
90103
}
91104

105+
public function setContainer(ContainerInterface $container)
106+
{
107+
$this->container = $container;
108+
}
109+
92110
public function getUrl()
93111
{
94112
return $this->url;
@@ -187,13 +205,24 @@ public function setParameters(array $parameters)
187205

188206
public function dispatch()
189207
{
190-
$action = explode('::', $this->config['_controller']);
208+
list($controller, $action) = explode('::', $this->config['_controller']);
209+
210+
$this->action = !$action && trim($action) !== '' ? $action : null;
191211

192212
if ($this->parametersByName) {
193213
$this->parameters = array($this->parameters);
194214
}
195215

196-
$this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
216+
if ($this->container && $this->container->has($controller)) {
217+
$instance = $this->container->get($controller);
218+
call_user_func_array(
219+
// @todo action seems to be inconsistent
220+
array($instance, $this->action),
221+
$this->parameters
222+
);
223+
224+
return;
225+
}
197226

198227
if (!is_null($this->action)) {
199228
$instance = new $action[0];

0 commit comments

Comments
 (0)