-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
130 additions
and
56 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
51 changes: 51 additions & 0 deletions
51
src/DAMA/DoctrineTestBundle/DependencyInjection/RegisterDoctrineEventListenersPass.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,51 @@ | ||
<?php | ||
|
||
namespace DAMA\DoctrineTestBundle\DependencyInjection; | ||
|
||
use DAMA\DoctrineTestBundle\Doctrine\DBAL\PostConnectEventListener; | ||
use Doctrine\DBAL\Events; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
final class RegisterDoctrineEventListenersPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
/** @var bool|array $enableStaticConnectionsConfig */ | ||
$enableStaticConnectionsConfig = $container->getParameter('dama.'.Configuration::ENABLE_STATIC_CONNECTION); | ||
|
||
$connectionNames = array_keys($container->getParameter('doctrine.connections')); | ||
if (is_array($enableStaticConnectionsConfig)) { | ||
$this->validateConnectionNames(array_keys($enableStaticConnectionsConfig), $connectionNames); | ||
} | ||
|
||
$enabledConnections = []; | ||
|
||
foreach ($connectionNames as $name) { | ||
if ($enableStaticConnectionsConfig === true | ||
|| isset($enableStaticConnectionsConfig[$name]) && $enableStaticConnectionsConfig[$name] === true | ||
) { | ||
$enabledConnections[] = $name; | ||
$postConnectListenerDef = new Definition(PostConnectEventListener::class); | ||
$postConnectListenerDef->addTag( | ||
'doctrine.event_listener', | ||
['event' => Events::postConnect, 'connection' => $name, 'priority' => 1000] | ||
); | ||
$container->setDefinition('dama.doctrine.dbal.post_connect_event_listener.'.$name, $postConnectListenerDef); | ||
} | ||
} | ||
|
||
$container->setParameter('dama.doctrine.enabled_connections', $enabledConnections); | ||
$container->getParameterBag()->remove('dama.'.Configuration::ENABLE_STATIC_CONNECTION); | ||
} | ||
|
||
private function validateConnectionNames(array $configNames, array $existingNames): void | ||
{ | ||
$unknown = array_diff($configNames, $existingNames); | ||
|
||
if (count($unknown)) { | ||
throw new \InvalidArgumentException(sprintf('Unknown doctrine dbal connection name(s): %s.', implode(', ', $unknown))); | ||
} | ||
} | ||
} |
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
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