diff --git a/app/config/schema.json b/app/config/schema.json index ef55d6f7..8bf1b6c5 100644 --- a/app/config/schema.json +++ b/app/config/schema.json @@ -73,6 +73,9 @@ "type": "object", "required": true, "properties": { + "url": { + "type": "string" + }, "driver": { "type": "string", "enum": ["pdo_mysql"] diff --git a/app/config/services.yaml b/app/config/services.yaml index bb913370..2c39356f 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -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: diff --git a/src/Config/Compiler/Processor/DatabaseUrlProcessor.php b/src/Config/Compiler/Processor/DatabaseUrlProcessor.php new file mode 100644 index 00000000..26a24b38 --- /dev/null +++ b/src/Config/Compiler/Processor/DatabaseUrlProcessor.php @@ -0,0 +1,49 @@ +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; + } +} diff --git a/tests/unit/Config/Compiler/Processor/DatabaseUrlProcessorTest.php b/tests/unit/Config/Compiler/Processor/DatabaseUrlProcessorTest.php new file mode 100644 index 00000000..b6c9a677 --- /dev/null +++ b/tests/unit/Config/Compiler/Processor/DatabaseUrlProcessorTest.php @@ -0,0 +1,34 @@ + + [ + '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']); + } + +}