1 | <?php |
||
16 | class ConfigMigrationController extends AbstractConsoleController |
||
17 | { |
||
18 | const WRITER_NAMESPACE = '\Zend\Config\Writer'; |
||
19 | |||
20 | private $formats = [ |
||
21 | 'php' => 'PhpArray', |
||
22 | 'ini' => 'Ini', |
||
23 | 'json' => 'Json', |
||
24 | 'xml' => 'Xml' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var ConfigMigrationServiceInterface |
||
29 | */ |
||
30 | protected $configMigrationService; |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $globalConfig; |
||
35 | |||
36 | 5 | public function __construct(ConfigMigrationServiceInterface $configMigrationService, array $globalConfig) |
|
37 | { |
||
38 | 5 | $this->configMigrationService = $configMigrationService; |
|
39 | 5 | $this->globalConfig = $globalConfig; |
|
40 | 5 | } |
|
41 | |||
42 | 4 | public function parseConfigAction() |
|
43 | { |
||
44 | /** @var Request $request */ |
||
45 | 4 | $request = $this->getRequest(); |
|
46 | 4 | $format = $request->getParam('format', 'php'); |
|
47 | 4 | $writer = $this->createWriter($format); |
|
48 | |||
49 | 3 | $configKey = $request->getParam('configKey', 'mail_options'); // Default to the old config key |
|
50 | 3 | $outputFile = $request->getParam('outputFile'); |
|
51 | |||
52 | // Global configuration not found |
||
53 | 3 | if (! isset($this->globalConfig[$configKey])) { |
|
54 | 1 | $this->getConsole()->writeLine( |
|
55 | 1 | sprintf('It wasn\'t possible to find the configuration key "%s"', $configKey), |
|
56 | ColorInterface::RED |
||
57 | 1 | ); |
|
58 | 1 | return PHP_EOL; |
|
59 | } |
||
60 | |||
61 | // Write new configuration to CLI |
||
62 | 2 | $newConfig = $this->configMigrationService->parseConfig($this->globalConfig[$configKey]); |
|
63 | 2 | if (! isset($outputFile)) { |
|
64 | 1 | $this->getConsole()->writeLine( |
|
65 | 1 | 'This is your new configuration for the AcMailer module:', |
|
66 | ColorInterface::GREEN |
||
67 | 1 | ); |
|
68 | 1 | $this->getConsole()->write($writer->toString($newConfig), ColorInterface::LIGHT_BLUE); |
|
69 | 1 | return PHP_EOL; |
|
70 | } |
||
71 | |||
72 | // Write new configuration to file |
||
73 | 1 | $writer->toFile($outputFile, $newConfig); |
|
74 | 1 | $this->getConsole()->writeLine( |
|
75 | 1 | sprintf('The new configuration for the AcMailer module has been written to "%s":', $outputFile), |
|
76 | ColorInterface::GREEN |
||
77 | 1 | ); |
|
78 | 1 | return PHP_EOL; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param $format |
||
83 | * @return WriterInterface |
||
84 | */ |
||
85 | 4 | protected function createWriter($format) |
|
99 | } |
||
100 |