alex-patterson-webdev /
laminas-doctrine-fixtures
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Arp\LaminasDoctrineFixtures\Command; |
||
| 6 | |||
| 7 | use Arp\LaminasDoctrineFixtures\Service\Executor; |
||
| 8 | use Doctrine\Common\DataFixtures\FixtureInterface; |
||
| 9 | use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
||
| 10 | use Symfony\Component\Console\Command\Command; |
||
| 11 | use Symfony\Component\Console\Input\InputInterface; |
||
| 12 | use Symfony\Component\Console\Input\InputOption; |
||
| 13 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @author Alex Patterson <[email protected]> |
||
| 17 | * @package Arp\LaminasDoctrineFixtures\Command |
||
| 18 | */ |
||
| 19 | class ImportCommand extends Command |
||
| 20 | {
|
||
| 21 | /** |
||
| 22 | * @var FixtureInterface[] |
||
| 23 | */ |
||
| 24 | private $fixtures; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Executor |
||
| 28 | */ |
||
| 29 | private $executor; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ORMPurger|null |
||
| 33 | */ |
||
| 34 | private $purger; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param FixtureInterface[] $fixtures |
||
| 38 | * @param Executor $executor |
||
| 39 | * @param ORMPurger|null $purger |
||
| 40 | */ |
||
| 41 | public function __construct(array $fixtures, Executor $executor, ORMPurger $purger = null) |
||
| 42 | {
|
||
| 43 | $this->fixtures = $fixtures; |
||
| 44 | $this->executor = $executor; |
||
| 45 | $this->purger = $purger; |
||
| 46 | |||
| 47 | parent::__construct(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Configure the command's options. |
||
| 52 | */ |
||
| 53 | protected function configure(): void |
||
| 54 | {
|
||
| 55 | parent::configure(); |
||
| 56 | |||
| 57 | $this->setName('data-fixture:import')
|
||
| 58 | ->setDescription('Import Data Fixtures')
|
||
| 59 | ->setHelp('The import command Imports data-fixtures')
|
||
| 60 | ->addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.');
|
||
| 61 | |||
| 62 | if (null !== $this->purger) {
|
||
| 63 | $this->addOption( |
||
| 64 | 'purge-with-truncate', |
||
| 65 | null, |
||
| 66 | InputOption::VALUE_NONE, |
||
| 67 | 'Truncate tables before inserting data' |
||
| 68 | ); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param InputInterface $input |
||
| 74 | * @param OutputInterface $output |
||
| 75 | * |
||
| 76 | * @return int|void|null |
||
| 77 | */ |
||
| 78 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 79 | {
|
||
| 80 | $output->writeln('Executing data fixtures...');
|
||
| 81 | |||
| 82 | $purgeMode = $input->getOption('purge-with-truncate');
|
||
| 83 | |||
| 84 | if (null !== $this->purger && $purgeMode) {
|
||
| 85 | $output->writeln(sprintf('Purging existing database data'));
|
||
| 86 | |||
| 87 | $this->purger->setPurgeMode((2 === $purgeMode) ? 2 : 1); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 88 | } |
||
| 89 | |||
| 90 | $this->executor->execute($this->fixtures, $input->getOption('append') ? true : false);
|
||
| 91 | |||
| 92 | $output->writeln(sprintf('Completed execution of \'%d\' fixtures', count($this->fixtures)));
|
||
| 93 | |||
| 94 | return 0; |
||
| 95 | } |
||
| 96 | } |
||
| 97 |