|
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\Interop\ServiceProviderBridgeBundle\Exception\InvalidArgumentException; |
|
9
|
|
|
use TheCodingMachine\ServiceProvider\Registry; |
|
10
|
|
|
use TheCodingMachine\ServiceProvider\RegistryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class InteropServiceProviderBridgeBundle extends Bundle implements RegistryProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $serviceProviders; |
|
15
|
|
|
private $useDiscovery; |
|
16
|
|
|
private $id; |
|
17
|
|
|
|
|
18
|
|
|
private static $count = 0; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @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 |
|
22
|
|
|
* @param bool $useDiscovery |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(array $serviceProviders = [], $useDiscovery = true) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->serviceProviders = $serviceProviders; |
|
27
|
|
|
$this->useDiscovery = $useDiscovery; |
|
28
|
|
|
$this->id = self::$count; |
|
29
|
|
|
self::$count++; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function build(ContainerBuilder $container) |
|
33
|
|
|
{ |
|
34
|
|
|
$container->addCompilerPass(new ServiceProviderCompilationPass($this->id, $this)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* At boot time, let's fill the container with the registry. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function boot() |
|
41
|
|
|
{ |
|
42
|
|
|
$registryServiceName = 'service_provider_registry_'.$this->id; |
|
43
|
|
|
$this->container->set($registryServiceName, $this->getRegistry($this->container)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ContainerInterface $container |
|
48
|
|
|
* @return RegistryInterface |
|
49
|
|
|
* @throws InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getRegistry(ContainerInterface $container) |
|
52
|
|
|
{ |
|
53
|
|
|
$discovery = null; |
|
54
|
|
|
if ($this->useDiscovery) { |
|
55
|
|
|
$discovery = \TheCodingMachine\Discovery\Discovery::getInstance(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// In parallel, let's merge the registry: |
|
59
|
|
|
$registry = new Registry($this->serviceProviders, $discovery); |
|
60
|
|
|
return $registry; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|