1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
6
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
7
|
|
|
use ApiClients\Foundation\Transport\ClientInterface; |
|
|
|
|
8
|
|
|
use ApiClients\Foundation\Transport\Factory as TransportFactory; |
9
|
|
|
use ApiClients\Tools\CommandBus\CommandBusInterface; |
10
|
|
|
use ApiClients\Tools\CommandBus\Factory as CommandBusFactory; |
11
|
|
|
use DI\ContainerBuilder; |
12
|
|
|
use Interop\Container\ContainerInterface; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use React\EventLoop\LoopInterface; |
15
|
|
|
|
16
|
|
|
final class Factory |
17
|
|
|
{ |
18
|
|
|
public static function create( |
19
|
|
|
LoopInterface $loop, |
20
|
|
|
array $options = [] |
21
|
|
|
): Client { |
22
|
|
|
return new Client( |
23
|
|
|
self::createContainer($loop, $options) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private static function createContainer(LoopInterface $loop, array $options): ContainerInterface |
28
|
|
|
{ |
29
|
|
|
$container = new ContainerBuilder(); |
30
|
|
|
|
31
|
|
|
$container->addDefinitions([ |
32
|
|
|
LoopInterface::class => $loop, |
33
|
|
|
ClientInterface::class => function (ContainerInterface $container, LoopInterface $loop) use ($options) { |
34
|
|
|
return self::createTransport($container, $loop, $options); |
35
|
|
|
}, |
36
|
|
|
Hydrator::class => function (ContainerInterface $container) use ($options) { |
37
|
|
|
return self::createHydrator($container, $options); |
38
|
|
|
}, |
39
|
|
|
CommandBusInterface::class => function (ContainerInterface $container) { |
40
|
|
|
return CommandBusFactory::create($container); |
41
|
|
|
}, |
42
|
|
|
]); |
43
|
|
|
$container->addDefinitions($options[Options::CONTAINER_DEFINITIONS] ?? []); |
44
|
|
|
|
45
|
|
|
return $container->build(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
private static function createTransport( |
|
|
|
|
49
|
|
|
ContainerInterface $container, |
50
|
|
|
LoopInterface $loop, |
51
|
|
|
array $options = [] |
52
|
|
|
): ClientInterface { |
53
|
|
|
if (!isset($options[Options::TRANSPORT_OPTIONS])) { |
54
|
|
|
throw new InvalidArgumentException('Missing Transport options'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return TransportFactory::create($container, $loop, $options[Options::TRANSPORT_OPTIONS]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
private static function createHydrator(ContainerInterface $container, array $options = []) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
63
|
|
|
throw new InvalidArgumentException('Missing Hydrator options'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: