|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
|
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 Hautelook\AliceBundle\Doctrine\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
15
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
16
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
|
17
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\Fixtures\LoaderInterface as FixturesLoaderInterface; |
|
18
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface; |
|
19
|
|
|
use Hautelook\AliceBundle\Doctrine\DataFixtures\Executor\FixturesExecutorInterface; |
|
20
|
|
|
use Hautelook\AliceBundle\Doctrine\Finder\FixturesFinder; |
|
21
|
|
|
use Hautelook\AliceBundle\Doctrine\Generator\LoaderGeneratorInterface; |
|
22
|
|
|
use Hautelook\AliceBundle\Finder\FixturesFinderInterface; |
|
23
|
|
|
use Hautelook\AliceBundle\Resolver\BundlesResolverInterface; |
|
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
25
|
|
|
use Symfony\Component\Console\Command\Command; |
|
26
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
|
27
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
28
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
29
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
31
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Command used to load the fixtures. |
|
35
|
|
|
* |
|
36
|
|
|
* @author Théo FIDRY <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class LoadDataFixturesCommand extends Command |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var BundlesResolverInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $bundlesResolver; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ManagerRegistry |
|
47
|
|
|
*/ |
|
48
|
|
|
private $doctrine; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var FixturesExecutorInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $fixturesExecutor; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var FixturesFinderInterface|FixturesFinder |
|
57
|
|
|
*/ |
|
58
|
|
|
private $fixturesFinder; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var FixturesLoaderInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
private $fixturesLoader; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var LoaderInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private $loader; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var LoaderGeneratorInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
private $loaderGenerator; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $name Command name |
|
77
|
|
|
* @param ManagerRegistry $doctrine |
|
78
|
|
|
* @param LoaderInterface $loader |
|
79
|
|
|
* @param FixturesLoaderInterface $fixturesLoader |
|
80
|
|
|
* @param FixturesFinderInterface $fixturesFinder |
|
81
|
|
|
* @param BundlesResolverInterface $bundlesResolver |
|
82
|
|
|
* @param LoaderGeneratorInterface $loaderGenerator |
|
83
|
|
|
* @param FixturesExecutorInterface $fixturesExecutor |
|
84
|
|
|
*/ |
|
85
|
12 |
|
public function __construct( |
|
86
|
|
|
$name, |
|
87
|
|
|
ManagerRegistry $doctrine, |
|
88
|
|
|
LoaderInterface $loader, |
|
89
|
|
|
FixturesLoaderInterface $fixturesLoader, |
|
90
|
|
|
FixturesFinderInterface $fixturesFinder, |
|
91
|
|
|
BundlesResolverInterface $bundlesResolver, |
|
92
|
|
|
LoaderGeneratorInterface $loaderGenerator, |
|
93
|
|
|
FixturesExecutorInterface $fixturesExecutor |
|
94
|
|
|
) { |
|
95
|
12 |
|
$this->doctrine = $doctrine; |
|
96
|
12 |
|
$this->loader = $loader; |
|
97
|
12 |
|
$this->fixturesLoader = $fixturesLoader; |
|
98
|
12 |
|
$this->fixturesFinder = $fixturesFinder; |
|
99
|
12 |
|
$this->bundlesResolver = $bundlesResolver; |
|
100
|
12 |
|
$this->loaderGenerator = $loaderGenerator; |
|
101
|
12 |
|
$this->fixturesExecutor = $fixturesExecutor; |
|
102
|
|
|
|
|
103
|
12 |
|
parent::__construct($name); |
|
104
|
12 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
12 |
|
protected function configure() |
|
110
|
|
|
{ |
|
111
|
12 |
|
$this |
|
112
|
12 |
|
->setDescription('Load data fixtures to your database.') |
|
113
|
12 |
|
->addOption( |
|
114
|
12 |
|
'bundle', |
|
115
|
12 |
|
'b', |
|
116
|
12 |
|
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
|
117
|
|
|
'Bundles where fixtures should be loaded.' |
|
118
|
12 |
|
) |
|
119
|
12 |
|
->addOption( |
|
120
|
12 |
|
'manager', |
|
121
|
12 |
|
'em', |
|
122
|
12 |
|
InputOption::VALUE_REQUIRED, |
|
123
|
|
|
'The entity manager to use for this command.' |
|
124
|
12 |
|
) |
|
125
|
12 |
|
->addOption( |
|
126
|
12 |
|
'append', |
|
127
|
12 |
|
null, |
|
128
|
12 |
|
InputOption::VALUE_NONE, |
|
129
|
|
|
'Append the data fixtures instead of deleting all data from the database first.' |
|
130
|
12 |
|
) |
|
131
|
12 |
|
->addOption( |
|
132
|
12 |
|
'shard', |
|
133
|
12 |
|
null, |
|
134
|
12 |
|
InputOption::VALUE_REQUIRED, |
|
135
|
|
|
'The shard database id to use for this command.' |
|
136
|
12 |
|
) |
|
137
|
|
|
; |
|
138
|
|
|
|
|
139
|
12 |
|
if ($this->doctrine instanceof Registry) { |
|
140
|
10 |
|
$this->addOption('purge-with-truncate', |
|
141
|
10 |
|
null, |
|
142
|
10 |
|
InputOption::VALUE_NONE, |
|
143
|
|
|
'Purge data by using a database-level TRUNCATE statement when using Doctrine ORM.' |
|
144
|
10 |
|
); |
|
145
|
10 |
|
} |
|
146
|
12 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
* |
|
151
|
|
|
* \RuntimeException Unsupported Application type |
|
152
|
|
|
*/ |
|
153
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
154
|
|
|
{ |
|
155
|
6 |
|
if (false !== strpos($input->getFirstArgument(), 'hautelook_alice:fixtures:load') |
|
156
|
6 |
|
|| false !== strpos($input->getFirstArgument(), 'h:f:l') |
|
157
|
6 |
|
) { |
|
158
|
|
|
$output->writeln('<comment>The use of "hautelook_alice:fixtures:load" command is deprecated since 1.0 and will be removed in 2.0. Use the |
|
159
|
|
|
"hautelook_alice:doctrine:fixtures:load" instead.</comment>'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Warn the user that the database will be purged |
|
163
|
|
|
// Ask him to confirm his choice |
|
164
|
6 |
|
if ($input->isInteractive() && !$input->getOption('append')) { |
|
165
|
|
|
if (false === $this->askConfirmation( |
|
166
|
|
|
$input, |
|
167
|
|
|
$output, |
|
168
|
|
|
'<question>Careful, database will be purged. Do you want to continue y/N ?</question>', |
|
169
|
|
|
false |
|
170
|
|
|
) |
|
171
|
|
|
) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
6 |
|
$manager = $this->doctrine->getManager($input->getOption('manager')); |
|
177
|
6 |
|
$environment = $input->getOption('env'); |
|
178
|
6 |
|
$bundles = $input->getOption('bundle'); |
|
179
|
|
|
|
|
180
|
|
|
/** @var Application $application */ |
|
181
|
6 |
|
$application = $this->getApplication(); |
|
182
|
6 |
|
if (false === $application instanceof Application) { |
|
183
|
|
|
throw new \RuntimeException('Expected Symfony\Bundle\FrameworkBundle\Console\Application application.'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// Get bundles |
|
187
|
6 |
|
if (true === empty($bundles)) { |
|
188
|
6 |
|
$bundles = $application->getKernel()->getBundles(); |
|
189
|
6 |
|
} else { |
|
190
|
|
|
$bundles = $this->bundlesResolver->resolveBundles($application, $bundles); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
6 |
|
$fixtures = $this->fixturesFinder->getFixtures($application->getKernel(), $bundles, $environment); |
|
194
|
|
|
|
|
195
|
6 |
|
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures found:')); |
|
196
|
6 |
|
foreach ($fixtures as $fixture) { |
|
197
|
6 |
|
$output->writeln(sprintf(' <comment>-</comment> <info>%s</info>', $fixture)); |
|
198
|
6 |
|
} |
|
199
|
|
|
|
|
200
|
6 |
|
$truncate = $input->hasOption('purge-with-truncate') ? $input->getOption('purge-with-truncate') : false; |
|
201
|
|
|
|
|
202
|
|
|
// Shard database |
|
203
|
6 |
|
$shard = $input->getOption('shard'); |
|
204
|
6 |
|
if (!empty($shard)) { |
|
205
|
|
|
$connection = $manager->getConnection(); |
|
206
|
|
|
if (!$connection instanceof PoolingShardConnection) { |
|
207
|
|
|
throw new \RuntimeException('Expected Doctrine\DBAL\Sharding\PoolingShardConnection connection when using shard option.'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Switch to shard database |
|
211
|
|
|
$connection->connect($shard); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
6 |
|
$this->fixturesExecutor->execute( |
|
215
|
6 |
|
$manager, |
|
216
|
6 |
|
$this->loaderGenerator->generate($this->loader, $this->fixturesLoader, $bundles, $environment), |
|
217
|
6 |
|
$fixtures, |
|
218
|
6 |
|
$input->getOption('append'), |
|
219
|
6 |
|
function ($message) use ($output) { |
|
220
|
6 |
|
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); |
|
221
|
6 |
|
}, |
|
222
|
|
|
$truncate |
|
223
|
6 |
|
); |
|
224
|
6 |
|
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures loaded')); |
|
225
|
6 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Prompt to the user a message to ask him a confirmation. |
|
229
|
|
|
* |
|
230
|
|
|
* @param InputInterface $input |
|
231
|
|
|
* @param OutputInterface $output |
|
232
|
|
|
* @param string $question |
|
233
|
|
|
* @param bool $default |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool User choice. |
|
236
|
|
|
*/ |
|
237
|
|
|
private function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default) |
|
238
|
|
|
{ |
|
239
|
|
|
if (false === class_exists('Symfony\Component\Console\Question\ConfirmationQuestion')) { |
|
240
|
|
|
/** @var DialogHelper $dialogHelper */ |
|
241
|
|
|
$dialogHelper = $this->getHelperSet()->get('dialog'); |
|
242
|
|
|
|
|
243
|
|
|
return $dialogHelper->askConfirmation($output, $question, $default); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** @var QuestionHelper $questionHelper */ |
|
247
|
|
|
$questionHelper = $this->getHelperSet()->get('question'); |
|
248
|
|
|
$question = new ConfirmationQuestion($question, $default); |
|
249
|
|
|
|
|
250
|
|
|
return (bool) $questionHelper->ask($input, $output, $question); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|