aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/functions_container.php
blob: a3ed21c35b9f25990d25e55cab282278c621a990 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
*
* @package phpBB3
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* Create the ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object
*/
function phpbb_create_container(array $extensions, $phpbb_root_path, $php_ext)
{
	$container = new ContainerBuilder();

	foreach ($extensions as $extension)
	{
		$container->registerExtension($extension);
		$container->loadFromExtension($extension->getAlias());
	}

	$container->setParameter('core.root_path', $phpbb_root_path);
	$container->setParameter('core.php_ext', $php_ext);

	return $container;
}

/**
* Create installer container
*
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object
*/
function phpbb_create_install_container($phpbb_root_path, $php_ext)
{
	$core = new phpbb_di_extension_core($phpbb_root_path);
	$container = phpbb_create_container(array($core), $phpbb_root_path, $php_ext);

	$container->setParameter('core.root_path', $phpbb_root_path);
	$container->setParameter('core.php_ext', $php_ext);
	$container->setParameter('core.table_prefix', '');

	$container->register('dbal.conn')->setSynthetic(true);

	$container->setAlias('cache.driver', 'cache.driver.install');

	$container->compile();

	return $container;
}

/**
* Create a compiled ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_compiled_container(array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
	// Create a temporary container for access to the ext.manager service
	$tmp_container = phpbb_create_container($extensions, $phpbb_root_path, $php_ext);
	$tmp_container->compile();

	// XXX stop writing to global $cache when
	// http://tracker.phpbb.com/browse/PHPBB3-11203 is fixed
	$GLOBALS['cache'] = $tmp_container->get('cache');
	$installed_exts = $tmp_container->get('ext.manager')->all_enabled();

	// Now pass the enabled extension paths into the ext compiler extension
	$extensions[] = new phpbb_di_extension_ext($installed_exts);

	// Create the final container to be compiled and cached
	$container = phpbb_create_container($extensions, $phpbb_root_path, $php_ext);

	// Compile the container
	foreach ($passes as $pass)
	{
		$container->addCompilerPass($pass);
	}
	$container->compile();

	return $container;
}

/**
* Create a compiled and dumped ContainerBuilder object
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_dumped_container(array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
	// Check for our cached container; if it exists, use it
	$container_filename = phpbb_container_filename($phpbb_root_path, $php_ext);
	if (file_exists($container_filename))
	{
		require($container_filename);
		return new phpbb_cache_container();
	}

	$container = phpbb_create_compiled_container($extensions, $passes, $phpbb_root_path, $php_ext);

	// Lastly, we create our cached container class
	$dumper = new PhpDumper($container);
	$cached_container_dump = $dumper->dump(array(
		'class'         => 'phpbb_cache_container',
		'base_class'    => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
	));

	file_put_contents($container_filename, $cached_container_dump);

	return $container;
}

/**
* Create an environment-specific ContainerBuilder object
*
* If debug is enabled, the container is re-compiled every time.
* This ensures that the latest changes will always be reflected
* during development.
*
* Otherwise it will get the existing dumped container and use
* that one instead.
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_dumped_container_unless_debug(array $extensions, array $passes, $phpbb_root_path, $php_ext)
{
	$container_factory = defined('DEBUG') ? 'phpbb_create_compiled_container' : 'phpbb_create_dumped_container';
	return $container_factory($extensions, $passes, $phpbb_root_path, $php_ext);
}

/**
* Create a default ContainerBuilder object
*
* Contains the default configuration of the phpBB container.
*
* @param array $extensions Array of Container extension objects
* @param array $passes Array of Compiler Pass objects
* @return ContainerBuilder object (compiled)
*/
function phpbb_create_default_container($phpbb_root_path, $php_ext)
{
	return phpbb_create_dumped_container_unless_debug(
		array(
			new phpbb_di_extension_config($phpbb_root_path . 'config.' . $php_ext),
			new phpbb_di_extension_core($phpbb_root_path),
		),
		array(
			new phpbb_di_pass_collection_pass(),
			new phpbb_di_pass_kernel_pass(),
		),
		$phpbb_root_path,
		$php_ext
	);
}

/**
* Get the filename under which the dumped container will be stored.
*
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP Extension
* @return Path for dumped container
*/
function phpbb_container_filename($phpbb_root_path, $php_ext)
{
	$filename = str_replace(array('/', '.'), array('slash', 'dot'), $phpbb_root_path);
	return $phpbb_root_path . 'cache/container_' . $filename . '.' . $php_ext;
}