1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace AcMailer\Tooling\Command; |
5
|
|
|
|
6
|
|
|
use AcMailer\Tooling\Exception; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Zend\ConfigAggregator\ConfigAggregator; |
12
|
|
|
use Zend\ConfigAggregator\PhpFileProvider; |
13
|
|
|
|
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() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
4 |
|
->setName('config:migrate') |
32
|
4 |
|
->setDescription( |
33
|
4 |
|
'Migrates configuration from the structure used in AcMailer v5/v6 to the structure used in v7' |
34
|
|
|
) |
35
|
4 |
|
->addOption( |
36
|
4 |
|
'config-file', |
37
|
4 |
|
'c', |
38
|
4 |
|
InputOption::VALUE_REQUIRED, |
39
|
|
|
'The file that will return the project\'s merged configuration. If not provided, it will try to ' |
40
|
4 |
|
. 'resolve the standard config for both Zend MVC and Expressive' |
41
|
|
|
) |
42
|
4 |
|
->addOption( |
43
|
4 |
|
'output-file', |
44
|
4 |
|
'o', |
45
|
4 |
|
InputOption::VALUE_REQUIRED, |
46
|
|
|
'The file in which output config should be dumped ' |
47
|
4 |
|
. 'If no value is provided, the config will be dumped in the standard output' |
48
|
|
|
); |
49
|
4 |
|
} |
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 |
109
|
|
|
{ |
110
|
|
|
$newConfig = [ |
111
|
1 |
|
'emails' => [], |
112
|
|
|
'mail_services' => [], |
113
|
|
|
]; |
114
|
1 |
|
foreach ($oldConfig as $namespace => $config) { |
115
|
1 |
|
$newConfig['mail_services'][$namespace] = $this->parseConfigToService($config); |
116
|
1 |
|
$newConfig['emails'][$namespace] = $this->parseConfigToEmail($config); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $newConfig; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
private function parseConfigToService(array $config): array |
123
|
|
|
{ |
124
|
1 |
|
return \array_filter([ |
125
|
1 |
|
'extends' => $config['extends'] ?? null, |
126
|
1 |
|
'transport' => $config['transport'] ?? $config['mail_adapter'] ?? null, |
127
|
1 |
|
'transport_options' => $config['smtp_options'] ?? $config['file_options'] ?? null, |
128
|
1 |
|
'renderer' => $config['renderer'] ?? null, |
129
|
1 |
|
'mail_listeners' => $config['mail_listeners'] ?? null, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
private function parseConfigToEmail(array $config): array |
134
|
|
|
{ |
135
|
1 |
|
$messageOptions = $config['message_options'] ?? []; |
136
|
1 |
|
return \array_filter([ |
137
|
1 |
|
'from' => $messageOptions['from'] ?? null, |
138
|
1 |
|
'from_name' => $messageOptions['from_name'] ?? null, |
139
|
1 |
|
'reply_to' => $messageOptions['reply_to'] ?? null, |
140
|
1 |
|
'reply_to_name' => $messageOptions['reply_to_name'] ?? null, |
141
|
1 |
|
'to' => (array) ($messageOptions['to'] ?? []), |
142
|
1 |
|
'cc' => (array) ($messageOptions['cc'] ?? []), |
143
|
1 |
|
'bcc' => (array) ($messageOptions['bcc'] ?? []), |
144
|
1 |
|
'encoding' => $messageOptions['encoding'] ?? null, |
145
|
1 |
|
'subject' => $messageOptions['subject'] ?? null, |
146
|
1 |
|
'body' => $messageOptions['body']['content'] ?? null, |
147
|
1 |
|
'charset' => $messageOptions['body']['charset'] ?? null, |
148
|
1 |
|
'template' => $messageOptions['body']['template']['path'] ?? null, |
149
|
1 |
|
'template_params' => \array_merge( |
150
|
1 |
|
(array) ($messageOptions['body']['template']['params'] ?? []), |
151
|
1 |
|
['layout' => $messageOptions['body']['template']['default_layout']['path'] ?? null] |
152
|
|
|
), |
153
|
1 |
|
'attachments' => $messageOptions['attachments']['files'] ?? null, |
154
|
1 |
|
'attachments_dir' => $messageOptions['attachments']['dir'] ?? null, |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|