|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\BaseDistribution\Composer; |
| 6 | + |
| 7 | +use Symfony\Component\Yaml\Yaml; |
| 8 | + |
| 9 | +class SecurityConfigScriptHandler |
| 10 | +{ |
| 11 | + private const GENERATED_FILE_NOTICE = '# This file is autogenerated. Please do not edit.'; |
| 12 | + |
| 13 | + /** |
| 14 | + * @SuppressWarnings("CyclomaticComplexity") |
| 15 | + * @SuppressWarnings("NPathComplexity") |
| 16 | + */ |
| 17 | + public static function ensureSecurityConfiguration(): void |
| 18 | + { |
| 19 | + $configurationFilePath = dirname(__DIR__, 2) . '/config/config_modules.yml'; |
| 20 | + if (!is_readable($configurationFilePath)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + $configuration = Yaml::parse(file_get_contents($configurationFilePath)); |
| 25 | + if (!is_array($configuration)) { |
| 26 | + $configuration = []; |
| 27 | + } |
| 28 | + |
| 29 | + if (!isset($configuration['security']) || !is_array($configuration['security'])) { |
| 30 | + $configuration['security'] = []; |
| 31 | + } |
| 32 | + |
| 33 | + if (!isset($configuration['security']['firewalls']) || !is_array($configuration['security']['firewalls'])) { |
| 34 | + $configuration['security']['firewalls'] = []; |
| 35 | + } |
| 36 | + |
| 37 | + if (!isset($configuration['security']['firewalls']['dev']) |
| 38 | + || !is_array($configuration['security']['firewalls']['dev']) |
| 39 | + ) { |
| 40 | + $configuration['security']['firewalls']['dev'] = []; |
| 41 | + } |
| 42 | + |
| 43 | + if (!array_key_exists('pattern', $configuration['security']['firewalls']['dev'])) { |
| 44 | + $configuration['security']['firewalls']['dev']['pattern'] = '^/'; |
| 45 | + } |
| 46 | + |
| 47 | + if (!array_key_exists('security', $configuration['security']['firewalls']['dev'])) { |
| 48 | + $configuration['security']['firewalls']['dev']['security'] = false; |
| 49 | + } |
| 50 | + |
| 51 | + $content = self::GENERATED_FILE_NOTICE . PHP_EOL . Yaml::dump($configuration); |
| 52 | + file_put_contents($configurationFilePath, $content); |
| 53 | + } |
| 54 | +} |
0 commit comments