Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.11% |
44 / 72 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ContainerBuilder | |
61.11% |
44 / 72 |
|
55.56% |
5 / 9 |
50.47 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| build | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| forceBuild | |
61.54% |
16 / 26 |
|
0.00% |
0 / 1 |
6.42 | |||
| get | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| has | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| findDefinition | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getTemporaryServiceFileContent | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| getExtensionsManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isApplicationInstalled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\generis\model\DependencyInjection; |
| 26 | |
| 27 | use common_ext_Extension; |
| 28 | use common_ext_ExtensionsManager; |
| 29 | use InvalidArgumentException; |
| 30 | use oat\generis\model\Middleware\MiddlewareExtensionsMapper; |
| 31 | use Psr\Container\ContainerInterface; |
| 32 | use Symfony\Component\Config\FileLocator; |
| 33 | use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder; |
| 34 | use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; |
| 35 | use Symfony\Component\DependencyInjection\Definition; |
| 36 | use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException as SymfonyServiceNotFoundException; |
| 37 | use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
| 38 | use oat\oatbox\service\ServiceNotFoundException; |
| 39 | use Symfony\Component\DependencyInjection\Reference; |
| 40 | use Throwable; |
| 41 | |
| 42 | class ContainerBuilder extends SymfonyContainerBuilder |
| 43 | { |
| 44 | /** @var ContainerCache */ |
| 45 | private $cache; |
| 46 | |
| 47 | /** @var bool|null */ |
| 48 | private $cachePath; |
| 49 | |
| 50 | /** @var ContainerInterface */ |
| 51 | private $legacyContainer; |
| 52 | |
| 53 | /** @var MiddlewareExtensionsMapper|null */ |
| 54 | private $middlewareExtensionsMapper; |
| 55 | |
| 56 | /** @var string|null */ |
| 57 | private $configPath; |
| 58 | |
| 59 | /** @var bool */ |
| 60 | private static $containerIsAlreadyBuilt = false; |
| 61 | |
| 62 | public function __construct( |
| 63 | string $cachePath, |
| 64 | ContainerInterface $legacyContainer, |
| 65 | bool $isDebugEnabled = null, |
| 66 | ContainerCache $cache = null, |
| 67 | MiddlewareExtensionsMapper $middlewareExtensionsMapper = null, |
| 68 | string $configPath = null |
| 69 | ) { |
| 70 | $this->cachePath = $cachePath; |
| 71 | $this->legacyContainer = $legacyContainer; |
| 72 | $this->cache = $cache ?? new ContainerCache( |
| 73 | $cachePath . '_di/container.php', |
| 74 | $this, |
| 75 | null, |
| 76 | null, |
| 77 | $isDebugEnabled |
| 78 | ); |
| 79 | |
| 80 | parent::__construct(); |
| 81 | $this->middlewareExtensionsMapper = $middlewareExtensionsMapper ?? new MiddlewareExtensionsMapper(); |
| 82 | $this->configPath = $configPath ?? (defined('CONFIG_PATH') ? CONFIG_PATH : null); |
| 83 | } |
| 84 | |
| 85 | public function build(): ContainerInterface |
| 86 | { |
| 87 | if ($this->cache->isFresh()) { |
| 88 | return $this->cache->load(); |
| 89 | } |
| 90 | |
| 91 | return $this->forceBuild(); |
| 92 | } |
| 93 | |
| 94 | public function forceBuild(): ContainerInterface |
| 95 | { |
| 96 | if (self::$containerIsAlreadyBuilt) { |
| 97 | return $this->cache->load(); |
| 98 | } |
| 99 | |
| 100 | if (!$this->isApplicationInstalled()) { |
| 101 | return $this->legacyContainer; |
| 102 | } |
| 103 | |
| 104 | if (!is_writable($this->cachePath)) { |
| 105 | throw new InvalidArgumentException( |
| 106 | sprintf( |
| 107 | 'DI container build requires directory "%s" to be writable', |
| 108 | $this->cachePath |
| 109 | ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | try { |
| 114 | file_put_contents($this->cachePath . '/services.php', $this->getTemporaryServiceFileContent()); |
| 115 | |
| 116 | $phpLoader = new PhpFileLoader( |
| 117 | $this, |
| 118 | new FileLocator( |
| 119 | [ |
| 120 | $this->cachePath |
| 121 | ] |
| 122 | ) |
| 123 | ); |
| 124 | $phpLoader->load('services.php'); |
| 125 | |
| 126 | self::$containerIsAlreadyBuilt = true; |
| 127 | |
| 128 | return $this->cache->forceLoad(); |
| 129 | } catch (Throwable $exception) { |
| 130 | self::$containerIsAlreadyBuilt = false; |
| 131 | |
| 132 | throw $exception; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @inheritDoc |
| 138 | */ |
| 139 | public function get(string $id, int $invalidBehavior = SymfonyContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) |
| 140 | { |
| 141 | try { |
| 142 | return parent::get($id, $invalidBehavior); |
| 143 | } catch (SymfonyServiceNotFoundException $exception) { |
| 144 | } |
| 145 | |
| 146 | try { |
| 147 | return $this->legacyContainer->get($id); |
| 148 | } catch (ServiceNotFoundException $exception) { |
| 149 | throw new SymfonyServiceNotFoundException($id); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @inheritDoc |
| 155 | */ |
| 156 | public function has(string $id) |
| 157 | { |
| 158 | if (parent::has($id)) { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | try { |
| 163 | $this->legacyContainer->get($id); |
| 164 | |
| 165 | return true; |
| 166 | } catch (ServiceNotFoundException $exception) { |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @inheritDoc |
| 173 | */ |
| 174 | public function findDefinition(string $id) |
| 175 | { |
| 176 | try { |
| 177 | return parent::findDefinition($id); |
| 178 | } catch (SymfonyServiceNotFoundException $exception) { |
| 179 | return (new Definition($id)) |
| 180 | ->setAutowired(true) |
| 181 | ->setPublic(true) |
| 182 | ->setFactory(new Reference(LegacyServiceGateway::class)) |
| 183 | ->setArguments([$id]); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private function getTemporaryServiceFileContent(): string |
| 188 | { |
| 189 | $contents = []; |
| 190 | |
| 191 | /** @var common_ext_Extension[] $extensions */ |
| 192 | $extensions = $this->getExtensionsManager()->getInstalledExtensions(); |
| 193 | |
| 194 | foreach ($extensions as $extension) { |
| 195 | foreach ($extension->getManifest()->getContainerServiceProvider() as $serviceProvider) { |
| 196 | $contents[] = '(new ' . $serviceProvider . '())($configurator);'; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return sprintf( |
| 201 | '<?php |
| 202 | use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 203 | use \oat\generis\model\Middleware\MiddlewareExtensionsMapper; |
| 204 | |
| 205 | return function (ContainerConfigurator $configurator): void |
| 206 | { |
| 207 | $parameter = $configurator->parameters(); |
| 208 | $parameter->set(MiddlewareExtensionsMapper::MAP_KEY, %s); |
| 209 | |
| 210 | %s |
| 211 | };', |
| 212 | var_export($this->middlewareExtensionsMapper->map($extensions), true), |
| 213 | implode('', $contents) |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @note This method as the $legacyContainer needs to be here in order to avoid to load the |
| 219 | * common_ext_ExtensionsManager unnecessarily during this class initialization. |
| 220 | */ |
| 221 | private function getExtensionsManager(): common_ext_ExtensionsManager |
| 222 | { |
| 223 | return $this->legacyContainer->get(common_ext_ExtensionsManager::SERVICE_ID); |
| 224 | } |
| 225 | |
| 226 | private function isApplicationInstalled(): bool |
| 227 | { |
| 228 | return file_exists(rtrim((string)$this->configPath, '/') . '/generis/installation.conf.php'); |
| 229 | } |
| 230 | } |