1 | <?php |
||
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 | 120 | public function __construct( |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 120 | protected function configure() |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | * |
||
151 | * \RuntimeException Unsupported Application type |
||
152 | */ |
||
153 | 114 | protected function execute(InputInterface $input, OutputInterface $output) |
|
154 | { |
||
155 | 114 | if (false !== strpos($input->getFirstArgument(), 'hautelook_alice:fixtures:load') |
|
156 | 114 | || false !== strpos($input->getFirstArgument(), 'h:f:l') |
|
157 | 114 | ) { |
|
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 | 114 | 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 | 114 | $manager = $this->doctrine->getManager($input->getOption('manager')); |
|
177 | 78 | $environment = $input->getOption('env'); |
|
178 | 78 | $bundles = $input->getOption('bundle'); |
|
179 | |||
180 | /** @var Application $application */ |
||
181 | 78 | $application = $this->getApplication(); |
|
182 | 78 | if (false === $application instanceof Application) { |
|
183 | throw new \RuntimeException('Expected Symfony\Bundle\FrameworkBundle\Console\Application application.'); |
||
184 | } |
||
185 | |||
186 | // Get bundles |
||
187 | 78 | if (true === empty($bundles)) { |
|
188 | 30 | $bundles = $application->getKernel()->getBundles(); |
|
189 | 30 | } else { |
|
190 | 48 | $bundles = $this->bundlesResolver->resolveBundles($application, $bundles); |
|
191 | } |
||
192 | |||
193 | 78 | $fixtures = $this->fixturesFinder->getFixtures($application->getKernel(), $bundles, $environment); |
|
194 | |||
195 | 78 | $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures found:')); |
|
196 | 78 | foreach ($fixtures as $fixture) { |
|
197 | 78 | $output->writeln(sprintf(' <comment>-</comment> <info>%s</info>', $fixture)); |
|
198 | 78 | } |
|
199 | |||
200 | 78 | $truncate = $input->hasOption('purge-with-truncate') ? $input->getOption('purge-with-truncate') : false; |
|
201 | |||
202 | // Shard database |
||
203 | 78 | $shard = $input->getOption('shard'); |
|
204 | 78 | if (!empty($shard)) { |
|
205 | 6 | $connection = $manager->getConnection(); |
|
206 | 6 | 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 | 6 | $connection->connect($shard); |
|
212 | 6 | } |
|
213 | |||
214 | 78 | $this->fixturesExecutor->execute( |
|
215 | 78 | $manager, |
|
216 | 78 | $this->loaderGenerator->generate($this->loader, $this->fixturesLoader, $bundles, $environment), |
|
217 | 78 | $fixtures, |
|
218 | 78 | $input->getOption('append'), |
|
219 | 78 | function ($message) use ($output) { |
|
220 | 78 | $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); |
|
221 | 78 | }, |
|
222 | $truncate |
||
223 | 78 | ); |
|
224 | 78 | $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures loaded')); |
|
225 | 78 | } |
|
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) |
||
252 | } |
||
253 |