From 956ba33fcb38f1ac451965360d693e87407d38b2 Mon Sep 17 00:00:00 2001 From: Cawa Date: Wed, 12 Nov 2014 14:30:43 +0200 Subject: [PATCH] Base idea for DI --- src/PHPRouter/DI/ContainerInterface.php | 15 +++++++++ .../DI/Exceptions/InjectionException.php | 10 ++++++ src/PHPRouter/DI/InjectableInterface.php | 17 ++++++++++ src/PHPRouter/Route.php | 33 ++++++++++++++++++- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/PHPRouter/DI/ContainerInterface.php create mode 100644 src/PHPRouter/DI/Exceptions/InjectionException.php create mode 100644 src/PHPRouter/DI/InjectableInterface.php diff --git a/src/PHPRouter/DI/ContainerInterface.php b/src/PHPRouter/DI/ContainerInterface.php new file mode 100644 index 0000000..e98ec1a --- /dev/null +++ b/src/PHPRouter/DI/ContainerInterface.php @@ -0,0 +1,15 @@ +_url = $resource; $this->_config = $config; + $this->_container= $container; + $this->_methods = isset($config['methods']) ? $config['methods'] : array(); $this->_target = isset($config['target']) ? $config['target'] : null; } @@ -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; @@ -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); } + + }