1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
8
|
|
|
use TheCodingMachine\ServiceProvider\Registry; |
9
|
|
|
use TheCodingMachine\ServiceProvider\RegistryInterface; |
10
|
|
|
|
11
|
|
|
class InteropServiceProviderBridgeBundle extends Bundle implements RegistryProviderInterface |
12
|
|
|
{ |
13
|
|
|
private $serviceProviders; |
14
|
|
|
private $useDiscovery; |
15
|
|
|
private $id; |
16
|
|
|
|
17
|
|
|
private static $count = 0; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array $serviceProviders An array of service providers, in the format specified in thecodingmachine/service-provider-registry: https://github.com/thecodingmachine/service-provider-registry#how-does-it-work |
21
|
|
|
* @param bool $useDiscovery |
22
|
|
|
*/ |
23
|
|
|
public function __construct(array $serviceProviders = [], $useDiscovery = true) |
24
|
|
|
{ |
25
|
|
|
$this->serviceProviders = $serviceProviders; |
26
|
|
|
$this->useDiscovery = $useDiscovery; |
27
|
|
|
$this->id = self::$count; |
28
|
|
|
$this->name = $this->getName() . $this->id; |
29
|
|
|
|
30
|
|
|
self::$count++; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function build(ContainerBuilder $container) |
34
|
|
|
{ |
35
|
|
|
$container->addCompilerPass(new ServiceProviderCompilationPass($this->id, $this)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* At boot time, let's fill the container with the registry. |
40
|
|
|
*/ |
41
|
|
|
public function boot() |
42
|
|
|
{ |
43
|
|
|
$registryServiceName = 'service_provider_registry_'.$this->id; |
44
|
|
|
$this->container->set($registryServiceName, $this->getRegistry($this->container)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* When the Kernel shuts down, this bundle's static identifier must be |
49
|
|
|
* reset, otherwise it may lead to incorrect identifier binding for the |
50
|
|
|
* compiled container. |
51
|
|
|
*/ |
52
|
|
|
public function shutdown() |
53
|
|
|
{ |
54
|
|
|
self::$count = 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ContainerInterface $container |
59
|
|
|
* @return RegistryInterface |
60
|
|
|
*/ |
61
|
|
|
public function getRegistry(ContainerInterface $container) |
62
|
|
|
{ |
63
|
|
|
$discovery = null; |
64
|
|
|
if ($this->useDiscovery) { |
65
|
|
|
$discovery = \TheCodingMachine\Discovery\Discovery::getInstance(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// In parallel, let's merge the registry: |
69
|
|
|
$registry = new Registry($this->serviceProviders, $discovery); |
70
|
|
|
return $registry; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|