Created
December 18, 2012 13:27
-
-
Save neonxp/4327975 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple DI container for projects with legacy code without PSR-0 classes and namespaces | |
* Usage: | |
* require_once('DIC/Container.php'); | |
* $c = new Container(); | |
* $c->get('example')->method(); | |
*/ | |
class Container { | |
private $services = null; | |
private $path = null; | |
public function __construct($serviceFile = 'services.json', $path = null) { | |
if (!file_exists($serviceFile)) { | |
throw new Exception("No such service definition file: $serviceFile"); | |
} | |
$json = file_get_contents($serviceFile); | |
$this->services = json_decode($json, true); | |
if (is_null($path)) { | |
$this->path = __DIR__ . '/../'; | |
} | |
} | |
public function get($service) { | |
if (!isset($this->services[$service])) { | |
throw new Exception("No such service: " . $service); | |
} | |
$serviceDefinition = $this->services[$service]; | |
if (isset($serviceDefinition['file'])) { | |
if (file_exists( . $serviceDefinition['file'])) { | |
require_once($this->path . $serviceDefinition['file']); | |
} else { | |
throw new Exception("No such file: " . $this->path . $serviceDefinition['file']); | |
} | |
} else { | |
throw new Exception("File section required for $service"); | |
} | |
$class = $serviceDefinition['class']; | |
$instance = null; | |
if (class_exists($class)) { | |
if (isset($serviceDefinition['arguments'])) { | |
$refMethod = new ReflectionMethod($class, '__construct'); | |
$params = $refMethod->getParameters(); | |
$re_args = array(); | |
foreach($params as $key=>$param) { | |
if (isset($param->name)) { | |
$re_args[$key] = $this->parseArgument($serviceDefinition['arguments'][$param->name]); | |
} | |
} | |
$refClass = new ReflectionClass($class); | |
$instance = $refClass->newInstanceArgs((array) $re_args); | |
} else { | |
$instance = new $class(); | |
} | |
} else { | |
throw new Exception("Class $class not found"); | |
} | |
if (is_null($instance)) { | |
throw new Exception("Class $class not instanced"); | |
} | |
if ($serviceDefinition['calls']) { | |
foreach($serviceDefinition['calls'] as $method => $arguments) { | |
if (method_exists($instance, $method)) { | |
$re_args = array(); | |
if (!empty($arguments)) { | |
foreach ($arguments as $argument) { | |
$re_args[] = $this->parseArgument($argument); | |
} | |
} | |
call_user_func_array(array($instance, $method), $re_args); | |
} else { | |
throw new Exception("No such method $method on $class"); | |
} | |
} | |
} | |
return $instance; | |
} | |
private function parseArgument($argument) { | |
if (substr($argument,0,1) == '@') { | |
$service = substr($argument,1); | |
return $this->get($service); | |
} else { | |
return $argument; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"example": { | |
"file" : "example/example.php", | |
"class" : "exampleClass", | |
"arguments" : { | |
"client" : "@fooService" | |
} | |
}, | |
"fooService": { | |
"file" : "foo/bar.php", | |
"class" : "bar", | |
"calls" : { | |
"init": { | |
"param1" : "test... test...", | |
"param2" : 123456 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment