1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command; |
6
|
|
|
|
7
|
|
|
use Shapin\Datagen\DBAL\Loader\SchemaLoader; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\Schema\Schema; |
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
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
class DBALSchemaCreateCommand extends Command |
17
|
|
|
{ |
18
|
|
|
private $groups; |
19
|
|
|
private $connection; |
20
|
|
|
|
21
|
1 |
|
public function __construct(Connection $connection, array $groups) |
22
|
|
|
{ |
23
|
1 |
|
$this->connection = $connection; |
24
|
1 |
|
$this->groups = $groups; |
25
|
|
|
|
26
|
1 |
|
parent::__construct(); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
1 |
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
1 |
|
->setName('datagen:dbal:schema:create') |
36
|
1 |
|
->setDescription('Create the DBAL schema.') |
37
|
1 |
|
->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)') |
38
|
|
|
; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
1 |
|
$io = new SymfonyStyle($input, $output); |
47
|
1 |
|
$io->title('Create database schema with DBAL.'); |
48
|
|
|
|
49
|
1 |
|
$groups = $this->groups; |
50
|
1 |
View Code Duplication |
if ([] !== $wantedGroups = $input->getOption('groups')) { |
|
|
|
|
51
|
|
|
$groups = []; |
52
|
|
|
foreach ($wantedGroups as $wantedGroup) { |
|
|
|
|
53
|
|
|
if (!array_key_exists($wantedGroup, $this->groups)) { |
54
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $wantedGroup, implode('", "', array_keys($this->groups)))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$groups[$wantedGroup] = $this->groups[$wantedGroup]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$schemaLoader = new SchemaLoader(new Schema()); |
62
|
1 |
|
foreach ($groups as $path) { |
63
|
1 |
|
$schemaLoader->load($path); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$statements = $schemaLoader->getSchema()->toSql($this->connection->getDatabasePlatform()); |
67
|
1 |
|
foreach ($statements as $statement) { |
68
|
1 |
|
$this->connection->query($statement); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$io->success('Schema created successfully.'); |
72
|
1 |
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.