|
1
|
|
|
<?php |
|
2
|
|
|
namespace Behapi; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
|
5
|
|
|
|
|
6
|
|
|
use Behat\Behat\HelperContainer\Exception\ServiceNotFoundException; |
|
7
|
|
|
|
|
8
|
|
|
use Http\Message\StreamFactory; |
|
9
|
|
|
use Http\Message\MessageFactory; |
|
10
|
|
|
|
|
11
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
12
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
13
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
|
14
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
15
|
|
|
|
|
16
|
|
|
use Http\Client\HttpClient; |
|
17
|
|
|
use Http\Client\Common\PluginClient; |
|
18
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
|
19
|
|
|
use Http\Client\Common\Plugin\HistoryPlugin; |
|
20
|
|
|
use Http\Client\Common\Plugin\ContentLengthPlugin; |
|
21
|
|
|
|
|
22
|
|
|
use Behapi\Tools\Debug; |
|
23
|
|
|
use Behapi\Tools\HttpHistory; |
|
24
|
|
|
|
|
25
|
|
|
final class Container implements ContainerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var object[] Instantiated services */ |
|
28
|
|
|
private $services = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string BaseURL for api requests */ |
|
31
|
|
|
private $baseUrl; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Debug Debug status */ |
|
34
|
|
|
private $debug; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(HttpHistory $history, Debug $debug, string $baseUrl) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->debug = $debug; |
|
39
|
|
|
$this->services[HttpHistory::class] = $history; |
|
40
|
|
|
|
|
41
|
|
|
$this->baseUrl = $baseUrl; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** {@inheritDoc} */ |
|
45
|
|
|
public function has($id) |
|
46
|
|
|
{ |
|
47
|
|
|
static $services = [ |
|
48
|
|
|
HttpClient::class, |
|
49
|
|
|
HttpHistory::class, |
|
50
|
|
|
StreamFactory::class, |
|
51
|
|
|
MessageFactory::class, |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
return in_array($id, $services); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** {@inheritDoc} */ |
|
58
|
|
|
public function get($id) |
|
59
|
|
|
{ |
|
60
|
|
|
if (array_key_exists($id, $this->services)) { |
|
61
|
|
|
return $this->services[$id]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
switch ($id) { |
|
65
|
|
|
case HttpClient::class: |
|
66
|
|
|
return $this->services[$id] = $this->getHttpClient(); |
|
67
|
|
|
|
|
68
|
|
|
case MessageFactory::class: |
|
69
|
|
|
return $this->services[$id] = MessageFactoryDiscovery::find(); |
|
70
|
|
|
|
|
71
|
|
|
case StreamFactory::class: |
|
72
|
|
|
return $this->services[$id] = StreamFactoryDiscovery::find(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new ServiceNotFoundException("Service {$id} is not available", $id); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getHttpClient(): HttpClient |
|
79
|
|
|
{ |
|
80
|
|
|
$uriFactory = UriFactoryDiscovery::find(); |
|
81
|
|
|
$baseUri = $uriFactory->createUri($this->baseUrl); |
|
82
|
|
|
|
|
83
|
|
|
$plugins = [ |
|
84
|
|
|
new ContentLengthPlugin, |
|
85
|
|
|
new BaseUriPlugin($baseUri), |
|
86
|
|
|
new HistoryPlugin($this->services[HttpHistory::class]) |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
$http = HttpClientDiscovery::find(); |
|
90
|
|
|
|
|
91
|
|
|
return new PluginClient($http, $plugins); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|