aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/di/container_builder.php
blob: 433847b2857fccc19819d064c0ebc29665f54e70 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\di;

use phpbb\filesystem\filesystem;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;

class container_builder
{
	/**
	 * @var string The environment to use.
	 */
	protected $environment;

	/**
	 * @var string phpBB Root Path
	 */
	protected $phpbb_root_path;

	/**
	 * @var string php file extension
	 */
	protected $php_ext;

	/**
	 * The container under construction
	 *
	 * @var ContainerBuilder
	 */
	protected $container;

	/**
	 * Indicates whether extensions should be used (default to true).
	 *
	 * @var bool
	 */
	protected $use_extensions = true;

	/**
	 * Defines a custom path to find the configuration of the container (default to $this->phpbb_root_path . 'config')
	 *
	 * @var string
	 */
	protected $config_path = null;

	/**
	 * Indicates whether the container should be dumped to the filesystem (default to true).
	 *
	 * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
	 *
	 * @var bool
	 */
	protected $use_cache = true;

	/**
	 * Indicates if the container should be compiled automatically (default to true).
	 *
	 * @var bool
	 */
	protected $compile_container = true;

	/**
	 * Custom parameters to inject into the container.
	 *
	 * Default to:
	 * 	array(
	 * 		'core.root_path', $this->phpbb_root_path,
	 * 		'core.php_ext', $this->php_ext,
	 * );
	 *
	 * @var array
	 */
	protected $custom_parameters = [];

	/**
	 * @var \phpbb\config_php_file
	 */
	protected $config_php_file;

	/**
	 * @var string
	 */
	protected $cache_dir;

	/**
	 * @var array
	 */
	private $container_extensions;

	/** @var \Exception */
	private $build_exception;

	/**
	 * Constructor
	 *
	 * @param string $phpbb_root_path Path to the phpbb includes directory.
	 * @param string $php_ext php file extension
	 */
	public function __construct($phpbb_root_path, $php_ext)
	{
		$this->phpbb_root_path = $phpbb_root_path;
		$this->php_ext = $php_ext;
	}

	/**
	 * Build and return a new Container respecting the current configuration
	 *
	 * @return \phpbb_cache_container|ContainerBuilder
	 */
	public function get_container()
	{
		try
		{
			$container_filename = $this->get_container_filename();
			$config_cache = new ConfigCache($container_filename, defined('DEBUG'));
			if ($this->use_cache && $config_cache->isFresh())
			{
				require($config_cache->getPath());
				$this->container = new \phpbb_cache_container();
			}
			else
			{
				$this->container_extensions = array(new extension\core($this->get_config_path()));

				if ($this->use_extensions)
				{
					$this->load_extensions();
				}

				// Inject the config
				if ($this->config_php_file)
				{
					$this->container_extensions[] = new extension\config($this->config_php_file);
				}

				$this->container = $this->create_container($this->container_extensions);

				// Easy collections through tags
				$this->container->addCompilerPass(new pass\collection_pass());

				// Event listeners "phpBB style"
				$this->container->addCompilerPass(new RegisterListenersPass('dispatcher', 'event.listener_listener', 'event.listener'));

				// Event listeners "Symfony style"
				$this->container->addCompilerPass(new RegisterListenersPass('dispatcher'));

				if ($this->use_extensions)
				{
					$this->register_ext_compiler_pass();
				}

				$filesystem = new filesystem();
				$loader     = new YamlFileLoader($this->container, new FileLocator($filesystem->realpath($this->get_config_path())));
				$loader->load($this->container->getParameter('core.environment') . '/config.yml');

				$this->inject_custom_parameters();

				if ($this->compile_container)
				{
					$this->container->compile();

					if ($this->use_cache)
					{
						$this->dump_container($config_cache);
					}
				}
			}

			if ($this->compile_container && $this->config_php_file)
			{
				$this->container->set('config.php', $this->config_php_file);
			}

			return $this->container;
		}
		catch (\Exception $e)
		{
			// Don't try to recover if we are in the development environment
			if ($this->get_environment() === 'development')
			{
				throw $e;
			}

			if ($this->build_exception === null)
			{
				$this->build_exception = $e;

				return $this
					->without_extensions()
					->without_cache()
					->with_custom_parameters(array_merge($this->custom_parameters, [
						'container_exception' => $e,
					]))
					->get_container();
			}
			else
			{
				// Rethrow the original exception if it's still failing
				throw $this->build_exception;
			}
		}
	}

