Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ServiceContainer.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Base;
8
9 use eZ\Publish\API\Container;
10 use Symfony\Component\Config\ConfigCache;
11 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
12 use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
13 use Symfony\Component\DependencyInjection\ContainerBuilder;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use RuntimeException;
16
21 class ServiceContainer implements Container
22 {
28 protected $containerClassName = 'EzPublishPublicAPIServiceContainer';
29
35 protected $innerContainer;
36
42 protected $installDir;
43
49 protected $cacheDir;
50
56 protected $debug;
57
63 protected $bypassCache;
64
73 public function __construct($container, $installDir, $cacheDir, $debug = false, $bypassCache = false)
74 {
75 $this->innerContainer = $container;
76 $this->installDir = $installDir;
77 $this->cacheDir = $cacheDir;
78 $this->debug = $debug;
79 $this->bypassCache = $bypassCache;
80
81 $this->initializeContainer();
82 }
83
91 public function getRepository()
92 {
93 return $this->innerContainer->get('ezpublish.api.repository');
94 }
95
99 public function getInnerContainer()
100 {
101 return $this->innerContainer;
102 }
103
111 public function get($id)
112 {
113 return $this->innerContainer->get($id);
114 }
115
123 public function getParameter($name)
124 {
125 return $this->innerContainer->getParameter($name);
126 }
127
133 protected function initializeContainer()
134 {
135 // First check if cache should be bypassed
136 if ($this->bypassCache) {
137 $this->getContainer();
138
139 return;
140 }
141
142 // Prepare cache directory
143 $this->prepareDirectory($this->cacheDir, 'cache');
144
145 // Instantiate cache
146 $cache = new ConfigCache(
147 $this->cacheDir . '/container/' . $this->containerClassName . '.php',
148 $this->debug
149 );
150
151 // Check if cache needs to be regenerated, depends on debug being set to true
152 if (!$cache->isFresh()) {
153 $this->getContainer();
154 $this->dumpContainer($cache);
155 }
156
157 // Include container cache
158 require_once $cache;
159
160 // Instantiate container
161 $this->innerContainer = new $this->containerClassName();
162 }
163
169 protected function getContainer()
170 {
171 if ($this->innerContainer instanceof ContainerInterface) {
172 // Do nothing
173 } elseif (!is_readable($this->innerContainer)) {
174 throw new RuntimeException(
175 sprintf(
176 "Unable to read file %s\n",
177 $this->innerContainer
178 )
179 );
180 } else {
181 // 'containerBuilder.php' file expects $installDir variable to be set by caller
182 $installDir = $this->installDir;
183 $this->innerContainer = require_once $this->innerContainer;
184 }
185
186 // Compile container if necessary
187 if ($this->innerContainer instanceof ContainerBuilder && !$this->innerContainer->isFrozen()) {
188 $this->innerContainer->compile();
189 }
190 }
191
197 protected function dumpContainer(ConfigCache $cache)
198 {
199 $dumper = new PhpDumper($this->innerContainer);
200
201 if (class_exists('ProxyManager\Configuration')) {
202 $dumper->setProxyDumper(new ProxyDumper());
203 }
204
205 $content = $dumper->dump(
206 [
207 'class' => $this->containerClassName,
208 'base_class' => 'Container',
209 ]
210 );
211
212 $cache->write($content, $this->innerContainer->getResources());
213 }
214
223 protected function prepareDirectory($directory, $name)
224 {
225 if (!is_dir($directory)) {
226 if (!@mkdir($directory, 0777, true) && !is_dir($directory)) {
227 throw new RuntimeException(
228 sprintf(
229 "Unable to create the %s directory (%s)\n",
230 $name,
231 $directory
232 )
233 );
234 }
235 } elseif (!is_writable($directory)) {
236 throw new RuntimeException(
237 sprintf(
238 "Unable to write in the %s directory (%s)\n",
239 $name,
240 $directory
241 )
242 );
243 }
244 }
245 }
eZ\Publish\Core\Base\ServiceContainer
Container implementation wrapping Symfony container component.
Definition: ServiceContainer.php:22
eZ\Publish\Core\Base\ServiceContainer\getParameter
getParameter($name)
Convenience method to inner container.
Definition: ServiceContainer.php:123
eZ\Publish\Core\Base\ServiceContainer\$debug
$debug
Definition: ServiceContainer.php:56
eZ\Publish\Core\Base\ServiceContainer\dumpContainer
dumpContainer(ConfigCache $cache)
Dumps the service container to PHP code in the cache.
Definition: ServiceContainer.php:197
eZ\Publish\Core\Base\ServiceContainer\getContainer
getContainer()
Returns ContainerBuilder by including the default file 'containerBuilder.php' from settings directory...
Definition: ServiceContainer.php:169
eZ\Publish\Core\Base\ServiceContainer\getRepository
getRepository()
Get Repository object.
Definition: ServiceContainer.php:91
eZ\Publish\Core\Base\ServiceContainer\$innerContainer
$innerContainer
Definition: ServiceContainer.php:35
eZ\Publish\Core\Base\ServiceContainer\getInnerContainer
getInnerContainer()
Definition: ServiceContainer.php:99
eZ\Publish\Core\Base\ServiceContainer\initializeContainer
initializeContainer()
Initializes inner container.
Definition: ServiceContainer.php:133
eZ\Publish\Core\Base\ServiceContainer\$installDir
$installDir
Definition: ServiceContainer.php:42
eZ\Publish\Core\Base\ServiceContainer\$cacheDir
$cacheDir
Definition: ServiceContainer.php:49
eZ\Publish\Core\Base\ServiceContainer\prepareDirectory
prepareDirectory($directory, $name)
Checks for existence of given $directory and tries to create it if found missing.
Definition: ServiceContainer.php:223
eZ\Publish\Core\Base\ServiceContainer\__construct
__construct($container, $installDir, $cacheDir, $debug=false, $bypassCache=false)
Definition: ServiceContainer.php:73
eZ\Publish\Core\Base\ServiceContainer\$containerClassName
$containerClassName
Definition: ServiceContainer.php:28
eZ\Publish\Core\Base\ServiceContainer\$bypassCache
$bypassCache
Definition: ServiceContainer.php:63
eZ\Publish\API\Container
Container interface.
Definition: Container.php:15
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.