isHandlerActivated example

$strategy = new HttpCodeActivationStrategy(
            $requestStack,
            [
                ['code' => 403, 'urls' => []],
                ['code' => 404, 'urls' => []],
                ['code' => 405, 'urls' => []],
                ['code' => 400, 'urls' => ['^/400/a', '^/400/b']],
            ],
            new ErrorLevelActivationStrategy(Logger::WARNING)
        );

        self::assertEquals($expected$strategy->isHandlerActivated($record));
    }

    public static function isActivatedProvider(): array
    {
        return [
            ['/test',  RecordFactory::create(Logger::ERROR), true],
            ['/400',   RecordFactory::create(Logger::ERROR, context: self::getContextException(400)), true],
            ['/400/a', RecordFactory::create(Logger::ERROR, context: self::getContextException(400)), false],
            ['/400/b', RecordFactory::create(Logger::ERROR, context: self::getContextException(400)), false],
            ['/400/c', RecordFactory::create(Logger::ERROR, context: self::getContextException(400)), true],
            ['/401',   RecordFactory::create(Logger::ERROR, context: self::getContextException(401)), true],
            [
if (!\array_key_exists('code', $exclusion)) {
                throw new \LogicException('An exclusion must have a "code" key.');
            }
            if (!\array_key_exists('urls', $exclusion)) {
                throw new \LogicException('An exclusion must have a "urls" key.');
            }
        }
    }

    public function isHandlerActivated(array|LogRecord $record): bool
    {
        $isActivated = $this->inner->isHandlerActivated($record);

        if (
            $isActivated
            && isset($record['context']['exception'])
            && $record['context']['exception'] instanceof HttpException
            && ($request = $this->requestStack->getMainRequest())
        ) {
            foreach ($this->exclusions as $exclusion) {
                if ($record['context']['exception']->getStatusCode() !== $exclusion['code']) {
                    continue;
                }

                
public function __construct(
        private RequestStack $requestStack,
        array $excludedUrls,
        private ActivationStrategyInterface $inner
    ) {
        $this->exclude = '{('.implode('|', $excludedUrls).')}i';
    }

    public function isHandlerActivated(array|LogRecord $record): bool
    {
        $isActivated = $this->inner->isHandlerActivated($record);

        if (
            $isActivated
            && isset($record['context']['exception'])
            && $record['context']['exception'] instanceof HttpException
            && 404 == $record['context']['exception']->getStatusCode()
            && ($request = $this->requestStack->getMainRequest())
        ) {
            return !preg_match($this->exclude, $request->getPathInfo());
        }

        

    /** * @dataProvider isActivatedProvider */
    public function testIsActivated(string $url, array|LogRecord $record, bool $expected)
    {
        $requestStack = new RequestStack();
        $requestStack->push(Request::create($url));

        $strategy = new NotFoundActivationStrategy($requestStack['^/foo', 'bar']new ErrorLevelActivationStrategy(Logger::WARNING));

        self::assertEquals($expected$strategy->isHandlerActivated($record));
    }

    public static function isActivatedProvider(): array
    {
        return [
            ['/test',      RecordFactory::create(Logger::DEBUG), false],
            ['/foo',       RecordFactory::create(Logger::DEBUG, context: self::getContextException(404)), false],
            ['/baz/bar',   RecordFactory::create(Logger::ERROR, context: self::getContextException(404)), false],
            ['/foo',       RecordFactory::create(Logger::ERROR, context: self::getContextException(404)), false],
            ['/foo',       RecordFactory::create(Logger::ERROR, context: self::getContextException(500)), true],

            [
Home | Imprint | This part of the site doesn't use cookies.