11<?php
2+
23namespace App \Command ;
34
45use Symfony \Component \Console \Attribute \AsCommand ;
56use Symfony \Component \Console \Command \Command ;
67use Symfony \Component \Console \Input \InputInterface ;
78use Symfony \Component \Console \Output \OutputInterface ;
9+ use Symfony \Component \Console \Style \SymfonyStyle ;
10+ use Symfony \Component \HttpKernel \KernelInterface ;
811
912#[AsCommand(
1013 name: 'app:fixtures ' ,
11- description: 'Load fixtures for all databases ' ,
14+ description: 'Load fixtures for all databases (only for dev environnement) ' ,
1215)]
1316class FixtureCommand extends Command
1417{
1518 // Entity Manager name (groups should be used the same value)
1619 private const LIST_DATABASE = ['default ' , 'second ' ];
1720
21+ /** @var bool[] */
22+ private array $ requirement ;
23+
24+ public function __construct (KernelInterface $ kernel , string $ name = null )
25+ {
26+ parent ::__construct ($ name );
27+
28+ $ this ->requirement = [
29+ 'doctrine_fixture ' => array_key_exists ('DoctrineFixturesBundle ' , $ kernel ->getBundles ()),
30+ 'doctrine_migration ' => array_key_exists ('DoctrineMigrationsBundle ' , $ kernel ->getBundles ()),
31+ 'doctrine_multiple_migration ' => array_key_exists ('DoctrineMigrationsMultipleDatabaseBundle ' , $ kernel ->getBundles ()),
32+ ];
33+ }
34+
1835 protected function execute (InputInterface $ input , OutputInterface $ output ): int
1936 {
37+ $ io = new SymfonyStyle ($ input , $ output );
38+ if (!$ this ->requirement ['doctrine_fixture ' ]) {
39+ $ io ->error ('The bundle DoctrineFixturesBundle is inactive or the APP_ENV value is not dev ' );
40+
41+ return Command::INVALID ;
42+ } elseif (!$ this ->requirement ['doctrine_migration ' ]) {
43+ $ io ->error ('The bundle DoctrineMigrationsBundle is inactive. ' );
44+
45+ return Command::INVALID ;
46+ } elseif (!$ this ->requirement ['doctrine_multiple_migration ' ]) {
47+ $ io ->error ('The bundle DoctrineMigrationsMultipleDatabaseBundle is inactive ' );
48+
49+ return Command::INVALID ;
50+ }
51+
2052 $ console = 'php ' .realpath (__DIR__ .'/../../bin/console ' );
2153 $ command = 'doctrine:fixtures:load ' ;
2254
@@ -25,8 +57,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2557 // $symfonyCommand->run(new ArrayInput(['--em' => 'default', '--group' => 'default', '--no-interaction' => null]), $output);
2658
2759 foreach (self ::LIST_DATABASE as $ database ) {
28- $ output -> write ('Install fixtures for database: ' .$ database );
29- $ output ->writeln ((string ) shell_exec ("{$ console } {$ command } --em= {$ database } --group= {$ database } --no-interaction " ));
60+ $ io -> title ('Install fixtures for database: ' .$ database );
61+ $ io ->writeln ((string ) shell_exec ("{$ console } {$ command } --em= {$ database } --group= {$ database } --no-interaction " ));
3062 }
3163
3264 return Command::SUCCESS ;
0 commit comments