-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Olivier Dolbeau
committed
Jun 4, 2019
1 parent
cfda603
commit 9a8bcf8
Showing
21 changed files
with
251 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command; | ||
|
||
use Shapin\Datagen\DBAL\Loader; | ||
use Doctrine\DBAL\Connection; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class DBALLoadCommand extends Command | ||
{ | ||
private $connection; | ||
private $loader; | ||
|
||
public function __construct(Connection $connection, Loader $loader) | ||
{ | ||
$this->connection = $connection; | ||
$this->loader = $loader; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('shapin:datagen:dbal:load') | ||
->setDescription('Load the DBAL schema.') | ||
->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)') | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title('Create database schema with DBAL.'); | ||
|
||
$groups = $input->getOption('groups', []); | ||
|
||
$statements = $this->loader->getSchema($groups)->toSql($this->connection->getDatabasePlatform()); | ||
foreach ($statements as $statement) { | ||
$this->connection->query($statement); | ||
} | ||
|
||
$io->success('Schema created successfully.'); | ||
|
||
foreach ($this->loader->getFixtures($groups) as $fixture) { | ||
$this->connection->insert($fixture[0], $fixture[1], $fixture[2]); | ||
} | ||
|
||
$io->success('Fixtures created successfully.'); | ||
} | ||
} |
86 changes: 0 additions & 86 deletions
86
src/Bridge/Symfony/Bundle/Command/DBALSchemaCreateCommand.php
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/Bridge/Symfony/Bundle/DependencyInjection/Compiler/DBALTablePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shapin\Datagen\Bridge\Symfony\Bundle\DependencyInjection\Compiler; | ||
|
||
use Shapin\Datagen\DBAL\Loader; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class DBALTablePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$loader = $container->getDefinition(Loader::class); | ||
|
||
foreach ($container->findTaggedServiceIds('shapin_datagen.dbal_table') as $id => $tags) { | ||
$groups = []; | ||
foreach ($tags as $attributes) { | ||
if (isset($attributes['group'])) { | ||
$groups[] = $attributes['group']; | ||
} | ||
} | ||
$loader->addMethodCall('addTable', [new Reference($id), $groups]); | ||
} | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shapin\Datagen\DBAL; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
|
||
class Loader | ||
{ | ||
private $tables = []; | ||
private $groups = []; | ||
|
||
public function addTable(Table $table, array $groups = []): void | ||
{ | ||
$this->tables[$table->getTableName()] = $table; | ||
|
||
foreach ($groups as $group) { | ||
if (!isset($this->groups[$group])) { | ||
$this->groups[$group] = []; | ||
} | ||
$this->groups[$group][] = $table->getTableName(); | ||
} | ||
} | ||
|
||
public function getSchema(array $groups = []): Schema | ||
{ | ||
$schema = new Schema(); | ||
|
||
foreach ($this->getTables() as $table) { | ||
$table->addTableToSchema($schema); | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
public function getFixtures(array $groups = []): iterable | ||
{ | ||
foreach ($this->getTables($groups) as $table) { | ||
$tableName = $table->getTableName(); | ||
$types = $table->getTypes(); | ||
|
||
foreach ($table->getRows() as $row) { | ||
yield [$tableName, $row, $types]; | ||
} | ||
} | ||
} | ||
|
||
private function getTables(array $groups = []): array | ||
{ | ||
// Check that all groups exists. | ||
foreach ($groups as $groups) { | ||
if (!isset($this->groups[$group])) { | ||
throw new \InvalidArgumentException(sprintf('Unknown group %s. Available: [%s]', $group, implode(', ', array_keys($this->groups)))); | ||
} | ||
} | ||
|
||
if (0 === count($groups)) { | ||
$tables = $this->tables; | ||
} else { | ||
$tables = []; | ||
foreach ($groups as $group) { | ||
foreach ($this->groups[$group] as $tableInGroup) { | ||
$tables[] = $this->tables[$tableInGroup]; | ||
} | ||
} | ||
} | ||
|
||
// Order all tables | ||
usort($tables, function ($a, $b) { | ||
if ($a->getOrder() === $b->getOrder()) { | ||
return 0; | ||
} | ||
|
||
return $a->getOrder() < $b->getOrder() ? -1 : 1; | ||
}); | ||
|
||
return $tables; | ||
} | ||
} |
Oops, something went wrong.