|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Panamax\Adapters\League; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\DefinitionContainerInterface; |
|
6
|
|
|
use League\Container\ServiceProvider\ServiceProviderInterface; |
|
7
|
|
|
use Panamax\Adapters\AbstractContainerAdapter; |
|
8
|
|
|
use Panamax\Contracts\ContainerAdapterInterface; |
|
9
|
|
|
use Panamax\Contracts\ProviderContainerInterface; |
|
10
|
|
|
use Panamax\Contracts\ServiceCreatorInterface; |
|
11
|
|
|
use Panamax\Contracts\ServiceFactoryInterface; |
|
12
|
|
|
use Panamax\Exceptions\InvalidServiceProviderException; |
|
13
|
|
|
use Panamax\Traits\ServiceCreatorTrait; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
use TypeError; |
|
16
|
|
|
|
|
17
|
|
|
class LeagueAdapter extends AbstractContainerAdapter implements ContainerAdapterInterface, ProviderContainerInterface, ServiceCreatorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use ServiceCreatorTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var DefinitionContainerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected ContainerInterface $container; |
|
25
|
|
|
|
|
26
|
3 |
|
public function __construct(DefinitionContainerInterface $container) |
|
27
|
|
|
{ |
|
28
|
3 |
|
$this->container = $container; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public function bind(string $id, $concrete = null, ?bool $shared = null) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$definition = $this->container->add($id, $concrete); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
3 |
|
if (isset($shared)) { |
|
36
|
3 |
|
$definition->setShared($shared); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function share(string $id, $concrete = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->container->addShared($id, $concrete); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function alias(string $id, string $alias) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->container->add($alias, $id); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function addServiceProvider(string $provider, ?array $args = null) |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$this->container->addServiceProvider(new $provider()); |
|
|
|
|
|
|
54
|
|
|
} catch (TypeError $e) { |
|
55
|
|
|
throw new InvalidServiceProviderException( |
|
56
|
|
|
__METHOD__, |
|
57
|
|
|
ServiceProviderInterface::class |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function addServiceProviders(iterable $providers) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($providers as $provider) { |
|
65
|
|
|
$this->addServiceProvider($provider); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function createServices(iterable $services) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($services as $service) { |
|
72
|
|
|
$id = $service['id']; |
|
73
|
|
|
$factory = $service['factory']; |
|
74
|
|
|
$args = $service['args'] ?? []; |
|
75
|
|
|
|
|
76
|
|
|
unset($service['id'], $service['factory'], $service['args']); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->isValidFactory($factory)) { |
|
79
|
|
|
$factory = $this->getResolvedFactory($factory); |
|
80
|
|
|
} else { |
|
81
|
|
|
throw $this->invalidFactoryException($id, $factory); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->createService($id, $factory, $args, $service); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function createService(string $id, ServiceFactoryInterface $factory, array $args = [], $options = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$definition = $this->container->add( |
|
91
|
|
|
$id, |
|
92
|
|
|
$factory->pledge($this->container, $args) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
if ($shared = $options['shared'] ?? null) { |
|
96
|
|
|
$definition->setShared($shared); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
array_map([$definition, 'addTag'], $options['tags'] ?? []); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|