Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#146 Feature: Allow database URL in config #147

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
"type": "object",
"required": true,
"properties": {
"url": {
"type": "string"
},
"driver": {
"type": "string",
"enum": ["pdo_mysql"]
Expand Down
3 changes: 3 additions & 0 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ services:
config.compiler.processor.version:
class: 'Smile\GdprDump\Config\Compiler\Processor\VersionProcessor'

config.compiler.processor.database_url:
class: 'Smile\GdprDump\Config\Compiler\Processor\DatabaseUrlProcessor'

converter.builder:
class: 'Smile\GdprDump\Converter\ConverterBuilder'
arguments:
Expand Down
49 changes: 49 additions & 0 deletions src/Config/Compiler/Processor/DatabaseUrlProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Config\Compiler\Processor;

use Smile\GdprDump\Config\Compiler\CompileException;
use Smile\GdprDump\Config\ConfigInterface;

class DatabaseUrlProcessor implements ProcessorInterface
{
/**
* Replace environment variable placeholders (e.g. "%env(DB_HOST)%")
*/
public function process(ConfigInterface $config): void
{
$data = $config->toArray();
$data['database'] = $this->processDatabaseNode($config->toArray()['database'] ?? []);
$config->reset($data);
}

/**
* Process a config item.
*
* @throws CompileException
*/
private function processDatabaseNode(array $data): array
{
if (isset($data['url'])) {
$parsed = parse_url($data['url']);
$mapped = [
'name' => ltrim($parsed['path'], '/'),
'user' => $parsed['user'] ?? null,
'password' => $parsed['pass'] ?? null,
'host' => $parsed['host'] ?? null,
'port' => $parsed['port'] ?? null,
'driver' => 'pdo_' . ltrim($parsed['scheme'], 'pdo_'),
];

foreach($mapped as $key => $value) {
if (!isset($data[$key]) && (!empty($mapped[$key]))) {
$data[$key] = $value;
}
}
}

return $data;
}
}
34 changes: 34 additions & 0 deletions tests/unit/Config/Compiler/Processor/DatabaseUrlProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Tests\Unit\Config\Compiler\Processor;

use Smile\GdprDump\Config\Compiler\Processor\DatabaseUrlProcessor;
use Smile\GdprDump\Config\Config;
use Smile\GdprDump\Tests\Unit\TestCase;

class DatabaseUrlProcessorTest extends TestCase
{
/**
* Assert that environment variables are processed successfully.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy & paste issue

*/
public function testEnvVarProcessor(): void
{
$data = [
'database' =>
[
'password' => 'another_secret_password',
'url' => 'mysql://foo:secret_password@localhost/databasename',
]
];

$config = new Config($data);
$processor = new DatabaseUrlProcessor();
$processor->process($config);

$this->assertSame('localhost', $config->get('database')['host']);
$this->assertSame('another_secret_password', $config->get('database')['password']);
}

}
Loading