Skip to content

Commit

Permalink
Refactor dumper config initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Mar 20, 2024
1 parent 3b15fe1 commit eb0ee65
Show file tree
Hide file tree
Showing 26 changed files with 384 additions and 235 deletions.
3 changes: 3 additions & 0 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ services:
arguments:
- !tagged_iterator config.compiler_processor

config.compiler.dump_output:
class: 'Smile\GdprDump\Config\Compiler\Processor\DumpOutputProcessor'

config.compiler.processor.env_var:
class: 'Smile\GdprDump\Config\Compiler\Processor\EnvVarProcessor'

Expand Down
28 changes: 28 additions & 0 deletions src/Config/Compiler/Processor/DumpOutputProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Config\Compiler\Processor;

use Smile\GdprDump\Config\ConfigInterface;

class DumpOutputProcessor implements ProcessorInterface
{
/**
* Process date placeholders in dump output (e.g. "dump-{Y-m-d-H.i.s}.sql").
*/
public function process(ConfigInterface $config): void
{
$dumpSettings = $config->get('dump', []);

if (array_key_exists('output', $dumpSettings)) {
$dumpSettings['output'] = preg_replace_callback(
'/{([^}]+)}/',
fn (array $matches) => date($matches[1]),
$dumpSettings['output']
);

$config->set('dump', $dumpSettings);
}
}
}
2 changes: 1 addition & 1 deletion src/Config/Compiler/Processor/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EnvVarProcessor implements ProcessorInterface
];

/**
* @inheritdoc
* Replace environment variable placeholders (e.g. "%env(DB_HOST)%")
*/
public function process(ConfigInterface $config): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Compiler/Processor/VersionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class VersionProcessor implements ProcessorInterface
{
/**
* @inheritdoc
* Process the "if_version" parameter.
*/
public function process(ConfigInterface $config): void
{
Expand Down
6 changes: 5 additions & 1 deletion src/Database/TableDependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ private function buildDependencyTree(): void
*/
private function isForeignKeyIgnored(ForeignKey $foreignKey): bool
{
return in_array($foreignKey->getConstraintName(), $this->config->getIgnoredForeignKeys(), true);
return in_array(
$foreignKey->getConstraintName(),
$this->config->getFilterPropagationSettings()->getIgnoredForeignKeys(),
true
);
}
}
20 changes: 20 additions & 0 deletions src/Dumper/Config/Definition/FakerSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Definition;

class FakerSettings
{
public function __construct(private string $locale)
{
}

/**
* Get the faker locale.
*/
public function getLocale(): string
{
return $this->locale;
}
}
28 changes: 28 additions & 0 deletions src/Dumper/Config/Definition/FilterPropagationSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Definition;

class FilterPropagationSettings
{
public function __construct(private bool $enabled, private array $ignoredForeignKeys)
{
}

/**
* Checker whether filter propagation is enabled.
*/
public function isEnabled(): bool
{
return $this->enabled;
}

/**
* Get foreign keys to ignore when propagating filters to table dependencies.
*/
public function getIgnoredForeignKeys(): array
{
return $this->ignoredForeignKeys;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Table\Filter;
namespace Smile\GdprDump\Dumper\Config\Definition\Table;

use UnexpectedValueException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Table\Filter;
namespace Smile\GdprDump\Dumper\Config\Definition\Table;

use UnexpectedValueException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Table;
namespace Smile\GdprDump\Dumper\Config\Definition;

use Smile\GdprDump\Dumper\Config\Table\Filter\Filter;
use Smile\GdprDump\Dumper\Config\Table\Filter\SortOrder;
use Smile\GdprDump\Dumper\Config\Definition\Table\Filter;
use Smile\GdprDump\Dumper\Config\Definition\Table\SortOrder;
use Smile\GdprDump\Dumper\Config\Validation\ValidationException;
use Smile\GdprDump\Dumper\Config\Validation\WhereExprValidator;
use UnexpectedValueException;

class TableConfig
{
private WhereExprValidator $whereExprValidator;
private string $name;
private ?string $where = null;
private ?int $limit = null;
Expand All @@ -31,7 +31,6 @@ class TableConfig

public function __construct(string $tableName, array $tableConfig)
{
$this->whereExprValidator = new WhereExprValidator();
$this->name = $tableName;
$this->prepareConfig($tableConfig);
}
Expand Down Expand Up @@ -145,6 +144,8 @@ private function prepareConfig(array $tableData): void

/**
* Prepare the table filters.
*
* @throws ValidationException
*/
private function prepareFilters(array $tableData): void
{
Expand All @@ -158,7 +159,8 @@ private function prepareFilters(array $tableData): void
// New way of declaring table filters (`where` parameter)
$whereCondition = (string) ($tableData['where'] ?? '');
if ($whereCondition !== '') {
$this->whereExprValidator->validate($whereCondition);
$whereExprValidator = new WhereExprValidator();
$whereExprValidator->validate($whereCondition);
$this->where = $whereCondition;
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/Dumper/Config/Definition/TableConfigCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Dumper\Config\Definition;

use ArrayIterator;
use IteratorAggregate;
use Traversable;
use UnexpectedValueException;

/**
* @implements IteratorAggregate<TableConfig>
*/
class TableConfigCollection implements IteratorAggregate
{
/**
* @var TableConfig[]
*/
private array $items = [];

/**+
* Add an item.
*/
public function add(TableConfig $tableConfig): void
{
$this->items[$tableConfig->getName()] = $tableConfig;
}

/**
* Get an item.
*
* @throws UnexpectedValueException
*/
public function get(string $name): TableConfig
{
return $this->has($name)
? $this->items[$name]
: throw new UnexpectedValueException(sprintf('The table "%s" is not defined.', $name));
}

/**
* Check whether an item exists.
*/
public function has(string $name): bool
{
return array_key_exists($name, $this->items);
}

/**
* Get all items.
*
* @return TableConfig[]
*/
public function all(): array
{
return $this->items;
}

/**
* @return ArrayIterator<string, TableConfig>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
}
}
Loading

0 comments on commit eb0ee65

Please sign in to comment.