	/**
	 * Enable the extensions.
	 *
	 * @param string $environment The environment to use
	 * @return $this
	 */
	public function with_environment($environment)
	{
		$this->environment = $environment;

		return $this;
	}

	/**
	 * Enable the extensions.
	 *
	 * @return $this
	 */
	public function with_extensions()
	{
		$this->use_extensions = true;

		return $this;
	}

	/**
	 * Disable the extensions.
	 *
	 * @return $this
	 */
	public function without_extensions()
	{
		$this->use_extensions = false;

		return $this;
	}

	/**
	 * Enable the caching of the container.
	 *
	 * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
	 *
	 * @return $this
	 */
	public function with_cache()
	{
		$this->use_cache = true;

		return $this;
	}

	/**
	 * Disable the caching of the container.
	 *
	 * @return $this
	 */
	public function without_cache()
	{
		$this->use_cache = false;

		return $this;
	}

	/**
	 * Set the cache directory.
	 *
	 * @param string $cache_dir The cache directory.
	 * @return $this
	 */
	public function with_cache_dir($cache_dir)
	{
		$this->cache_dir = $cache_dir;

		return $this;
	}

	/**
	 * Enable the compilation of the container.
	 *
	 * @return $this
	 */
	public function with_compiled_container()
	{
		$this->compile_container = true;

		return $this;
	}

	/**
	 * Disable the compilation of the container.
	 *
	 * @return $this
	 */
	public function without_compiled_container()
	{
		$this->compile_container = false;

		return $this;
	}

	/**
	 * Set a custom path to find the configuration of the container.
	 *
	 * @param string $config_path
	 * @return $this
	 */
	public function with_config_path($config_path)
	{
		$this->config_path = $config_path;

		return $this;
	}

	/**
	 * Set custom parameters to inject into the container.
	 *
	 * @param array $custom_parameters
	 * @return $this
	 */
	public function with_custom_parameters($custom_parameters)
	{
		$this->custom_parameters = $custom_parameters;

		return $this;
	}

	/**
	 * Set custom parameters to inject into the container.
	 *
	 * @param \phpbb\config_php_file $config_php_file
	 * @return $this
	 */
	public function with_config(\phpbb\config_php_file $config_php_file)
	{
		$this->config_php_file = $config_php_file;

		return $this;
	}

	/**
	 * Returns the path to the container configuration (default: root_path/config)
	 *
	 * @return string
	 */
	protected function get_config_path()
	{
		return $this->config_path ?: $this->phpbb_root_path . 'config';
	}

	/**
	 * Returns the path to the cache directory (default: root_path/cache/environment).
	 *
	 * @return string Path to the cache directory.
	 */
	protected function get_cache_dir()
	{
		return $this->cache_dir ?: $this->phpbb_root_path . 'cache/' . $this->get_environment() . '/';
	}

	/**
	 * Load the enabled extensions.
	 */
	protected function load_extensions()
	{
		if ($this->config_php_file !== null)
		{
			// Build an intermediate container to load the ext list from the database
			$container_builder = new container_builder($this->phpbb_root_path, $this->php_ext);
			$ext_container = $container_builder
				->without_cache()
				->without_extensions()
				->with_config($this->config_php_file)
				->with_config_path($this->get_config_path())
				->with_environment('production')
				->without_compiled_container()
				->get_container()
			;

			$ext_container->register('cache.driver', '\\phpbb\\cache\\driver\\dummy');
			$ext_container->compile();

			$extensions = $ext_container->get('ext.manager')->all_enabled();

			// Load each extension found
			foreach ($extensions as $ext_name => $path)
			{
				$extension_class = '\\' . str_replace('/', '\\', $ext_name) . '\\di\\extension';

				if (!class_exists($extension_class))
				{
					$extension_class = '\\phpbb\\extension\\di\\extension_base';
				}

				$this->container_extensions[] = new $extension_class($ext_name, $path);

				// Load extension autoloader
				$filename = $path . 'vendor/autoload.php';
				if (file_exists($filename))
				{
					require $filename;
				}
			}
		}
		else
		{
			// To load the extensions we need the database credentials.
			// Automatically disable the extensions if we don't have them.
			$this->use_extensions = false;
		}
	}

