1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Behapi; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\HelperContainer\Exception\ServiceNotFoundException; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
|
11
|
|
|
use Http\Message\StreamFactory; |
12
|
|
|
use Http\Message\MessageFactory; |
13
|
|
|
|
14
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
15
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
16
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
17
|
|
|
|
18
|
|
|
use Http\Client\Common\PluginClient; |
19
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
20
|
|
|
use Http\Client\Common\Plugin\HistoryPlugin; |
21
|
|
|
use Http\Client\Common\Plugin\ContentLengthPlugin; |
22
|
|
|
|
23
|
|
|
use Behapi\Http\PluginClientBuilder; |
24
|
|
|
use Behapi\HttpHistory\History as HttpHistory; |
25
|
|
|
|
26
|
|
|
use function in_array; |
27
|
|
|
use function array_key_exists; |
28
|
|
|
|
29
|
|
|
final 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
|
|
|
public function __construct(HttpHistory $history, string $baseUrl) |
38
|
|
|
{ |
39
|
|
|
$this->baseUrl = $baseUrl; |
40
|
|
|
$this->services[HttpHistory::class] = $history; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** {@inheritDoc} */ |
44
|
|
|
public function has($id) |
45
|
|
|
{ |
46
|
|
|
static $services = [ |
47
|
|
|
StreamFactory::class, |
48
|
|
|
MessageFactory::class, |
49
|
|
|
PluginClientBuilder::class, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
return in_array($id, $services); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** {@inheritDoc} */ |
56
|
|
|
public function get($id) |
57
|
|
|
{ |
58
|
|
|
if (array_key_exists($id, $this->services)) { |
59
|
|
|
return $this->services[$id]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
switch ($id) { |
63
|
|
|
case PluginClientBuilder::class: |
64
|
|
|
return $this->services[$id] = $this->getPluginClientBuilder(); |
65
|
|
|
|
66
|
|
|
case MessageFactory::class: |
67
|
|
|
return $this->services[$id] = MessageFactoryDiscovery::find(); |
68
|
|
|
|
69
|
|
|
case StreamFactory::class: |
70
|
|
|
return $this->services[$id] = StreamFactoryDiscovery::find(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new ServiceNotFoundException("Service {$id} is not available", $id); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getPluginClientBuilder(): PluginClientBuilder |
77
|
|
|
{ |
78
|
|
|
$builder = new PluginClientBuilder; |
79
|
|
|
$uriFactory = UriFactoryDiscovery::find(); |
80
|
|
|
$baseUri = $uriFactory->createUri($this->baseUrl); |
81
|
|
|
|
82
|
|
|
$builder->addPlugin(new ContentLengthPlugin); |
83
|
|
|
$builder->addPlugin(new BaseUriPlugin($baseUri)); |
84
|
|
|
$builder->addPlugin(new HistoryPlugin($this->services[HttpHistory::class])); |
85
|
|
|
|
86
|
|
|
return $builder; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|