|
1
|
|
|
<?php |
|
2
|
|
|
namespace AcMailer\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use AcMailer\Service\ConfigMigrationServiceInterface; |
|
5
|
|
|
use Zend\Console\ColorInterface; |
|
6
|
|
|
use Zend\Console\Request; |
|
7
|
|
|
use Zend\Mvc\Controller\AbstractConsoleController; |
|
8
|
|
|
use Zend\Config\Writer\WriterInterface; |
|
9
|
|
|
use AcMailer\Exception\InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ConfigMigrationController |
|
13
|
|
|
* @author Alejandro Celaya Alastrué |
|
14
|
|
|
* @link http://www.alejandrocelaya.com |
|
15
|
|
|
*/ |
|
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) |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (! array_key_exists($format, $this->formats) && ! in_array($format, $this->formats)) { |
|
88
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
89
|
1 |
|
'Provided format "%s" is not valid. Expected one of ["%s"]', |
|
90
|
1 |
|
$format, |
|
91
|
1 |
|
implode('", "', array_keys($this->formats)) |
|
92
|
1 |
|
)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
$writerClass = array_key_exists($format, $this->formats) ? $this->formats[$format] : $format; |
|
96
|
3 |
|
$writerClass = sprintf('%s\%s', self::WRITER_NAMESPACE, $writerClass); |
|
97
|
3 |
|
return new $writerClass; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|