Skip to content

Commit

Permalink
issue #155 - move standalone console tool into Bridge/Standalone subf…
Browse files Browse the repository at this point in the history
…older
  • Loading branch information
pounard committed May 30, 2024
1 parent da27da4 commit 4cc8948
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bin/db-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MakinaCorpus\DbToolsBundle;

use MakinaCorpus\DbToolsBundle\Helper\Cli\Bootstrap;
use MakinaCorpus\DbToolsBundle\Bridge\Standalone\Bootstrap;

(static function (): void {
$autoloadFiles = [
Expand Down
2 changes: 1 addition & 1 deletion docs/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A complete example of this file can be found in the bundle sources in: `vendor/m
:::

For detailed information about configuration options, please see the
[configuration reference](../configuration/reference).
[configuration reference](configuration/reference).

## Backup configuration

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

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Helper\Cli;
namespace MakinaCorpus\DbToolsBundle\Bridge\Standalone;

use Composer\InstalledVersions;
use MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\DbToolsBundle\Backupper\BackupperFactory;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\AnonymizeCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\AnonymizerListCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\CleanCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\ConfigDumpCommand;
use MakinaCorpus\DbToolsBundle\Bridge\Symfony\DependencyInjection\DbToolsConfiguration;
use MakinaCorpus\DbToolsBundle\Command\BackupCommand;
use MakinaCorpus\DbToolsBundle\Command\CheckCommand;
use MakinaCorpus\DbToolsBundle\Command\RestoreCommand;
use MakinaCorpus\DbToolsBundle\Command\StatsCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\AnonymizeCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\AnonymizerListCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\CleanCommand;
use MakinaCorpus\DbToolsBundle\Command\Anonymization\ConfigDumpCommand;
use MakinaCorpus\DbToolsBundle\Database\DatabaseSessionRegistry;
use MakinaCorpus\DbToolsBundle\Database\StandaloneDatabaseSessionRegistry;
use MakinaCorpus\DbToolsBundle\Error\ConfigurationException;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactory;
use MakinaCorpus\DbToolsBundle\Stats\StatsProviderFactory;
use MakinaCorpus\DbToolsBundle\Storage\Storage;
use MakinaCorpus\QueryBuilder\Error\ConfigurationError;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Config\Definition\Processor;
Expand Down Expand Up @@ -110,7 +110,7 @@ public static function createApplication(): Application
],
'anonymization:run' => [
fn (Context $context) => new AnonymizeCommand(
defaultConnectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
connectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
restorerFactory: $context->restorerFactory,
backupperFactory: $context->backupperFactory,
anonymizatorFactory: $context->anonymizatorFactory,
Expand All @@ -121,7 +121,7 @@ public static function createApplication(): Application
],
'database:backup' => [
fn (Context $context) => new BackupCommand(
defaultConnectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
connectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
backupperFactory: $context->backupperFactory,
storage: $context->storage,
),
Expand All @@ -139,7 +139,7 @@ public static function createApplication(): Application
],
'database:restore' => [
fn (Context $context) => new RestoreCommand(
defaultConnectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
connectionName: $context->databaseSessionRegistry->getDefaultConnectionName(),
restorerFactory: $context->restorerFactory,
storage: $context->storage,
),
Expand Down Expand Up @@ -323,6 +323,7 @@ private static function configParse(array $config, array $files, LoggerInterface
$processor = new Processor();

$config = $processor->processConfiguration($configuration, $configs);
$config = DbToolsConfiguration::appendPostConfig($config);

return $config;
}
Expand Down Expand Up @@ -405,11 +406,10 @@ private static function createAnonymizeRegistry(array $config): AnonymizerRegist
*/
private static function createDatabaseSessionRegistry(array $config): DatabaseSessionRegistry
{
if (empty($config['connections'])) {
throw new ConfigurationError("No database connection found, this means that either you forgot it into your configuration file, or no configuration files were found. Please run using the -vvv switch for more information.");
}

return new StandaloneDatabaseSessionRegistry($config['connections'], $config['default_connection']);
// Do not crash on initialization, it will crash later when a connection
// will be request instead: this allows commands that don't act on
// database (such as anonymizer list) to work even if not configured.
return new StandaloneDatabaseSessionRegistry($config['connections'] ?? [], $config['default_connection']);
}

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

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Helper\Cli;
namespace MakinaCorpus\DbToolsBundle\Bridge\Standalone;

use MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Helper\Cli;
namespace MakinaCorpus\DbToolsBundle\Bridge\Standalone;

use MakinaCorpus\DbToolsBundle\Bridge\Symfony\DependencyInjection\DbToolsConfiguration;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down
17 changes: 17 additions & 0 deletions src/Bridge/Symfony/DependencyInjection/DbToolsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ protected function getDefaultStoragePath(): ?string
return '%kernel.project_dir%/var/db_tools';
}

/**
* Append values in configuration we cannot set a default.
*
* For example, 'anonymizer_paths', if set by the user, will loose the
* default anonymizer paths, and we need them to be set in all cases.
*
* So we act after configuration has been processed and restore missing
* values from here. This also allows the standalone configuration doing
* it outside of Symfony extension context.
*/
public static function appendPostConfig(array $config): array
{
$config['anonymizer_paths'][] = \realpath(\dirname(__DIR__, 3)) . '/Anonymization/Anonymizer';

return $config;
}

#[\Override]
public function getConfigTreeBuilder(): TreeBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function load(array $configs, ContainerBuilder $container): void
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$config = DbToolsConfiguration::appendPostConfig($config);

$loader = new YamlFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config'));
$loader->load('services.yaml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function processYamlConfiguration(array|string $dataOrFilename): array
public function testConfigurationMinimal(): array
{
$result = $this->processYamlConfiguration(
__DIR__ . '/../../Resources/config/packages/db_tools_min.yaml'
\dirname(__DIR__, 4) . '/Resources/config/packages/db_tools_min.yaml'
);

self::assertSame(
Expand Down Expand Up @@ -63,7 +63,7 @@ public function testConfigurationMinimal(): array
public function testConfigurationAlternative1(): array
{
$result = $this->processYamlConfiguration(
__DIR__ . '/../../Resources/config/packages/db_tools_alt1.yaml'
\dirname(__DIR__, 4) . '/Resources/config/packages/db_tools_alt1.yaml'
);

self::assertSame(
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testConfigurationAlternative1(): array
public function testConfigurationAlternative2(): array
{
$result = $this->processYamlConfiguration(
__DIR__ . '/../../Resources/config/packages/db_tools_alt2.yaml'
\dirname(__DIR__, 4) . '/Resources/config/packages/db_tools_alt2.yaml'
);

self::assertSame(
Expand Down

0 comments on commit 4cc8948

Please sign in to comment.