Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

InstallPlatformCommand.php
Go to the documentation of this file.
1 <?php
2
7 namespace EzSystems\PlatformInstallerBundle\Command;
8
9 use Doctrine\DBAL\Connection;
10 use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
11 use Psr\Cache\CacheItemPoolInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputOption;
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 {
24 private $db;
25
27 private $output;
28
30 private $cachePool;
31
33 private $environment;
34
36 private $installers = [];
37
39 private $repositoryConfigurationProvider;
40
41 const EXIT_GENERAL_DATABASE_ERROR = 4;
42 const EXIT_PARAMETERS_NOT_FOUND = 5;
43 const EXIT_UNKNOWN_INSTALL_TYPE = 6;
44 const EXIT_MISSING_PERMISSIONS = 7;
45
46 public function __construct(
47 Connection $db,
48 array $installers,
49 CacheItemPoolInterface $cachePool,
50 $environment,
51 RepositoryConfigurationProvider $repositoryConfigurationProvider
52 ) {
53 $this->db = $db;
54 $this->installers = $installers;
55 $this->cachePool = $cachePool;
56 $this->environment = $environment;
57 $this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
58 parent::__construct();
59 }
60
61 protected function configure()
62 {
63 $this->setName('ezplatform:install');
64 $this->addArgument(
65 'type',
66 InputArgument::REQUIRED,
67 'The type of install. Available options: ' . implode(', ', array_keys($this->installers))
68 );
69 $this->addOption(
70 'skip-indexing',
71 null,
72 InputOption::VALUE_NONE,
73 'Skip indexing (ezplaform:reindex)'
74 );
75 }
76
77 protected function execute(InputInterface $input, OutputInterface $output)
78 {
79 $this->output = $output;
80 $this->checkPermissions();
81 $this->checkParameters();
82 $this->checkCreateDatabase($output);
83
84 $type = $input->getArgument('type');
85 $siteaccess = $input->getOption('siteaccess');
86 $installer = $this->getInstaller($type);
87 if ($installer === false) {
88 $output->writeln(
89 "Unknown install type '$type', available options in currently installed eZ Platform package: " .
90 implode(', ', array_keys($this->installers))
91 );
92 exit(self::EXIT_UNKNOWN_INSTALL_TYPE);
93 }
94
95 $installer->setOutput($output);
96
97 $installer->importSchema();
98 $installer->importData();
99 $installer->importBinaries();
100 $this->cacheClear($output);
101
102 if (!$input->getOption('skip-indexing')) {
103 $this->indexData($output, $siteaccess);
104 }
105 }
106
107 private function checkPermissions()
108 {
109 if (!is_writable('web') && !is_writable('web/var')) {
110 $this->output->writeln('[web/ | web/var] is not writable');
111 exit(self::EXIT_MISSING_PERMISSIONS);
112 }
113 }
114
115 private function checkParameters()
116 {
117 $parametersFile = 'app/config/parameters.yml';
118 if (!is_file($parametersFile)) {
119 $this->output->writeln("Required configuration file '$parametersFile' not found");
120 exit(self::EXIT_PARAMETERS_NOT_FOUND);
121 }
122 }
123
124 private function checkCreateDatabase(OutputInterface $output)
125 {
126 $output->writeln(
127 sprintf(
128 'Creating the database <comment>%s</comment> if it does not exist, executing command doctrine:database:create --if-not-exists',
129 $this->db->getDatabase()
130 )
131 );
132 try {
133 $bufferedOutput = new BufferedOutput();
134 $connectionName = $this->repositoryConfigurationProvider->getStorageConnectionName();
135 $command = sprintf('doctrine:database:create --if-not-exists --connection=%s', $connectionName);
136 $this->executeCommand($bufferedOutput, $command);
137 $output->writeln($bufferedOutput->fetch());
138 } catch (\RuntimeException $exception) {
139 $this->output->writeln(
140 sprintf(
141 "<error>The configured database '%s' does not exist or cannot be created (%s).</error>",
142 $this->db->getDatabase(),
143 $exception->getMessage()
144 )
145 );
146 $this->output->writeln("Please check the database configuration in 'app/config/parameters.yml'");
147 exit(self::EXIT_GENERAL_DATABASE_ERROR);
148 }
149 }
150
156 private function cacheClear(OutputInterface $output)
157 {
158 $this->cachePool->clear();
159 }
160
173 private function indexData(OutputInterface $output, $siteaccess = null)
174 {
175 $output->writeln(
176 sprintf('Search engine re-indexing, executing command ezplatform:reindex')
177 );
178
179 $command = 'ezplatform:reindex';
180 if ($siteaccess) {
181 $command .= sprintf(' --siteaccess=%s', $siteaccess);
182 }
183
184 $this->executeCommand($output, $command);
185 }
186
192 private function getInstaller($type)
193 {
194 if (!isset($this->installers[$type])) {
195 return false;
196 }
197
198 return $this->installers[$type];
199 }
200
213 private function executeCommand(OutputInterface $output, $cmd, $timeout = 300)
214 {
215 $phpFinder = new PhpExecutableFinder();
216 if (!$phpPath = $phpFinder->find(false)) {
217 throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
218 }
219
220 // We don't know which php arguments where used so we gather some to be on the safe side
221 $arguments = $phpFinder->findArguments();
222 if (false !== ($ini = php_ini_loaded_file())) {
223 $arguments[] = '--php-ini=' . $ini;
224 }
225
226 // Pass memory_limit in case this was specified as php argument, if not it will most likely be same as $ini.
227 if ($memoryLimit = ini_get('memory_limit')) {
228 $arguments[] = '-d memory_limit=' . $memoryLimit;
229 }
230
231 $phpArgs = implode(' ', array_map('escapeshellarg', $arguments));
232 $php = escapeshellarg($phpPath) . ($phpArgs ? ' ' . $phpArgs : '');
233
234 // Make sure to pass along relevant global Symfony options to console command
235 $console = escapeshellarg('bin/console');
236 if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
237 $console .= ' -' . str_repeat('v', $output->getVerbosity() - 1);
238 }
239
240 if ($output->isDecorated()) {
241 $console .= ' --ansi';
242 }
243
244 $console .= ' --env=' . escapeshellarg($this->environment);
245
246 $process = new Process($php . ' ' . $console . ' ' . $cmd, null, null, null, $timeout);
247 $process->run(function ($type, $buffer) use ($output) { $output->write($buffer, false); });
248 if (!$process->isSuccessful()) {
249 throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd)));
250 }
251 }
252 }
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand
Definition: InstallPlatformCommand.php:22
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\executeCommand
executeCommand(OutputInterface $output, $cmd, $timeout=300)
Executes a Symfony command in separate process.
Definition: InstallPlatformCommand.php:213
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$db
$db
Definition: InstallPlatformCommand.php:24
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$installers
$installers
Definition: InstallPlatformCommand.php:36
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\EXIT_UNKNOWN_INSTALL_TYPE
const EXIT_UNKNOWN_INSTALL_TYPE
Definition: InstallPlatformCommand.php:43
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\EXIT_MISSING_PERMISSIONS
const EXIT_MISSING_PERMISSIONS
Definition: InstallPlatformCommand.php:44
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\checkPermissions
checkPermissions()
Definition: InstallPlatformCommand.php:107
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\configure
configure()
Definition: InstallPlatformCommand.php:61
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\checkParameters
checkParameters()
Definition: InstallPlatformCommand.php:115
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\EXIT_PARAMETERS_NOT_FOUND
const EXIT_PARAMETERS_NOT_FOUND
Definition: InstallPlatformCommand.php:42
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\getInstaller
getInstaller($type)
Definition: InstallPlatformCommand.php:192
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\indexData
indexData(OutputInterface $output, $siteaccess=null)
Calls indexing commands.
Definition: InstallPlatformCommand.php:173
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\__construct
__construct(Connection $db, array $installers, CacheItemPoolInterface $cachePool, $environment, RepositoryConfigurationProvider $repositoryConfigurationProvider)
Definition: InstallPlatformCommand.php:46
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\cacheClear
cacheClear(OutputInterface $output)
Clear all content related cache (persistence cache).
Definition: InstallPlatformCommand.php:156
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$output
$output
Definition: InstallPlatformCommand.php:27
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\execute
execute(InputInterface $input, OutputInterface $output)
Definition: InstallPlatformCommand.php:77
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$cachePool
$cachePool
Definition: InstallPlatformCommand.php:30
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$environment
$environment
Definition: InstallPlatformCommand.php:33
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\$repositoryConfigurationProvider
$repositoryConfigurationProvider
Definition: InstallPlatformCommand.php:39
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\EXIT_GENERAL_DATABASE_ERROR
const EXIT_GENERAL_DATABASE_ERROR
Definition: InstallPlatformCommand.php:41
EzSystems\PlatformInstallerBundle\Command\InstallPlatformCommand\checkCreateDatabase
checkCreateDatabase(OutputInterface $output)
Definition: InstallPlatformCommand.php:124
eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider
The repository configuration provider.
Definition: RepositoryConfigurationProvider.php:16
EzSystems\PlatformInstallerBundle\Command
Definition: InstallPlatformCommand.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.