forked from Smile-SA/gdpr-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smile-SA#146 Feature: Allow database URL in config
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
tests/unit/Config/Compiler/Processor/DatabaseUrlProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
|
||
} |