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