|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Container\Container as GacelaContainer; |
|
8
|
|
|
use Gacela\Framework\Bootstrap\ContainerConfigurationInterface; |
|
9
|
|
|
use Gacela\Framework\Config\Config; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is a decorator class to simplify the usage of the decoupled Container |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Container extends GacelaContainer implements ContainerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
public static function withConfig(Config $config): self |
|
17
|
|
|
{ |
|
18
|
|
|
return self::withContainerConfiguration( |
|
19
|
|
|
$config->getSetupGacela(), |
|
20
|
|
|
$config->getFactory()->createGacelaFileConfig()->getBindings(), |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array<class-string,class-string|object|callable> $bindings |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
public static function withContainerConfiguration( |
|
28
|
|
|
ContainerConfigurationInterface $containerConfig, |
|
29
|
|
|
array $bindings, |
|
30
|
|
|
): self { |
|
31
|
|
|
$container = new self( |
|
32
|
|
|
$bindings, |
|
33
|
|
|
$containerConfig->getServicesToExtend(), |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
// Register factory services |
|
37
|
|
|
foreach ($containerConfig->getFactories() as $id => $factory) { |
|
38
|
|
|
$container->set($id, $container->factory($factory)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Register protected services |
|
42
|
|
|
foreach ($containerConfig->getProtectedServices() as $id => $service) { |
|
43
|
|
|
$container->set($id, $container->protect($service)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Register aliases |
|
47
|
|
|
foreach ($containerConfig->getAliases() as $alias => $id) { |
|
48
|
|
|
$container->alias($alias, $id); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Register contextual bindings |
|
52
|
|
|
foreach ($containerConfig->getContextualBindings() as $concrete => $needs) { |
|
53
|
|
|
foreach ($needs as $abstract => $implementation) { |
|
54
|
|
|
/** @var class-string $concrete */ |
|
55
|
|
|
/** @var class-string $abstract */ |
|
56
|
|
|
$container->when($concrete)->needs($abstract)->give($implementation); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $container; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getLocator(): LocatorInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return Locator::getInstance($this); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|