-
-
Notifications
You must be signed in to change notification settings - Fork 460
Allow userland middlewares #1472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = [], | ||
) { | ||
} | ||
} |
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 in_array; | ||
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]) && ! in_array($name, $middlewareConnections[$id], true)) { | ||
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]); | ||
} | ||
} | ||
} |
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use case for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote this PR with the replacement of
It's not a big deal to duplicate an abstract middleware for each connection with a tag, but duplicating too the debug data holder seemed to me more complicated; it's why I chose the second possibility. This is how I plan to create the debug data holder: class DebugDataHolder
{
private array $data = [];
public function addQuery(string $connexionName, Query $query): void
{
$this->data[$connexionName][] = [
'sql' => $query->getSql(),
'params' => $query->getParams(),
'types' => $query->getTypes(),
'executionMS' => static fn() => $query->getDuration(), // stop() may not be called at this point
];
}
public function getData(string $connexionName): array
{
if (!isset($this->data[$connexionName])) {
return [];
}
foreach ($this->data[$connexionName] as &$data) {
if (is_callable($data['executionMS'])) {
$data['executionMS'] = $data['executionMS']();
}
}
return $this->data[$connexionName];
}
} |
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> |
Uh oh!
There was an error while loading. Please reload this page.