1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace EzSystems\PlatformInstallerBundle\Command; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Process\Process; |
19
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
20
|
|
|
|
21
|
|
|
class InstallPlatformCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
24
|
|
|
private $db; |
25
|
|
|
|
26
|
|
|
/** @var \Symfony\Component\Console\Output\OutputInterface */ |
27
|
|
|
private $output; |
28
|
|
|
|
29
|
|
|
/** @var \Psr\Cache\CacheItemPoolInterface */ |
30
|
|
|
private $cachePool; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $environment; |
34
|
|
|
|
35
|
|
|
/** @var \EzSystems\PlatformInstallerBundle\Installer\Installer[] */ |
36
|
|
|
private $installers = array(); |
37
|
|
|
|
38
|
|
|
const EXIT_GENERAL_DATABASE_ERROR = 4; |
39
|
|
|
const EXIT_PARAMETERS_NOT_FOUND = 5; |
40
|
|
|
const EXIT_UNKNOWN_INSTALL_TYPE = 6; |
41
|
|
|
const EXIT_MISSING_PERMISSIONS = 7; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
Connection $db, |
45
|
|
|
array $installers, |
46
|
|
|
CacheItemPoolInterface $cachePool, |
47
|
|
|
$environment |
48
|
|
|
) { |
49
|
|
|
$this->db = $db; |
50
|
|
|
$this->installers = $installers; |
51
|
|
|
$this->cachePool = $cachePool; |
52
|
|
|
$this->environment = $environment; |
53
|
|
|
parent::__construct(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function configure() |
57
|
|
|
{ |
58
|
|
|
$this->setName('ezplatform:install'); |
59
|
|
|
$this->addArgument( |
60
|
|
|
'type', |
61
|
|
|
InputArgument::REQUIRED, |
62
|
|
|
'The type of install. Available options: ' . implode(', ', array_keys($this->installers)) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$this->output = $output; |
69
|
|
|
$this->checkPermissions(); |
70
|
|
|
$this->checkParameters(); |
71
|
|
|
$this->checkCreateDatabase($output); |
72
|
|
|
|
73
|
|
|
$type = $input->getArgument('type'); |
74
|
|
|
$installer = $this->getInstaller($type); |
75
|
|
|
if ($installer === false) { |
76
|
|
|
$output->writeln( |
77
|
|
|
"Unknown install type '$type', available options in currently installed eZ Platform package: " . |
78
|
|
|
implode(', ', array_keys($this->installers)) |
79
|
|
|
); |
80
|
|
|
exit(self::EXIT_UNKNOWN_INSTALL_TYPE); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$installer->setOutput($output); |
84
|
|
|
|
85
|
|
|
$installer->importSchema(); |
86
|
|
|
$installer->importData(); |
87
|
|
|
$installer->importBinaries(); |
88
|
|
|
$this->cacheClear($output); |
89
|
|
|
$this->indexData($output); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function checkPermissions() |
93
|
|
|
{ |
94
|
|
|
if (!is_writable('web') && !is_writable('web/var')) { |
95
|
|
|
$this->output->writeln('[web/ | web/var] is not writable'); |
96
|
|
|
exit(self::EXIT_MISSING_PERMISSIONS); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function checkParameters() |
101
|
|
|
{ |
102
|
|
|
$parametersFile = 'app/config/parameters.yml'; |
103
|
|
|
if (!is_file($parametersFile)) { |
104
|
|
|
$this->output->writeln("Required configuration file '$parametersFile' not found"); |
105
|
|
|
exit(self::EXIT_PARAMETERS_NOT_FOUND); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function checkCreateDatabase(OutputInterface $output) |
110
|
|
|
{ |
111
|
|
|
$output->writeln( |
112
|
|
|
sprintf( |
113
|
|
|
'Creating the database <comment>%s</comment> if it does not exist, executing command doctrine:database:create --if-not-exists', |
114
|
|
|
$this->db->getDatabase() |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
try { |
118
|
|
|
$bufferedOutput = new BufferedOutput(); |
119
|
|
|
$this->executeCommand($bufferedOutput, 'doctrine:database:create --if-not-exists'); |
120
|
|
|
$output->writeln($bufferedOutput->fetch()); |
121
|
|
|
} catch (\RuntimeException $exception) { |
122
|
|
|
$this->output->writeln( |
123
|
|
|
sprintf( |
124
|
|
|
"<error>The configured database '%s' does not exist or cannot be created.</error>", |
125
|
|
|
$this->db->getDatabase() |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
$this->output->writeln("Please check the database configuration in 'app/config/parameters.yml'"); |
129
|
|
|
exit(self::EXIT_GENERAL_DATABASE_ERROR); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Clear all content related cache (persistence cache). |
135
|
|
|
* |
136
|
|
|
* @param OutputInterface $output |
137
|
|
|
*/ |
138
|
|
|
private function cacheClear(OutputInterface $output) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$this->cachePool->clear(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Calls indexing commands. |
145
|
|
|
* |
146
|
|
|
* @todo This should not be needed once/if the Installer starts using API in the future. |
147
|
|
|
* So temporary measure until it is not raw SQL based for the data itself (as opposed to the schema). |
148
|
|
|
* This is done after cache clearing to make sure no cached data from before sql import is used. |
149
|
|
|
* |
150
|
|
|
* IMPORTANT: This is done using a command because config has change, so container and all services are different. |
151
|
|
|
* |
152
|
|
|
* @param OutputInterface $output |
153
|
|
|
*/ |
154
|
|
|
private function indexData(OutputInterface $output) |
155
|
|
|
{ |
156
|
|
|
$output->writeln( |
157
|
|
|
sprintf('Search engine re-indexing, executing command ezplatform:reindex') |
158
|
|
|
); |
159
|
|
|
$this->executeCommand($output, 'ezplatform:reindex'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $type |
164
|
|
|
* |
165
|
|
|
* @return \EzSystems\PlatformInstallerBundle\Installer\Installer |
166
|
|
|
*/ |
167
|
|
|
private function getInstaller($type) |
168
|
|
|
{ |
169
|
|
|
if (!isset($this->installers[$type])) { |
170
|
|
|
return false; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->installers[$type]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Executes a Symfony command in separate process. |
178
|
|
|
* |
179
|
|
|
* Typically useful when configuration has changed, or you are outside of Symfony context (Composer commands). |
180
|
|
|
* |
181
|
|
|
* Based on {@see \Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::executeCommand}. |
182
|
|
|
* |
183
|
|
|
* @param OutputInterface $output |
184
|
|
|
* @param string $cmd eZ Platform command to execute, like 'ezplatform:solr_create_index' |
185
|
|
|
* Escape any user provided arguments, like: 'assets:install '.escapeshellarg($webDir) |
186
|
|
|
* @param int $timeout |
187
|
|
|
*/ |
188
|
|
|
private function executeCommand(OutputInterface $output, $cmd, $timeout = 300) |
189
|
|
|
{ |
190
|
|
|
$phpFinder = new PhpExecutableFinder(); |
191
|
|
|
if (!$phpPath = $phpFinder->find(false)) { |
192
|
|
|
throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// We don't know which php arguments where used so we gather some to be on the safe side |
196
|
|
|
$arguments = $phpFinder->findArguments(); |
197
|
|
|
if (false !== ($ini = php_ini_loaded_file())) { |
198
|
|
|
$arguments[] = '--php-ini=' . $ini; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Pass memory_limit in case this was specified as php argument, if not it will most likely be same as $ini. |
202
|
|
|
if ($memoryLimit = ini_get('memory_limit')) { |
203
|
|
|
$arguments[] = '-d memory_limit=' . $memoryLimit; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$phpArgs = implode(' ', array_map('escapeshellarg', $arguments)); |
207
|
|
|
$php = escapeshellarg($phpPath) . ($phpArgs ? ' ' . $phpArgs : ''); |
208
|
|
|
|
209
|
|
|
// Make sure to pass along relevant global Symfony options to console command |
210
|
|
|
$console = escapeshellarg('bin/console'); |
211
|
|
|
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
212
|
|
|
$console .= ' -' . str_repeat('v', $output->getVerbosity() - 1); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($output->isDecorated()) { |
216
|
|
|
$console .= ' --ansi'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$console .= ' --env=' . escapeshellarg($this->environment); |
220
|
|
|
|
221
|
|
|
$process = new Process($php . ' ' . $console . ' ' . $cmd, null, null, null, $timeout); |
222
|
|
|
$process->run(function ($type, $buffer) use ($output) { $output->write($buffer, false); }); |
223
|
|
|
if (!$process->isSuccessful()) { |
224
|
|
|
throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd))); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.