-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDefaultInitializer.php
More file actions
116 lines (98 loc) · 4.04 KB
/
DefaultInitializer.php
File metadata and controls
116 lines (98 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* Copyright (c) Tyler Sommer
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Nice\DependencyInjection\ContainerInitializer;
use Nice\Application;
use Nice\DependencyInjection\CompilerAwareExtensionInterface;
use Nice\DependencyInjection\ConfigurationProviderInterface;
use Nice\DependencyInjection\ContainerInitializerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
/**
* Creates and initializes a Service Container, ready for use by the Application
*/
class DefaultInitializer implements ContainerInitializerInterface
{
/**
* @var ConfigurationProviderInterface
*/
private $configProvider;
/**
* Constructor
*
* @param ConfigurationProviderInterface $configProvider
*/
public function __construct(ConfigurationProviderInterface $configProvider)
{
$this->configProvider = $configProvider;
}
/**
* Returns a fully built, ready to use Container
*
* @param Application $application
* @param array|ExtensionInterface[] $extensions
* @param array|CompilerPassInterface[] $compilerPasses
*
* @return ContainerInterface
*/
public function initializeContainer(Application $application, array $extensions = array(), array $compilerPasses = array())
{
$container = $this->getContainerBuilder();
$container->addObjectResource($application);
$container->setParameter('app.env', $application->getEnvironment());
$container->setParameter('app.debug', $application->isDebug());
$container->setParameter('app.cache', $application->isCacheEnabled());
$container->setParameter('app.root_dir', $application->getRootDir());
$container->setParameter('app.cache_dir', $application->getCacheDir());
$container->setParameter('app.log_dir', $application->getLogDir());
$container->register('event_dispatcher', 'Symfony\Component\EventDispatcher\EventDispatcher')
->setPublic(true);
$container->register('app', 'Symfony\Component\HttpKernel\HttpKernelInterface')
->setSynthetic(true);
$container->register('request', 'Symfony\Componenet\HttpKernel\Request')
->setSynthetic(true);
$extensionAliases = array();
foreach ($extensions as $extension) {
$container->registerExtension($extension);
$extensionAliases[] = $extension->getAlias();
if ($extension instanceof CompilerAwareExtensionInterface) {
$passes = $extension->getCompilerPasses();
if (!is_array($passes)) {
$passes = array($passes);
}
$compilerPasses = array_merge($compilerPasses, $passes);
}
}
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensionAliases));
$container->addCompilerPass(new RegisterListenersPass());
foreach ($compilerPasses as $pass) {
if (is_array($pass)) {
$container->addCompilerPass($pass[0], $pass[1]);
} else {
$container->addCompilerPass($pass);
}
}
$this->configProvider->load($container);
$container->compile();
return $container;
}
/**
* Gets a new ContainerBuilder instance used to build the service container.
*
* @return ContainerBuilder
*/
protected function getContainerBuilder()
{
return new ContainerBuilder();
}
}