Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ValidateAndSanitizeConfig | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
7 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| process | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\LaravelApie\Config; |
| 3 | |
| 4 | use Symfony\Component\Config\ConfigCache; |
| 5 | use Symfony\Component\Config\Definition\Processor; |
| 6 | use Symfony\Component\Config\Resource\ReflectionClassResource; |
| 7 | |
| 8 | final class ValidateAndSanitizeConfig |
| 9 | { |
| 10 | /** @var array<string, bool> */ |
| 11 | private static array $alreadyProcessing = []; |
| 12 | |
| 13 | /** @var array<string, array<string, mixed>> */ |
| 14 | private static array $cache = []; |
| 15 | |
| 16 | /** |
| 17 | * @codeCoverageIgnore |
| 18 | */ |
| 19 | private function __construct() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @param array<string, mixed> $rawConfig |
| 25 | * @return array<string, mixed> |
| 26 | */ |
| 27 | public static function process(array $rawConfig): array |
| 28 | { |
| 29 | $key = md5(json_encode($rawConfig)); |
| 30 | if (isset(self::$cache[$key])) { |
| 31 | return self::$cache[$key]; |
| 32 | } |
| 33 | if (!empty(self::$alreadyProcessing[$key])) { |
| 34 | return $rawConfig; |
| 35 | } |
| 36 | |
| 37 | self::$alreadyProcessing[$key] = true; |
| 38 | try { |
| 39 | $path = storage_path('framework/cache/apie-config' . md5(json_encode($rawConfig)) . '.php'); |
| 40 | $resources = [ |
| 41 | new ReflectionClassResource(new \ReflectionClass(LaravelConfiguration::class)), |
| 42 | new ReflectionClassResource(new \ReflectionClass(static::class)), |
| 43 | ]; |
| 44 | $configCache = new ConfigCache($path, true); |
| 45 | if ($configCache->isFresh()) { |
| 46 | $processedConfig = require $path; |
| 47 | } else { |
| 48 | $configuration = new LaravelConfiguration(); |
| 49 | |
| 50 | $processor = new Processor(); |
| 51 | |
| 52 | $processedConfig = $processor->processConfiguration($configuration, ['apie' => $rawConfig]); |
| 53 | |
| 54 | if (!isset($processedConfig['scan_bounded_contexts'])) { |
| 55 | $processedConfig['scan_bounded_contexts'] = []; |
| 56 | } |
| 57 | if (empty($processedConfig['storage'])) { |
| 58 | $processedConfig['storage'] = null; |
| 59 | } |
| 60 | $code = '<?php' . PHP_EOL . 'return ' . var_export($processedConfig, true) . ';'; |
| 61 | $configCache->write($code, $resources); |
| 62 | } |
| 63 | |
| 64 | self::$cache[$key] = $processedConfig; |
| 65 | return $processedConfig; |
| 66 | } finally { |
| 67 | unset(self::$alreadyProcessing[$key]); |
| 68 | } |
| 69 | } |
| 70 | } |