1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration\Console\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
|
12
|
|
|
class DefaultCommand extends Command |
13
|
|
|
{ |
14
|
|
|
const COMMAND = 'magium:configuration:init'; |
15
|
|
|
|
16
|
6 |
|
protected function configure() |
17
|
|
|
{ |
18
|
6 |
|
$this->setName(self::COMMAND) |
19
|
6 |
|
->setHelp('Initializes Magium Configuration') |
20
|
6 |
|
->setDescription('Initializes Magium Configuration by creating the default magium-configuration.xml and ' |
21
|
6 |
|
. 'contexts.xml files'); |
22
|
6 |
|
} |
23
|
|
|
|
24
|
4 |
|
protected function getPossibleLocations() |
25
|
|
|
{ |
26
|
4 |
|
$path = __DIR__; |
27
|
4 |
|
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path); |
28
|
4 |
|
$path = preg_replace('@/lib/.+@', '', $path); |
29
|
4 |
|
$paths = explode('/', $path); |
30
|
4 |
|
array_shift($paths); // Probably should not be in the root path... |
31
|
4 |
|
$configurationLocation = realpath(DIRECTORY_SEPARATOR); |
32
|
4 |
|
$foundVendor = false; |
33
|
4 |
|
$possibleLocations = []; |
34
|
4 |
|
foreach ($paths as $path) { |
35
|
4 |
|
$configurationLocation .= DIRECTORY_SEPARATOR . $path; |
36
|
4 |
|
$configurationLocation = realpath($configurationLocation); |
37
|
4 |
|
$file = $configurationLocation . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
38
|
|
|
|
39
|
4 |
|
if (file_exists($file)) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
4 |
|
if (basename($path) == 'vendor') { |
43
|
|
|
$foundVendor = true; |
44
|
|
|
} |
45
|
4 |
|
if (!$foundVendor) { |
46
|
4 |
|
$possibleLocations[] = $file; |
47
|
|
|
} |
48
|
|
|
} |
49
|
4 |
|
$possibleLocations = array_unique($possibleLocations); // just in case... |
50
|
4 |
|
return $possibleLocations; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|null $file |
55
|
|
|
*/ |
56
|
2 |
|
protected function writeMagiumConfigurationFile($file) |
57
|
|
|
{ |
58
|
2 |
|
file_put_contents($file, <<<XML |
59
|
|
|
<?xml version="1.0" encoding="UTF-8" ?> |
60
|
|
|
<magiumBase xmlns="http://www.magiumlib.com/BaseConfiguration"> |
61
|
|
|
<persistenceConfiguration> |
62
|
|
|
<driver></driver> |
63
|
|
|
<database></database> |
64
|
|
|
</persistenceConfiguration> |
65
|
|
|
<contextConfigurationFile file="contexts.xml" type="xml"/> |
66
|
|
|
<cache> |
67
|
|
|
<adapter>filesystem</adapter> |
68
|
|
|
<options> |
69
|
|
|
<cache_dir>/tmp</cache_dir> |
70
|
|
|
</options> |
71
|
|
|
</cache> |
72
|
2 |
|
</magiumBase> |
73
|
|
|
XML |
74
|
|
|
); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|null $configPath |
79
|
|
|
*/ |
80
|
2 |
|
protected function getContextFileFromConfigPath($configPath) |
81
|
|
|
{ |
82
|
2 |
|
$basePath = dirname($configPath); |
83
|
2 |
|
$contextPath = $basePath . DIRECTORY_SEPARATOR . 'contexts.xml'; |
84
|
2 |
|
return $contextPath; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $contextPath |
89
|
|
|
*/ |
90
|
1 |
|
protected function writeContextFileXml($contextPath) |
91
|
|
|
{ |
92
|
1 |
|
file_put_contents($contextPath, <<<XML |
93
|
|
|
<?xml version="1.0" encoding="UTF-8" ?> |
94
|
|
|
<magiumDefaultContext xmlns="http://www.magiumlib.com/ConfigurationContext"> |
95
|
|
|
<context id="production" label="Production" /> |
96
|
|
|
<context id="development" label="Development" /> |
97
|
1 |
|
</magiumDefaultContext> |
98
|
|
|
XML |
99
|
|
|
); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
2 |
|
protected function executeChoices(array $possibleLocations, InputInterface $input, OutputInterface $output) |
103
|
|
|
{ |
104
|
|
|
|
105
|
2 |
|
$result = $this->askConfigurationQuestion($possibleLocations, $input, $output); |
106
|
2 |
|
$this->writeMagiumConfigurationFile($result); |
107
|
2 |
|
$output->writeln('Wrote XML configuration file to: ' . $result); |
108
|
|
|
|
109
|
2 |
|
$contextPath = $this->getContextFileFromConfigPath($result); |
110
|
2 |
|
if (!file_exists($contextPath)) { |
111
|
2 |
|
$result = $this->askContextFileQuestion($input, $output, $contextPath); |
112
|
2 |
|
if ($result != null) { |
113
|
1 |
|
$this->writeContextFileXml($contextPath); |
114
|
|
|
} |
115
|
|
|
} |
116
|
2 |
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
protected function askConfigurationQuestion(array $possibleLocations, InputInterface $input, OutputInterface $output) |
119
|
|
|
{ |
120
|
|
|
$question = new ChoiceQuestion( |
121
|
|
|
'Could not find a magium-configuration.xml file. Where would you like me to put it?', |
122
|
|
|
$possibleLocations |
123
|
|
|
); |
124
|
|
|
$ask = $this->getHelper('question'); |
125
|
|
|
if ($ask instanceof QuestionHelper) { |
126
|
|
|
$result = $ask->ask($input, $output, $question); |
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $contextPath |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
protected function askContextFileQuestion(InputInterface $input, OutputInterface $output, $contextPath) |
137
|
|
|
{ |
138
|
|
|
$question = new ConfirmationQuestion(sprintf('The context file %s does not exist next to the magium-configuration.xml file. Create it? ', $contextPath)); |
139
|
|
|
$ask = $this->getHelper('question'); |
140
|
|
|
if ($ask instanceof QuestionHelper) { |
141
|
|
|
$result = $ask->ask($input, $output, $question); |
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
148
|
|
|
{ |
149
|
2 |
|
$possibleLocations = $this->getPossibleLocations(); |
150
|
2 |
|
if ($possibleLocations === false) { |
151
|
|
|
$command = $this->getApplication()->find('list'); |
152
|
|
|
$command->run($input, $output); |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
$this->executeChoices($possibleLocations, $input, $output); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|