1 | <?php |
||
11 | class Container implements \Psr\Container\ContainerInterface |
||
12 | { |
||
13 | private $services = []; |
||
14 | |||
15 | private $builder; |
||
16 | |||
17 | private $resolver; |
||
18 | |||
19 | private $construcrtorResolver; |
||
20 | |||
21 | private $methodResolver; |
||
22 | |||
23 | private $register; |
||
24 | |||
25 | 10 | public function __construct() |
|
26 | { |
||
27 | 10 | $this->register = new Register(); |
|
28 | |||
29 | 10 | $this->construcrtorResolver = new ConstructorResolver(); |
|
30 | |||
31 | 10 | $this->resolver = new Resolver(); |
|
32 | 10 | $this->resolver->setConstructorResolver($this->construcrtorResolver); |
|
33 | |||
34 | 10 | $this->methodResolver = new MethodResolver(); |
|
35 | |||
36 | 10 | $this->builder = new ArgumentBuilder(); |
|
37 | 10 | $this->builder->setContainer($this); |
|
38 | |||
39 | 10 | $this->methodResolver->setArgumentBuilder( |
|
40 | 10 | $this->builder |
|
41 | ); |
||
42 | 10 | } |
|
43 | |||
44 | 9 | public function loadServices(array $services) : void |
|
48 | |||
49 | 1 | public function getServicesConfiguration() : array |
|
53 | |||
54 | 7 | private function ensureServiceIsDefined(string $serviceName) : bool |
|
55 | { |
||
56 | 7 | if (!$this->contains($serviceName)) { |
|
57 | 1 | throw new \RuntimeException( |
|
58 | 1 | 'Oops! Service ' . $serviceName . ' not defined' |
|
59 | ); |
||
60 | } |
||
61 | |||
62 | 6 | return true; |
|
63 | } |
||
64 | |||
65 | 7 | public function get($serviceName) |
|
66 | { |
||
67 | 7 | $this->ensureServiceIsDefined($serviceName); |
|
68 | |||
69 | 6 | $service = Service::box([ |
|
70 | 6 | 'name' => $serviceName, |
|
71 | 6 | 'services' => $this->services, |
|
72 | ]); |
||
73 | |||
74 | 6 | if (!$this->register->has($service)) { |
|
75 | 6 | $this->register->store( |
|
76 | 6 | $service, |
|
77 | 6 | $this->getResolver($service) |
|
78 | ); |
||
79 | } |
||
80 | |||
81 | 5 | return $this->register->get($service); |
|
82 | } |
||
83 | |||
84 | 6 | public function getResolver(Service $service) |
|
96 | |||
97 | public function has($id) |
||
98 | { |
||
99 | return $this->contains($id); |
||
101 | |||
102 | 8 | public function contains(string $serviceName) : bool |
|
107 | |||
108 | 1 | public function hasArguments(string $serviceName) : bool |
|
112 | } |
||
113 |