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

Commit

Permalink
#44 — Work with container Interop interface for DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefersson Nathan committed Apr 25, 2017
1 parent cce97c2 commit 4d8918a
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Fig\Http\Message\RequestMethodInterface;
use Exception;
use Interop\Container\ContainerInterface;

class Route
{
Expand Down Expand Up @@ -75,12 +76,24 @@ class Route
*/
private $config;

/**
* @var ContainerInterface
*/
private $container;

/**
* @var string
*/
private $action;

/**
* @param $resource
* @param array $config
*/
public function __construct($resource, array $config)
{

// @todo get action and controller when create the object instance
$this->url = $resource;
$this->config = $config;
$this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
Expand All @@ -89,6 +102,11 @@ public function __construct($resource, array $config)
$this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
}

public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}

public function getUrl()
{
return $this->url;
Expand Down Expand Up @@ -187,13 +205,24 @@ public function setParameters(array $parameters)

public function dispatch()
{
$action = explode('::', $this->config['_controller']);
list($controller, $action) = explode('::', $this->config['_controller']);

$this->action = !$action && trim($action) !== '' ? $action : null;

if ($this->parametersByName) {
$this->parameters = array($this->parameters);
}

$this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
if ($this->container && $this->container->has($controller)) {
$instance = $this->container->get($controller);
call_user_func_array(
// @todo action seems to be inconsistent
array($instance, $this->action),
$this->parameters
);

return;
}

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

0 comments on commit 4d8918a

Please sign in to comment.