-
-
Notifications
You must be signed in to change notification settings - Fork 455
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
9 changed files
with
468 additions
and
73 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,15 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class AsMiddleware | ||
{ | ||
/** @param string[] $connections */ | ||
public function __construct( | ||
public array $connections = [], | ||
) { | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Middleware\ConnectionNameAwareInterface; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
use function array_keys; | ||
use function array_search; | ||
use function is_subclass_of; | ||
use function sprintf; | ||
|
||
final class MiddlewaresPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$middlewareAbstractDefs = []; | ||
$middlewareConnections = []; | ||
foreach ($container->findTaggedServiceIds('doctrine.middleware') as $id => $tags) { | ||
$middlewareAbstractDefs[$id] = $container->getDefinition($id); | ||
// When a def has doctrine.middleware tags with connection attributes equal to connection names | ||
// registration of this middleware is limited to the connections with these names | ||
foreach ($tags as $tag) { | ||
if (! isset($tag['connection'])) { | ||
continue; | ||
} | ||
|
||
$middlewareConnections[$id][] = $tag['connection']; | ||
} | ||
} | ||
|
||
foreach (array_keys($container->getParameter('doctrine.connections')) as $name) { | ||
$middlewareDefs = []; | ||
foreach ($middlewareAbstractDefs as $id => $abstractDef) { | ||
if (isset($middlewareConnections[$id]) && array_search($name, $middlewareConnections[$id]) === false) { | ||
continue; | ||
} | ||
|
||
$middlewareDefs[] = $childDef = $container->setDefinition( | ||
sprintf('%s.%s', $id, $name), | ||
new ChildDefinition($id) | ||
); | ||
|
||
if (! is_subclass_of($abstractDef->getClass(), ConnectionNameAwareInterface::class)) { | ||
continue; | ||
} | ||
|
||
$childDef->addMethodCall('setConnectionName', [$name]); | ||
} | ||
|
||
$container | ||
->getDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name)) | ||
->addMethodCall('setMiddlewares', [$middlewareDefs]); | ||
} | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Middleware; | ||
|
||
interface ConnectionNameAwareInterface | ||
{ | ||
public function setConnectionName(string $name): void; | ||
} |
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,14 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="doctrine.dbal.logging_middleware" class="Doctrine\DBAL\Logging\Middleware" abstract="true"> | ||
<argument type="service" id="logger" on-invalid="null" /> | ||
<tag name="monolog.logger" channel="doctrine" /> | ||
<tag name="doctrine.middleware" /> | ||
</service> | ||
</services> | ||
</container> |
Oops, something went wrong.