-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.php
82 lines (69 loc) · 1.71 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace LabZone\Injector;
use LabZone\Injector\Factory\Creator;
use LabZone\Injector\Exception\ServiceNotFoundException;
/**
* Dependency injection container
*/
class Container extends Creator
{
/**
* service instances
* @var array
*/
private static $instances=array();
/**
* Container instance
* @var Container
*/
private static $instance;
/**
* get container instance
* @return Container
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance=new Container();
}
return self::$instance;
}
/**
* create new service
* @param string $className
* @return object
*/
public function create($className)
{
if (isset($this->instances[$className])) {
return $this->instances[$classNames];
}
$class=new \ReflectionClass($className);
$constructor=$class->getConstructor();
return $class->newInstance();
}
/**
* register new service
* @param string $service unique name
* @param object $callback
* @return self
*/
public function register($service,$callback)
{
self::$instances[$service]=$callback;
return $this;
}
/**
* get service
* @param string $service unique service name
* @return object service requested
* @throws ServiceNotFoundException If Service not found
*/
public function get($service)
{
if (isset(self::$instances[$service])) {
return self::$instances[$service];
}
throw new ServiceNotFoundException($service);
}
}