1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Container\Provider; |
15
|
|
|
|
16
|
|
|
use Valkyrja\Application\Config; |
17
|
|
|
use Valkyrja\Attribute\Contract\Attributes; |
18
|
|
|
use Valkyrja\Container\Collector\AttributeCollector; |
19
|
|
|
use Valkyrja\Container\Collector\Contract\Collector; |
20
|
|
|
use Valkyrja\Container\Contract\Container; |
21
|
|
|
use Valkyrja\Container\Contract\Service; |
22
|
|
|
use Valkyrja\Container\Data; |
23
|
|
|
use Valkyrja\Container\Exception\InvalidArgumentException; |
24
|
|
|
use Valkyrja\Container\Support\Provider; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ServiceProvider. |
28
|
|
|
* |
29
|
|
|
* @author Melech Mizrachi |
30
|
|
|
*/ |
31
|
|
|
final class ServiceProvider extends Provider |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
|
|
public static function publishers(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
Collector::class => [self::class, 'publishAttributesCollector'], |
40
|
|
|
Data::class => [self::class, 'publishData'], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public static function provides(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
Collector::class, |
51
|
|
|
Data::class, |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Publish the attributes service. |
57
|
|
|
*/ |
58
|
|
|
public static function publishAttributesCollector(Container $container): void |
59
|
|
|
{ |
60
|
|
|
$container->setSingleton( |
61
|
|
|
Collector::class, |
62
|
|
|
new AttributeCollector( |
63
|
|
|
$container->getSingleton(Attributes::class) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Publish the data service. |
70
|
|
|
*/ |
71
|
|
|
public static function publishData(Container $container): void |
72
|
|
|
{ |
73
|
|
|
$config = $container->getSingleton(Config::class); |
74
|
|
|
|
75
|
|
|
$collector = $container->getSingleton(Collector::class); |
76
|
|
|
|
77
|
|
|
foreach ($collector->getServices(...$config->services) as $service) { |
78
|
|
|
$class = $service->dispatch->getClass(); |
79
|
|
|
|
80
|
|
|
if (! is_a($class, Service::class, true)) { |
81
|
|
|
throw new InvalidArgumentException("Class for $class must implement " . Service::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($service->isSingleton) { |
85
|
|
|
$container->bindSingleton($service->serviceId, $class); |
86
|
|
|
|
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$container->bind($service->serviceId, $class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($collector->getAliases(...$config->aliases) as $service) { |
94
|
|
|
$container->bindAlias($service->dispatch->getClass(), $service->serviceId); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$container->setSingleton( |
98
|
|
|
Data::class, |
99
|
|
|
new Data( |
100
|
|
|
providers: $config->providers |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|