-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
LoadDataFixturesCommand.php
163 lines (135 loc) · 4.88 KB
/
LoadDataFixturesCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php declare(strict_types = 1);
namespace Nettrine\Fixtures\Command;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\ManagerRegistry;
use InvalidArgumentException;
use Nettrine\Fixtures\Loader\FixturesLoader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* @see http://symfony.com/doc/2.2/bundles/DoctrineFixturesBundle/index.html
*/
class LoadDataFixturesCommand extends Command
{
/** @var FixturesLoader */
private $loader;
/** @var ManagerRegistry */
private $managerRegistry;
/** @var string */
protected static $defaultName = 'doctrine:fixtures:load';
public function __construct(FixturesLoader $loader, ManagerRegistry $managerRegistry)
{
parent::__construct();
$this->loader = $loader;
$this->managerRegistry = $managerRegistry;
}
/**
* Configures the current command.
*/
protected function configure(): void
{
$this
->setName(static::$defaultName)
->setDescription('Load data fixtures to your database.')
->addOption(
'fixtures',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'The directory to load data fixtures from.'
)
->addOption(
'append',
null,
InputOption::VALUE_NONE,
'Append the data fixtures instead of deleting all data from the database first.'
)
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
->addOption(
'purge-with-truncate',
null,
InputOption::VALUE_NONE,
'Purge data by using a database-level TRUNCATE statement'
)
// ->addOption('multiple-transactions', NULL, InputOption::VALUE_NONE,
// 'Use one transaction per fixture file instead of a single transaction for all')
->setHelp('
The <info>doctrine:fixtures:load</info> command loads data fixtures from your config:
<info>doctrine:fixtures:load</info>
You can also optionally specify the path to fixtures with the <info>--fixtures</info> option:
<info>doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info>
If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option:
<info>doctrine:fixtures:load --append</info>
By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from
the database. If you want to use a TRUNCATE statement instead you can use the <info>--purge-with-truncate</info> flag:
<info>doctrine:fixtures:load --purge-with-truncate</info>
');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string|null $em */
$em = $input->getOption('em');
/** @var EntityManager $em */
$em = $this->managerRegistry->getManager($em);
$append = $input->getOption('append');
if ($input->isInteractive() && $append !== null && $append !== '') {
if (!$this->askConfirmation(
$input,
$output,
'<question>Careful, database will be purged. Do you want to continue y/N ?</question>',
false
)
) {
return 1;
}
}
$dirOrFile = $input->getOption('fixtures');
if ($dirOrFile !== null && $dirOrFile !== '' && !(is_array($dirOrFile) && count($dirOrFile) === 0)) {
$paths = is_array($dirOrFile) ? $dirOrFile : [$dirOrFile];
$this->loader->loadPaths($paths);
} else {
$this->loader->load();
$paths = $this->loader->getPaths();
}
$fixtures = $this->loader->getFixtures();
if ($fixtures === []) {
throw new InvalidArgumentException(
sprintf(
'Could not find any fixtures to load in: %s',
"\n\n- " . implode("\n- ", $paths)
)
);
}
$purgeTruncate = $input->getOption('purge-with-truncate');
$purger = new ORMPurger($em);
$purger->setPurgeMode($purgeTruncate !== null && $purgeTruncate !== '' ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE);
$executor = new ORMExecutor($em, $purger);
$executor->setLogger(function ($message) use ($output): void {
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message));
});
$executor->execute($fixtures, (bool) $append);
//$executor->execute($fixtures, $input->getOption('append'), $input->getOption('multiple-transactions'));
return 0;
}
private function askConfirmation(
InputInterface $input,
OutputInterface $output,
string $question,
bool $default
): bool
{
$set = $this->getHelperSet();
if ($set === null) {
return false;
}
/** @var QuestionHelper $questionHelper */
$questionHelper = $set->get('question');
$question = new ConfirmationQuestion($question, $default);
return (bool) $questionHelper->ask($input, $output, $question);
}
}