Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.10% |
27 / 29 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ContainerCache | |
93.10% |
27 / 29 |
|
71.43% |
5 / 7 |
9.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| isFresh | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| forceLoad | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getCachedContainer | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isEnvVarTrue | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getDumper | |
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 Psr\Container\ContainerInterface; |
| 28 | use Symfony\Component\Config\ConfigCache; |
| 29 | use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder; |
| 30 | use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
| 31 | |
| 32 | class ContainerCache |
| 33 | { |
| 34 | /** @var string */ |
| 35 | private $cacheFile; |
| 36 | |
| 37 | /** @var ConfigCache */ |
| 38 | private $configCache; |
| 39 | |
| 40 | /** @var SymfonyContainerBuilder */ |
| 41 | private $builder; |
| 42 | |
| 43 | /** @var PhpDumper */ |
| 44 | private $dumper; |
| 45 | |
| 46 | /** @var string */ |
| 47 | private $cachedContainerClassName; |
| 48 | |
| 49 | public function __construct( |
| 50 | string $cacheFile, |
| 51 | SymfonyContainerBuilder $builder, |
| 52 | ConfigCache $configCache = null, |
| 53 | PhpDumper $dumper = null, |
| 54 | bool $isDebugEnabled = null, |
| 55 | string $cachedContainerClassName = null |
| 56 | ) { |
| 57 | $this->cacheFile = $cacheFile; |
| 58 | $this->builder = $builder; |
| 59 | $this->configCache = $configCache ?? new ConfigCache( |
| 60 | $this->cacheFile, |
| 61 | $isDebugEnabled ?? $this->isEnvVarTrue('DI_CONTAINER_DEBUG') |
| 62 | ); |
| 63 | |
| 64 | $this->dumper = $dumper; |
| 65 | $this->cachedContainerClassName = $cachedContainerClassName ?? 'MyCachedContainer'; |
| 66 | } |
| 67 | |
| 68 | public function isFresh(): bool |
| 69 | { |
| 70 | return $this->configCache->isFresh(); |
| 71 | } |
| 72 | |
| 73 | public function load(): ContainerInterface |
| 74 | { |
| 75 | if ($this->isFresh()) { |
| 76 | return $this->getCachedContainer(); |
| 77 | } |
| 78 | |
| 79 | return $this->forceLoad(); |
| 80 | } |
| 81 | |
| 82 | public function forceLoad(): ContainerInterface |
| 83 | { |
| 84 | $this->builder->compile(); |
| 85 | |
| 86 | $this->configCache->write( |
| 87 | $this->getDumper()->dump( |
| 88 | [ |
| 89 | 'class' => $this->cachedContainerClassName, |
| 90 | 'base_class' => BaseContainer::class |
| 91 | ] |
| 92 | ), |
| 93 | $this->builder->getResources() |
| 94 | ); |
| 95 | |
| 96 | return $this->getCachedContainer(); |
| 97 | } |
| 98 | |
| 99 | private function getCachedContainer(): ContainerInterface |
| 100 | { |
| 101 | require_once $this->cacheFile; |
| 102 | |
| 103 | return new $this->cachedContainerClassName(); |
| 104 | } |
| 105 | |
| 106 | private function isEnvVarTrue(string $envVar): bool |
| 107 | { |
| 108 | if (!isset($_ENV[$envVar])) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return filter_var($_ENV[$envVar], FILTER_VALIDATE_BOOLEAN) ?? false; |
| 113 | } |
| 114 | |
| 115 | private function getDumper(): PhpDumper |
| 116 | { |
| 117 | return $this->dumper ?? new PhpDumper($this->builder); |
| 118 | } |
| 119 | } |