Skip to content

Commit

Permalink
Huge refacto to stop using paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Dolbeau committed Jun 4, 2019
1 parent cfda603 commit 9a8bcf8
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 426 deletions.
63 changes: 63 additions & 0 deletions src/Bridge/Symfony/Bundle/Command/DBALLoadCommand.php
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 src/Bridge/Symfony/Bundle/Command/DBALSchemaCreateCommand.php

This file was deleted.

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 src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ final class ShapinDatagenExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$definition = $container->getDefinition('shapin.datagen.command.dbal_schema_create');
$definition->replaceArgument(2, $config['groups']);
}
}
9 changes: 4 additions & 5 deletions src/Bridge/Symfony/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

<services>

<service id="shapin.datagen.command.dbal_schema_create" class="Shapin\Datagen\Bridge\Symfony\Bundle\Command\DBALSchemaCreateCommand">
<service id="Shapin\Datagen\DBAL\Loader" />

<service id="Shapin\Datagen\Bridge\Symfony\Bundle\Command\DBALLoadCommand">
<argument type="service" id="doctrine.dbal.default_connection" />
<argument type="service" id="shapin.datagen.fixture_loader" />
<argument type="collection"></argument>
<argument type="service" id="Shapin\Datagen\DBAL\Loader" />
<tag name="console.command" />
</service>

<service id="shapin.datagen.fixture_loader" class="Shapin\Datagen\DBAL\Loader\FixtureLoader" />

</services>

</container>
8 changes: 8 additions & 0 deletions src/Bridge/Symfony/Bundle/ShapinDatagenBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

namespace Shapin\Datagen\Bridge\Symfony\Bundle;

use Shapin\Datagen\Bridge\Symfony\Bundle\DependencyInjection\Compiler\DBALTablePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class ShapinDatagenBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new DBALTablePass());
}
}
80 changes: 80 additions & 0 deletions src/DBAL/Loader.php
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;
}
}

0 comments on commit 9a8bcf8

Please sign in to comment.