Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/PHPRouter/DI/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PHPRouter\DI;

/**
* Interface ContainerInterface
* @package PHPRouter\DI
*/
interface ContainerInterface
{
/**
* @TODO maybe some methods?
*/

}
10 changes: 10 additions & 0 deletions src/PHPRouter/DI/Exceptions/InjectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PHPRouter\DI\Exceptions;

use \Exception;

class InjectionException extends Exception
{

}
17 changes: 17 additions & 0 deletions src/PHPRouter/DI/InjectableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace PHPRouter\DI;

/**
* Interface InjectableInterface
* @package PHPRouter\DI
*/
interface InjectableInterface
{
/**
* Set the service container
* @return mixed
*/
public function setServiceContainer();

}
33 changes: 32 additions & 1 deletion src/PHPRouter/Route.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace PHPRouter;

use PHPRouter\DI\ContainerInterface;
use PHPRouter\DI\InjectableInterface;
use PHPRouter\DI\Exceptions\InjectionException;

class Route
{
/**
Expand Down Expand Up @@ -39,14 +43,22 @@ class Route
*/
private $_parameters = array();

/**
* Service container
* @var object
*/
private $_container;

/**
* @param $resource
* @param array $config
*/
public function __construct($resource, array $config)
public function __construct($resource, array $config, ContainerInterface $container = null)
{
$this->_url = $resource;
$this->_config = $config;
$this->_container= $container;

$this->_methods = isset($config['methods']) ? $config['methods'] : array();
$this->_target = isset($config['target']) ? $config['target'] : null;
}
Expand Down Expand Up @@ -120,6 +132,16 @@ public function getParameters()
return $this->_parameters;
}

/**
* @return object
*/
public function getContainer()
{
return $this->_container;
}



public function setParameters(array $parameters)
{
$this->_parameters = $parameters;
Expand All @@ -129,6 +151,15 @@ public function dispatch()
{
$action = explode('::', $this->_config['_controller']);
$instance = new $action[0];

if($this->getContainer() && $instance instanceof InjectableInterface){
$instance->setServiceContainer($this->getContainer());
}else{
throw new InjectionException('To inject container you need to implement InjectableInterface');
}

call_user_func_array(array($instance, $action[1]), $this->_parameters);
}


}