1 | <?php |
||
29 | class Container implements ContainerInterface |
||
30 | { |
||
31 | /** @var object[] Instantiated services */ |
||
32 | private $services = []; |
||
33 | |||
34 | /** @var string BaseURL for api requests */ |
||
35 | private $baseUrl; |
||
36 | |||
37 | /** @var mixed[] Twig configuration (if needed) */ |
||
38 | private $twigConfig = []; |
||
39 | |||
40 | /** @var Debug Debug status */ |
||
41 | private $debug; |
||
42 | |||
43 | /** @var HttpHistory Latest requests / responses made */ |
||
44 | private $history; |
||
45 | |||
46 | public function __construct(HttpHistory $history, Debug $debug, string $baseUrl, array $twigConfig = []) |
||
47 | { |
||
48 | $this->debug = $debug; |
||
49 | $this->history = $history; |
||
50 | |||
51 | $this->baseUrl = $baseUrl; |
||
52 | $this->twigConfig = $twigConfig; |
||
53 | } |
||
54 | |||
55 | /** {@inheritDoc} */ |
||
56 | public function has($id) |
||
57 | { |
||
58 | static $services = [ |
||
59 | HttpClient::class, |
||
60 | HttpHistory::class, |
||
61 | StreamFactory::class, |
||
62 | MessageFactory::class, |
||
63 | ]; |
||
64 | |||
65 | if (class_exists(Twig_Environment::class)) { |
||
66 | $services[] = Twig_Environment::class; |
||
67 | } |
||
68 | |||
69 | return in_array($this->resolveAlias($id), $services); |
||
70 | } |
||
71 | |||
72 | /** {@inheritDoc} */ |
||
73 | public function get($id) |
||
74 | { |
||
75 | $id = $this->resolveAlias($id); |
||
76 | |||
77 | if (array_key_exists($id, $this->services)) { |
||
78 | return $this->services[$id]; |
||
79 | } |
||
80 | |||
81 | switch ($id) { |
||
82 | case HttpHistory::class: |
||
83 | return $this->history; |
||
84 | |||
85 | case HttpClient::class: |
||
86 | return $this->services[HttpClient::class] = $this->getHttpClient(); |
||
87 | |||
88 | case MessageFactory::class: |
||
89 | return $this->services[MessageFactory::class] = MessageFactoryDiscovery::find(); |
||
90 | |||
91 | case StreamFactory::class: |
||
92 | return $this->services[StreamFactory::class] = StreamFactoryDiscovery::find(); |
||
93 | |||
94 | case Twig_Environment::class: |
||
95 | return $this->services[Twig_Environment::class] = $this->getTwigService(); |
||
96 | } |
||
97 | |||
98 | throw new NotFoundException($id); |
||
99 | } |
||
100 | |||
101 | private function getHttpClient(): HttpClient |
||
116 | |||
117 | private function getTwigService(): ?Twig_Environment |
||
131 | |||
132 | private function resolveAlias(string $id): string |
||
133 | { |
||
134 | static $aliases = [ |
||
150 | } |
||
151 |