1
|
|
|
<?php |
2
|
|
|
namespace Behapi; |
3
|
|
|
|
4
|
|
|
use Twig_Environment; |
5
|
|
|
use Twig_Loader_Array; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
use Interop\Container\ContainerInterface; |
9
|
|
|
|
10
|
|
|
use Http\Message\UriFactory; |
11
|
|
|
use Http\Message\StreamFactory; |
12
|
|
|
use Http\Message\MessageFactory; |
13
|
|
|
|
14
|
|
|
use Http\Discovery\HttpClientDiscovery; |
15
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
16
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
17
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
18
|
|
|
|
19
|
|
|
use Http\Client\HttpClient; |
20
|
|
|
use Http\Client\Common\PluginClient; |
21
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
22
|
|
|
use Http\Client\Common\Plugin\HistoryPlugin; |
23
|
|
|
use Http\Client\Common\Plugin\ContentLengthPlugin; |
24
|
|
|
|
25
|
|
|
use Behapi\Tools\Debug; |
26
|
|
|
use Behapi\Tools\HttpHistory; |
27
|
|
|
|
28
|
|
|
use Behapi\ServiceContainer\NotFoundException; |
29
|
|
|
use Behapi\ServiceContainer\ServiceNotAvailableException; |
30
|
|
|
|
31
|
|
|
class Container implements ContainerInterface |
32
|
|
|
{ |
33
|
|
|
/** @var object[] Instanciated services */ |
34
|
|
|
private $services = []; |
35
|
|
|
|
36
|
|
|
/** @var string BaseURL for api requests */ |
37
|
|
|
private $baseUrl; |
38
|
|
|
|
39
|
|
|
/** @var mixed[] Twig configuration (if needed) */ |
40
|
|
|
private $twigConfig = []; |
41
|
|
|
|
42
|
|
|
/** @var Debug Debug status */ |
43
|
|
|
private $debug; |
44
|
|
|
|
45
|
|
|
/** @var HttpHistory Latest requests / responses made */ |
46
|
|
|
private $history; |
47
|
|
|
|
48
|
|
|
public function __construct(HttpHistory $history, Debug $debug, string $baseUrl, array $twigConfig = []) |
49
|
|
|
{ |
50
|
|
|
$this->debug = $debug; |
51
|
|
|
$this->history = $history; |
52
|
|
|
|
53
|
|
|
$this->baseUrl = $baseUrl; |
54
|
|
|
$this->twigConfig = $twigConfig; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritDoc} */ |
58
|
|
|
public function has($id) |
59
|
|
|
{ |
60
|
|
|
static $services = [ |
61
|
|
|
'http.client', |
62
|
|
|
'http.history', |
63
|
|
|
'http.stream_factory', |
64
|
|
|
'http.message_factory', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
if (class_exists(Twig_Environment::class)) { |
68
|
|
|
$services[] = 'twig'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return in_array($id, $services); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** {@inheritDoc} */ |
75
|
|
|
public function get($id) |
76
|
|
|
{ |
77
|
|
|
if (array_key_exists($id, $this->services)) { |
78
|
|
|
return $this->services[$id]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
switch ($id) { |
82
|
|
|
case 'http.history': |
83
|
|
|
return $this->history; |
84
|
|
|
|
85
|
|
|
case 'http.client': |
86
|
|
|
return $this->getHttpClient(); |
87
|
|
|
|
88
|
|
|
case 'http.message_factory': |
89
|
|
|
return $this->services['http.message_factory'] = MessageFactoryDiscovery::find(); |
90
|
|
|
|
91
|
|
|
case 'http.stream_factory': |
92
|
|
|
return $this->services['http.stream_factory'] = StreamFactoryDiscovery::find(); |
93
|
|
|
|
94
|
|
|
case 'twig': |
95
|
|
|
return $this->getTwigService(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new NotFoundException($id); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getHttpClient(): HttpClient |
102
|
|
|
{ |
103
|
|
|
$uriFactory = UriFactoryDiscovery::find(); |
104
|
|
|
$baseUri = $uriFactory->createUri($this->baseUrl); |
105
|
|
|
|
106
|
|
|
$plugins = [ |
107
|
|
|
new ContentLengthPlugin, |
108
|
|
|
new BaseUriPlugin($baseUri), |
109
|
|
|
new HistoryPlugin($this->history) |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$http = HttpClientDiscovery::find(); |
113
|
|
|
|
114
|
|
|
return $this->services['http.client'] = new PluginClient($http, $plugins); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getTwigService(): ?Twig_Environment |
118
|
|
|
{ |
119
|
|
|
if (!class_exists(Twig_Environment::class)) { |
120
|
|
|
return $this->services['twig'] = null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$options = [ |
124
|
|
|
'debug' => $this->debug->getStatus(), |
125
|
|
|
'cache' => $this->twigConfig['cache'] ?? false, |
126
|
|
|
'autoescape' => $this->twigConfig['autoescape'] ?? false |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
return $this->services['twig'] = new Twig_Environment(new Twig_Loader_Array, $options); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|