1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command; |
6
|
|
|
|
7
|
|
|
use Shapin\Datagen\DBAL\Loader; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
class DBALLoadCommand extends Command |
16
|
|
|
{ |
17
|
|
|
private $connection; |
18
|
|
|
private $loader; |
19
|
|
|
|
20
|
3 |
|
public function __construct(Connection $connection, Loader $loader) |
21
|
|
|
{ |
22
|
3 |
|
$this->connection = $connection; |
23
|
3 |
|
$this->loader = $loader; |
24
|
|
|
|
25
|
3 |
|
parent::__construct(); |
26
|
3 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
3 |
|
protected function configure() |
32
|
|
|
{ |
33
|
|
|
$this |
34
|
3 |
|
->setName('shapin:datagen:dbal:load') |
35
|
3 |
|
->setDescription('Load the DBAL schema.') |
36
|
3 |
|
->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)') |
37
|
3 |
|
->addOption('exclude-groups', 'G', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be excluded? (default: none)') |
38
|
3 |
|
->addOption('schema-only', null, InputOption::VALUE_NONE, 'Load only schema.') |
39
|
3 |
|
->addOption('fixtures-only', null, InputOption::VALUE_NONE, 'Load only fixtures.') |
40
|
|
|
; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
3 |
|
$io = new SymfonyStyle($input, $output); |
49
|
3 |
|
$io->title('Create database schema with DBAL.'); |
50
|
|
|
|
51
|
3 |
|
$groups = [] !== $input->getOption('groups') ? $input->getOption('groups') : $this->loader->getGroups(); |
52
|
3 |
|
$groups = array_diff($groups, $input->getOption('exclude-groups', [])); |
|
|
|
|
53
|
|
|
|
54
|
3 |
|
if (0 === count($groups) && 0 < count($this->loader->getGroups())) { |
55
|
|
|
$io->warning('No group to load.'); |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if (!$input->getOption('fixtures-only')) { |
61
|
2 |
|
$statements = $this->loader->getSchema($groups)->toSql($this->connection->getDatabasePlatform()); |
62
|
2 |
|
foreach ($statements as $statement) { |
63
|
|
|
$this->connection->query($statement); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$io->success('Schema created successfully.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
if (!$input->getOption('schema-only')) { |
70
|
2 |
|
foreach ($this->loader->getFixtures($groups) as $fixture) { |
71
|
|
|
$this->connection->insert($fixture[0], $fixture[1], $fixture[2]); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$io->success('Fixtures created successfully.'); |
75
|
|
|
} |
76
|
3 |
|
} |
77
|
|
|
} |
78
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.