|
| 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 | + public static function ensureSecurityConfiguration(): void |
| 14 | + { |
| 15 | + $configurationFilePath = dirname(__DIR__, 2) . '/config/config_modules.yml'; |
| 16 | + if (!is_readable($configurationFilePath)) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + $configuration = Yaml::parse(file_get_contents($configurationFilePath)); |
| 21 | + if (!is_array($configuration)) { |
| 22 | + $configuration = []; |
| 23 | + } |
| 24 | + |
| 25 | + if (!isset($configuration['security']) || !is_array($configuration['security'])) { |
| 26 | + $configuration['security'] = []; |
| 27 | + } |
| 28 | + |
| 29 | + if (!isset($configuration['security']['firewalls']) || !is_array($configuration['security']['firewalls'])) { |
| 30 | + $configuration['security']['firewalls'] = []; |
| 31 | + } |
| 32 | + |
| 33 | + if (!isset($configuration['security']['firewalls']['dev']) || !is_array($configuration['security']['firewalls']['dev'])) { |
| 34 | + $configuration['security']['firewalls']['dev'] = []; |
| 35 | + } |
| 36 | + |
| 37 | + if (!array_key_exists('pattern', $configuration['security']['firewalls']['dev'])) { |
| 38 | + $configuration['security']['firewalls']['dev']['pattern'] = '^/'; |
| 39 | + } |
| 40 | + |
| 41 | + if (!array_key_exists('security', $configuration['security']['firewalls']['dev'])) { |
| 42 | + $configuration['security']['firewalls']['dev']['security'] = false; |
| 43 | + } |
| 44 | + |
| 45 | + $content = self::GENERATED_FILE_NOTICE . PHP_EOL . Yaml::dump($configuration); |
| 46 | + file_put_contents($configurationFilePath, $content); |
| 47 | + } |
| 48 | +} |
0 commit comments