Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
container_factory
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 get_parameter
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 build_container
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\install\helper;
15
16use phpbb\install\exception\cannot_build_container_exception;
17use phpbb\language\language;
18use phpbb\request\request;
19
20class container_factory
21{
22    /**
23     * @var language
24     */
25    protected $language;
26
27    /**
28     * @var string
29     */
30    protected $phpbb_root_path;
31
32    /**
33     * @var string
34     */
35    protected $php_ext;
36
37    /**
38     * @var request
39     */
40    protected $request;
41
42    /**
43     * @var update_helper
44     */
45    protected $update_helper;
46
47    /**
48     * The full phpBB container
49     *
50     * @var \Symfony\Component\DependencyInjection\ContainerInterface|null
51     */
52    protected $container;
53
54    /**
55     * Constructor
56     *
57     * @param language         $language            Language service
58     * @param request        $request            Request interface
59     * @param update_helper    $update_helper        Update helper
60     * @param string        $phpbb_root_path    Path to phpBB's root
61     * @param string        $php_ext            Extension of PHP files
62     */
63    public function __construct(language $language, request $request, update_helper $update_helper, $phpbb_root_path, $php_ext)
64    {
65        $this->language            = $language;
66        $this->request            = $request;
67        $this->update_helper    = $update_helper;
68        $this->phpbb_root_path    = $phpbb_root_path;
69        $this->php_ext            = $php_ext;
70        $this->container        = null;
71    }
72
73    /**
74     * Container getter
75     *
76     * @param null|string    $service_name    Name of the service to return
77     *
78     * @return \Symfony\Component\DependencyInjection\ContainerInterface|object|null    phpBB's dependency injection container
79     *                                                                             or the service specified in $service_name
80     *
81     * @throws cannot_build_container_exception                                                        When container cannot be built
82     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException            If the service is not defined
83     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException    When a circular reference is detected
84     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException            When the service is not defined
85     */
86    public function get($service_name = null)
87    {
88        // Check if container was built, if not try to build it
89        if ($this->container === null)
90        {
91            $this->build_container();
92        }
93
94        return $service_name === null ? $this->container : $this->container->get($service_name);
95    }
96
97    /**
98     * Returns the specified parameter from the container
99     *
100     * @param string    $param_name
101     *
102     * @return mixed
103     *
104     * @throws cannot_build_container_exception    When container cannot be built
105     */
106    public function get_parameter($param_name)
107    {
108        // Check if container was built, if not try to build it
109        if ($this->container === null)
110        {
111            $this->build_container();
112        }
113
114        return $this->container->getParameter($param_name);
115    }
116
117    /**
118     * Build dependency injection container
119     *
120     * @throws cannot_build_container_exception    When container cannot be built
121     */
122    protected function build_container()
123    {
124        // If the container has been already built just return.
125        // Although this should never happen
126        if ($this->container instanceof \Symfony\Component\DependencyInjection\ContainerInterface)
127        {
128            return;
129        }
130
131        // Check whether container can be built
132        // We need config.php for that so let's check if it has been set up yet
133        if (!filesize($this->phpbb_root_path . 'config.' . $this->php_ext))
134        {
135            throw new cannot_build_container_exception();
136        }
137
138        $disable_super_globals = $this->request->super_globals_disabled();
139
140        // This is needed because container_builder::get_env_parameters() uses $_SERVER
141        if ($disable_super_globals)
142        {
143            $this->request->enable_super_globals();
144        }
145
146        $phpbb_config_php_file = new \phpbb\config_php_file($this->phpbb_root_path, $this->php_ext);
147        $phpbb_container_builder = new \phpbb\di\container_builder($this->phpbb_root_path, $this->php_ext);
148
149        // For BC with functions that we need during install
150        global $phpbb_container, $table_prefix;
151
152        $other_config_path = $this->phpbb_root_path . 'install/update/new/config';
153        $config_path = (is_dir($other_config_path)) ? $other_config_path : $this->phpbb_root_path . 'config';
154
155        $this->container = $phpbb_container_builder
156            ->with_environment('production')
157            ->with_config($phpbb_config_php_file)
158            ->with_config_path($config_path)
159            ->without_compiled_container()
160            ->get_container();
161
162        // Setting request is required for the compatibility globals as those are generated from
163        // this container
164        if (!$this->container->isCompiled())
165        {
166            $this->container->register('request')->setSynthetic(true);
167            $this->container->register('language')->setSynthetic(true);
168        }
169
170        $this->container->set('request', $this->request);
171        $this->container->set('language', $this->language);
172
173        $this->container->compile();
174
175        $phpbb_container = $this->container;
176        $table_prefix = $phpbb_config_php_file->get('table_prefix');
177
178        // Restore super globals to previous state
179        if ($disable_super_globals)
180        {
181            $this->request->disable_super_globals();
182        }
183
184        // Get compatibility globals and constants
185        $this->update_helper->include_file('includes/compatibility_globals.' . $this->php_ext);
186
187        $this->update_helper->include_file('includes/constants.' . $this->php_ext);
188
189        register_compatibility_globals();
190    }
191}