1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\Question; |
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
21
|
|
|
|
22
|
|
|
abstract class AbstractInstallCommand extends ContainerAwareCommand |
23
|
|
|
{ |
24
|
|
|
const WEB_ASSETS_DIRECTORY = 'web/assets/'; |
25
|
|
|
const WEB_BUNDLES_DIRECTORY = 'web/bundles/'; |
26
|
|
|
const WEB_MEDIA_DIRECTORY = 'web/media/'; |
27
|
|
|
const WEB_MEDIA_IMAGE_DIRECTORY = 'web/media/image/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CommandExecutor |
31
|
|
|
*/ |
32
|
|
|
protected $commandExecutor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
38
|
|
|
{ |
39
|
|
|
$application = $this->getApplication(); |
40
|
|
|
$application->setCatchExceptions(false); |
41
|
|
|
|
42
|
|
|
$this->commandExecutor = new CommandExecutor($input, $output, $application); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $id |
47
|
|
|
* |
48
|
|
|
* @return object |
49
|
|
|
*/ |
50
|
|
|
protected function get($id) |
51
|
|
|
{ |
52
|
|
|
return $this->getContainer()->get($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function getEnvironment() |
59
|
|
|
{ |
60
|
|
|
return $this->get('kernel')->getEnvironment(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
protected function isDebug() |
67
|
|
|
{ |
68
|
|
|
return $this->get('kernel')->isDebug(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $headers |
73
|
|
|
* @param array $rows |
74
|
|
|
* @param OutputInterface $output |
75
|
|
|
*/ |
76
|
|
|
protected function renderTable(array $headers, array $rows, OutputInterface $output) |
77
|
|
|
{ |
78
|
|
|
$table = $this->getHelper('table'); |
79
|
|
|
|
80
|
|
|
$table |
|
|
|
|
81
|
|
|
->setHeaders($headers) |
82
|
|
|
->setRows($rows) |
83
|
|
|
->render($output); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param OutputInterface $output |
88
|
|
|
* @param int $length |
89
|
|
|
* |
90
|
|
|
* @return ProgressBar |
91
|
|
|
*/ |
92
|
|
|
protected function createProgressBar(OutputInterface $output, $length = 10) |
93
|
|
|
{ |
94
|
|
|
$progress = new ProgressBar($output); |
95
|
|
|
$progress->setBarCharacter('<info>|</info>'); |
96
|
|
|
$progress->setEmptyBarCharacter(' '); |
97
|
|
|
$progress->setProgressCharacter('|'); |
98
|
|
|
|
99
|
|
|
$progress->start($length); |
100
|
|
|
|
101
|
|
|
return $progress; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $commands |
106
|
|
|
* @param InputInterface $input |
107
|
|
|
* @param OutputInterface $output |
108
|
|
|
* @param bool $displayProgress |
109
|
|
|
*/ |
110
|
|
|
protected function runCommands(array $commands, InputInterface $input, OutputInterface $output, $displayProgress = true) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if ($displayProgress) { |
113
|
|
|
$progress = $this->createProgressBar($output, count($commands)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($commands as $key => $value) { |
117
|
|
|
if (is_string($key)) { |
118
|
|
|
$command = $key; |
119
|
|
|
$parameters = $value; |
120
|
|
|
} else { |
121
|
|
|
$command = $value; |
122
|
|
|
$parameters = []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->commandExecutor->runCommand($command, $parameters); |
126
|
|
|
|
127
|
|
|
// PDO does not always close the connection after Doctrine commands. |
128
|
|
|
// See https://github.com/symfony/symfony/issues/11750. |
129
|
|
|
$this->get('doctrine')->getManager()->getConnection()->close(); |
130
|
|
|
|
131
|
|
|
if ($displayProgress) { |
132
|
|
|
$progress->advance(); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($displayProgress) { |
137
|
|
|
$progress->finish(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $directory |
143
|
|
|
* @param OutputInterface $output |
144
|
|
|
*/ |
145
|
|
|
protected function ensureDirectoryExistsAndIsWritable($directory, OutputInterface $output) |
146
|
|
|
{ |
147
|
|
|
$checker = $this->get('sylius.installer.checker.command_directory'); |
148
|
|
|
$checker->setCommandName($this->getName()); |
149
|
|
|
|
150
|
|
|
$checker->ensureDirectoryExists($directory, $output); |
151
|
|
|
$checker->ensureDirectoryIsWritable($directory, $output); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: