Skip to content

Commit

Permalink
Smile-SA#146 Feature: Allow database URL in config
Browse files Browse the repository at this point in the history
  • Loading branch information
amenk committed Dec 13, 2024
1 parent be5544b commit e39dbc4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
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.
*/
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']);
}

}

0 comments on commit e39dbc4

Please sign in to comment.