	/**
	 * Dump the container to the disk.
	 *
	 * @param ConfigCache $cache The config cache
	 */
	protected function dump_container($cache)
	{
		try
		{
			$dumper                = new PhpDumper($this->container);
			$cached_container_dump = $dumper->dump(array(
				'class'      => 'phpbb_cache_container',
				'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
			));

			$cache->write($cached_container_dump, $this->container->getResources());
		}
		catch (IOException $e)
		{
			// Don't fail if the cache isn't writeable
		}
	}

	/**
	 * Create the ContainerBuilder object
	 *
	 * @param array $extensions Array of Container extension objects
	 * @return ContainerBuilder object
	 */
	protected function create_container(array $extensions)
	{
		$container = new ContainerBuilder(new ParameterBag($this->get_core_parameters()));

		$extensions_alias = array();

		foreach ($extensions as $extension)
		{
			$container->registerExtension($extension);
			$extensions_alias[] = $extension->getAlias();
		}

		$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions_alias));

		return $container;
	}

	/**
	 * Inject the customs parameters into the container
	 */
	protected function inject_custom_parameters()
	{
		foreach ($this->custom_parameters as $key => $value)
		{
			$this->container->setParameter($key, $value);
		}

	}

	/**
	 * Returns the core parameters.
	 *
	 * @return array An array of core parameters
	 */
	protected function get_core_parameters()
	{
		return array_merge(
			array(
				'core.root_path'     => $this->phpbb_root_path,
				'core.php_ext'       => $this->php_ext,
				'core.environment'   => $this->get_environment(),
				'core.debug'         => defined('DEBUG') ? DEBUG : false,
			),
			$this->get_env_parameters()
		);
	}

	/**
	 * Gets the environment parameters.
	 *
	 * Only the parameters starting with "PHPBB__" are considered.
	 *
	 * @return array An array of parameters
	 */
	protected function get_env_parameters()
	{
		$parameters = array();
		foreach ($_SERVER as $key => $value)
		{
			if (0 === strpos($key, 'PHPBB__'))
			{
				$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
			}
		}

		return $parameters;
	}

	/**
	 * Get the filename under which the dumped container will be stored.
	 *
	 * @return string Path for dumped container
	 */
	protected function get_container_filename()
	{
		return $this->get_cache_dir() . 'container_' . md5($this->phpbb_root_path) . '.' . $this->php_ext;
	}

	/**
	 * Return the name of the current environment.
	 *
	 * @return string
	 */
	protected function get_environment()
	{
		return $this->environment ?: PHPBB_ENVIRONMENT;
	}

	private function register_ext_compiler_pass()
	{
		$finder = new Finder();
		$finder
			->name('*_pass.php')
			->path('di/pass')
			->files()
			->ignoreDotFiles(true)
			->ignoreUnreadableDirs(true)
			->ignoreVCS(true)
			->followLinks()
			->in($this->phpbb_root_path . 'ext/')
		;

		/** @var \SplFileInfo $pass */
		foreach ($finder as $pass)
		{
			$filename = $pass->getPathname();
			$filename = substr($filename, 0, -strlen('.' . $pass->getExtension()));
			$filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename);
			$className = preg_replace('#^.*ext/#', '', $filename);
			$className = '\\' . str_replace('/', '\\', $className);

			if (class_exists($className) && in_array('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface', class_implements($className), true))
			{
				$this->container->addCompilerPass(new $className());
			}
		}
	}
}