phoenix icon indicating copy to clipboard operation
phoenix copied to clipboard

AsMiddleware will ignore the abstract definition's tags

Open ywisax opened this issue 6 months ago • 0 comments

Sample code:

<?php

namespace App\Doctrine\Middleware;

use App\Doctrine\Service\QueryExecutionTimeLogger;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsMiddleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\Service\ResetInterface;

#[AsMiddleware]
class LogMiddleware implements Middleware, ResetInterface
{
    private Stopwatch $stopwatch;

    public function __construct(
        private readonly QueryExecutionTimeLogger $timeLogger,
    ) {
        $this->stopwatch = new Stopwatch(true);
    }

    public function wrap(Driver $driver): Driver
    {
        return new LogDriver($driver, $this->timeLogger, $this->stopwatch);
    }

    public function reset(): void
    {
        var_dump('LogMiddleware reset');
        debug_print_backtrace();
        $this->stopwatch->reset();
    }
}

We have three connection profile, so the \Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MiddlewaresPass will create three new service, like:

image

And the abstract middleware service's reset method will not trigger, because there are no kernel.reset tag like this:

image

ywisax avatar Aug 26 '24 11:08 ywisax