-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from naitsirch/use_doctrine_config
Reuse doctrine's connection params
- Loading branch information
Showing
10 changed files
with
180 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of TheCadien/SuluImportExportBundle. | ||
* | ||
* (c) Oliver Kossin | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace TheCadien\Bundle\SuluImportExportBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* This compiler pass is responsible for fetching the connection params | ||
* of the DBAL and passes them to our export and import service. | ||
*/ | ||
class DbConnectionPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$connectionName = $container->getParameter('sulu_import_export_bundle.dbal_connection'); | ||
|
||
if (!$connectionName) { | ||
$connectionName = $container->getParameter('doctrine.default_connection'); | ||
} | ||
|
||
$connections = $container->getParameter('doctrine.connections'); | ||
|
||
if (!isset($connections[$connectionName])) { | ||
throw new \InvalidArgumentException(sprintf('There is no doctrine connection configured which is named "%s". Available names are "%s".', $connectionName, implode('", "', array_keys($connections)))); | ||
} | ||
|
||
$connectionId = $connections[$connectionName]; | ||
$connectionParams = $container->getDefinition($connectionId)->getArgument(0); | ||
|
||
$exportService = $container->getDefinition('sulu.service.export'); | ||
$exportService->setArgument('$databaseParams', $connectionParams); | ||
|
||
$importService = $container->getDefinition('sulu.service.import'); | ||
$importService->setArgument('$databaseParams', $connectionParams); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of TheCadien/SuluImportExportBundle. | ||
* | ||
* (c) Oliver Kossin | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace TheCadien\Bundle\SuluImportExportBundle\Helper; | ||
|
||
class DbConnectionParamsNormalizer | ||
{ | ||
/** | ||
* Normalizes the array with the connection params to a defined structure. | ||
* | ||
* @param array $connectionParams | ||
* The database connection params for the connection. Should be passed | ||
* as an associative array with the key 'url' or with the keys 'host', | ||
* 'user', 'dbname' and 'password'. | ||
* If an URL is used, it should follow the format | ||
* 'mysql://<user>:<password>@<host>/<dbname>'. | ||
* | ||
* @throws \InvalidArgumentException if the passed array does not contain the required fields | ||
* | ||
* @return array Returns an array with the keys 'host', 'user', 'dbname' and 'password'. | ||
* The latter may be null. | ||
*/ | ||
public static function normalize(array $connectionParams): array | ||
{ | ||
if (empty($connectionParams['url'])) { | ||
$diff = array_diff_key(['user', 'dbname'], $connectionParams); | ||
|
||
if (\count($diff) > 0) { | ||
throw new \InvalidArgumentException(sprintf('The following keys are missing from the $connectionParams: ', implode(', ', $diff))); | ||
} | ||
|
||
return [ | ||
'host' => $connectionParams['host'] ?? 'localhost', | ||
'user' => $connectionParams['user'], | ||
'dbname' => $connectionParams['dbname'], | ||
'password' => $connectionParams['password'] ?? null, | ||
]; | ||
} | ||
|
||
$urlParts = parse_url($connectionParams['url']); | ||
|
||
if (false === $urlParts) { | ||
throw new \InvalidArgumentException('The connection URL is not a valid URL that follows the format "<schema>://<user>:<password>@<host>/<dbname>'); | ||
} | ||
|
||
return [ | ||
'host' => $urlParts['host'] ?? 'localhost', | ||
'user' => $urlParts['user'], | ||
'dbname' => substr($urlParts['path'], 1), | ||
'password' => $urlParts['pass'] ?? null, | ||
]; | ||
} | ||
} |
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
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