1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Cocotte; |
4
|
|
|
|
5
|
|
|
use Cocotte\DependencyInjection\ConsoleCommandPass; |
6
|
|
|
use Cocotte\DependencyInjection\LazyEnvironmentPass; |
7
|
|
|
use Cocotte\DependencyInjection\OptionProviderPass; |
8
|
|
|
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; |
9
|
|
|
use Symfony\Component\Config\FileLocator; |
10
|
|
|
use Symfony\Component\Console\Application as Console; |
11
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
15
|
|
|
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
|
18
|
|
|
final class Application |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ContainerInterface |
22
|
|
|
*/ |
23
|
|
|
private $container; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $serviceResource |
27
|
|
|
* @param CompilerPassInterface[] $extraPasses |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $serviceResource, array $extraPasses = []) |
31
|
|
|
{ |
32
|
|
|
$container = new ContainerBuilder(); |
33
|
|
|
$container->setProxyInstantiator(new RuntimeInstantiator()); |
34
|
|
|
|
35
|
|
|
$loader = new YamlFileLoader($container, new FileLocator()); |
36
|
|
|
$loader->load($serviceResource); |
37
|
|
|
|
38
|
|
|
$this->addCompilerPasses($extraPasses, $container); |
39
|
|
|
|
40
|
|
|
$container->compile(true); |
41
|
|
|
|
42
|
|
|
$this->container = $container; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function container(): ContainerInterface |
46
|
|
|
{ |
47
|
|
|
return $this->container; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function console(): Console |
51
|
|
|
{ |
52
|
|
|
return $this->container->get(Console::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function addCompilerPasses(array $extraPasses, ContainerBuilder $container): void |
56
|
|
|
{ |
57
|
|
|
$container->addCompilerPass(new LazyEnvironmentPass()); |
58
|
|
|
$container->addCompilerPass(new ConsoleCommandPass()); |
59
|
|
|
$container->addCompilerPass(new OptionProviderPass()); |
60
|
|
|
$container->addCompilerPass(new RegisterListenersPass( |
61
|
|
|
EventDispatcherInterface::class, |
62
|
|
|
'event.listener', |
63
|
|
|
'event.subscriber' |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
foreach ($extraPasses as $pass) { |
67
|
|
|
$container->addCompilerPass($pass); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |