aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/helper/container_factory.php
blob: 9e372fecdeff6b0e024ba905e91bbca07aa0fbb2 (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
<?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\install\helper;

use phpbb\install\exception\cannot_build_container_exception;
use phpbb\language\language;
use phpbb\request\request;

class container_factory
{
	/**
	 * @var language
	 */
	protected $language;

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

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

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

	/**
	 * @var update_helper
	 */
	protected $update_helper;

	/**
	 * The full phpBB container
	 *
	 * @var \Symfony\Component\DependencyInjection\ContainerInterface
	 */
	protected $container;

	/**
	 * Constructor
	 *
	 * @param language 		$language			Language service
	 * @param request		$request			Request interface
	 * @param update_helper	$update_helper		Update helper
	 * @param string		$phpbb_root_path	Path to phpBB's root
	 * @param string		$php_ext			Extension of PHP files
	 */
	public function __construct(language $language, request $request, update_helper $update_helper, $phpbb_root_path, $php_ext)
	{
		$this->language			= $language;
		$this->request			= $request;
		$this->update_helper	= $update_helper;
		$this->phpbb_root_path	= $phpbb_root_path;
		$this->php_ext			= $php_ext;
		$this->container		= null;
	}

	/**
	 * Container getter
	 *
	 * @param null|string	$service_name	Name of the service to return
	 *
	 * @return \Symfony\Component\DependencyInjection\ContainerInterface|Object	phpBB's dependency injection container
	 * 																			or the service specified in $service_name
	 *
	 * @throws \phpbb\install\exception\cannot_build_container_exception							When container cannot be built
	 * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException			If the service is not defined
	 * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException	When a circular reference is detected
	 * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException			When the service is not defined
	 */
	public function get($service_name = null)
	{
		// Check if container was built, if not try to build it
		if ($this->container === null)
		{
			$this->build_container();
		}

		return ($service_name === null) ? $this->container : $this->container->get($service_name);
	}

	/**
	 * Returns the specified parameter from the container
	 *
	 * @param string	$param_name
	 *
	 * @return mixed
	 *
	 * @throws \phpbb\install\exception\cannot_build_container_exception	When container cannot be built
	 */
	public function get_parameter($param_name)
	{
		// Check if container was built, if not try to build it
		if ($this->container === null)
		{
			$this->build_container();
		}

		return $this->container->getParameter($param_name);
	}

	/**
	 * Build dependency injection container
	 *
	 * @throws \phpbb\install\exception\cannot_build_container_exception	When container cannot be built
	 */
	protected function build_container()
	{
		// If the container has been already built just return.
		// Although this should never happen
		if ($this->container instanceof \Symfony\Component\DependencyInjection\ContainerInterface)
		{
			return;
		}

		// Check whether container can be built
		// We need config.php for that so let's check if it has been set up yet
		if (!filesize($this->phpbb_root_path . 'config.' . $this->php_ext))
		{
			throw new cannot_build_container_exception();
		}

		$phpbb_config_php_file = new \phpbb\config_php_file($this->phpbb_root_path, $this->php_ext);
		$phpbb_container_builder = new \phpbb\di\container_builder($this->phpbb_root_path, $this->php_ext);

		// For BC with functions that we need during install
		global $phpbb_container, $table_prefix;

		$disable_super_globals = $this->request->super_globals_disabled();

		// This is needed because container_builder::get_env_parameters() uses $_SERVER
		if ($disable_super_globals)
		{
			$this->request->enable_super_globals();
		}

		$other_config_path = $this->phpbb_root_path . 'install/update/new/config';
		$config_path = (is_dir($other_config_path)) ? $other_config_path : $this->phpbb_root_path . 'config';

		$this->container = $phpbb_container_builder
			->with_environment('production')
			->with_config($phpbb_config_php_file)
			->with_config_path($config_path)
			->without_compiled_container()
			->get_container();

		// Setting request is required for the compatibility globals as those are generated from
		// this container
		if (!$this->container->isFrozen())
		{
			$this->container->register('request')->setSynthetic(true);
			$this->container->register('language')->setSynthetic(true);
		}

		$this->container->set('request', $this->request);
		$this->container->set('language', $this->language);

		$this->container->compile();

		$phpbb_container = $this->container;
		$table_prefix = $phpbb_config_php_file->get('table_prefix');

		// Restore super globals to previous state
		if ($disable_super_globals)
		{
			$this->request->disable_super_globals();
		}

		// Get compatibilty globals and constants
		$this->update_helper->include_file('includes/compatibility_globals.' . $this->php_ext);

		register_compatibility_globals();

		$this->update_helper->include_file('includes/constants.' . $this->php_ext);
	}
}