1 | <?php |
||
14 | class MigrateConfigCommand extends Command |
||
15 | { |
||
16 | const CONFIG_FILE_TEMPLATE = <<< 'EOT' |
||
17 | <?php |
||
18 | declare(strict_types=1); |
||
19 | |||
20 | /** |
||
21 | * This configuration cache file was generated by zf-acmailer-tooling |
||
22 | * at %s |
||
23 | */ |
||
24 | return %s; |
||
25 | |||
26 | EOT; |
||
27 | |||
28 | 4 | protected function configure() |
|
50 | |||
51 | 4 | protected function execute(InputInterface $input, OutputInterface $output) |
|
52 | { |
||
53 | 4 | $config = $this->resolveGlobalConfig($input)['acmailer_options'] ?? []; |
|
54 | 2 | if (empty($config)) { |
|
55 | 1 | throw new Exception\RuntimeException( |
|
56 | 1 | 'The first level "acmailer_options" config entry was not found or is empty' |
|
57 | ); |
||
58 | } |
||
59 | |||
60 | 1 | $migratedConfig = ['acmailer_options' => $this->migrateConfig($config)]; |
|
61 | 1 | $outputFile = $input->getOption('output-file'); |
|
62 | 1 | $generatedConfig = \sprintf( |
|
63 | 1 | self::CONFIG_FILE_TEMPLATE, |
|
64 | 1 | \date(\DateTime::ATOM), |
|
65 | 1 | \var_export($migratedConfig, true) |
|
66 | ); |
||
67 | 1 | if ($outputFile !== null) { |
|
68 | \file_put_contents($outputFile, $generatedConfig); |
||
69 | } else { |
||
70 | 1 | $output->writeln($generatedConfig); |
|
71 | } |
||
72 | 1 | } |
|
73 | |||
74 | 4 | private function resolveGlobalConfig(InputInterface $input): array |
|
75 | { |
||
76 | 4 | $configFile = $input->getOption('config-file'); |
|
77 | 4 | if ($configFile !== null) { |
|
78 | 3 | if (\is_file($configFile)) { |
|
79 | 2 | return require $configFile; |
|
80 | } |
||
81 | |||
82 | 1 | throw new Exception\UnexpectedValueException( |
|
83 | 1 | \sprintf('Provided config file "%s" does not exist', $configFile) |
|
84 | ); |
||
85 | } |
||
86 | |||
87 | // Try to load expressive's global config |
||
88 | 1 | $configFile = getcwd() . '/config/config.php'; |
|
89 | 1 | if (\is_file($configFile)) { |
|
90 | return require $configFile; |
||
91 | } |
||
92 | |||
93 | // If not found, try to load MVC's global config |
||
94 | 1 | $appConfigFile = getcwd() . '/config/application.config.php'; |
|
95 | 1 | if (\is_file($appConfigFile)) { |
|
96 | $appConfig = require $appConfigFile; |
||
97 | return (new ConfigAggregator(\array_map(function (string $glob) { |
||
98 | return new PhpFileProvider($glob); |
||
99 | }, $appConfig['module_listener_options']['config_glob_paths'] ?? [])))->getMergedConfig(); |
||
100 | } |
||
101 | |||
102 | // If none of them was found, throw an exception |
||
103 | 1 | throw new Exception\RuntimeException( |
|
104 | 1 | 'It wasn\'t possible to find expressive or Zend MVC standard configurations' |
|
105 | ); |
||
106 | } |
||
107 | |||
108 | 1 | private function migrateConfig(array $oldConfig): array |
|
121 | |||
122 | 1 | private function parseConfigToService(array $config): array |
|
132 | |||
133 | 1 | private function parseConfigToEmail(array $config): array |
|
157 | } |
||
158 |