Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.

Commit d643c77

Browse files
committed
Command Fixture: Add requirement
1 parent e04f5e0 commit d643c77

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

.php-cs-fixer.dist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
'php_unit_strict' => true,
3535
'phpdoc_order' => true,
3636
'semicolon_after_instruction' => true,
37-
'single_blank_line_before_namespace' => false,
37+
'blank_lines_before_namespace' => false,
3838
'single_import_per_statement' => false,
3939
'single_trait_insert_per_statement' => false,
4040
'strict_comparison' => true,
4141
'strict_param' => true,
4242
])
4343
->setFinder($finder)
4444
->setCacheFile(__DIR__.'/var/cache/.php_cs.cache')
45-
;
45+
;

src/Command/FixtureCommand.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
11
<?php
2+
23
namespace App\Command;
34

45
use Symfony\Component\Console\Attribute\AsCommand;
56
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputInterface;
78
use 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
)]
1316
class 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

Comments
 (0)