Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Allow #[WithHttpStatus] and #[WithLogLevel] to take effect on interfaces #53191

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

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,9 @@ public function logKernelException(ExceptionEvent $event): void
}

// There's no specific status code defined in the configuration for this exception
if (!$throwable instanceof HttpExceptionInterface) {
$class = new \ReflectionClass($throwable);

do {
if ($attributes = $class->getAttributes(WithHttpStatus::class, \ReflectionAttribute::IS_INSTANCEOF)) {
/** @var WithHttpStatus $instance */
$instance = $attributes[0]->newInstance();

$throwable = HttpException::fromStatusCode($instance->statusCode, $throwable->getMessage(), $throwable, $instance->headers);
$event->setThrowable($throwable);
break;
}
} while ($class = $class->getParentClass());
if (!$throwable instanceof HttpExceptionInterface && $withHttpStatus = $this->getInheritedAttribute($throwable::class, WithHttpStatus::class)) {
$throwable = HttpException::fromStatusCode($withHttpStatus->statusCode, $throwable->getMessage(), $throwable, $withHttpStatus->headers);
$event->setThrowable($throwable);
}

$e = FlattenException::createFromThrowable($throwable);
Expand Down Expand Up @@ -200,16 +190,9 @@ private function resolveLogLevel(\Throwable $throwable): string
}
}

$class = new \ReflectionClass($throwable);

do {
if ($attributes = $class->getAttributes(WithLogLevel::class)) {
/** @var WithLogLevel $instance */
$instance = $attributes[0]->newInstance();

return $instance->level;
}
} while ($class = $class->getParentClass());
if ($withLogLevel = $this->getInheritedAttribute($throwable::class, WithLogLevel::class)) {
return $withLogLevel->level;
}

if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() >= 500) {
return LogLevel::CRITICAL;
Expand All @@ -233,4 +216,45 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re

return $request;
}

/**
* @template T
*
* @param class-string<T> $attribute
*
* @return T|null
*/
private function getInheritedAttribute(string $class, string $attribute): ?object
{
$class = new \ReflectionClass($class);
$interfaces = [];
$attributeReflector = null;
$parentInterfaces = [];
$ownInterfaces = [];

do {
if ($attributes = $class->getAttributes($attribute, \ReflectionAttribute::IS_INSTANCEOF)) {
$attributeReflector = $attributes[0];
$parentInterfaces = class_implements($class->name);
break;
}

$interfaces[] = class_implements($class->name);
} while ($class = $class->getParentClass());

while ($interfaces) {
$ownInterfaces = array_diff_key(array_pop($interfaces), $parentInterfaces);
$parentInterfaces += $ownInterfaces;

foreach ($ownInterfaces as $interface) {
$class = new \ReflectionClass($interface);

if ($attributes = $class->getAttributes($attribute, \ReflectionAttribute::IS_INSTANCEOF)) {
$attributeReflector = $attributes[0];
}
}
}

return $attributeReflector?->newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ public function testHandleWithLogLevelAttribute()
$this->assertCount(1, $logger->getLogs('warning'));
}

public function testHandleClassImplementingInterfaceWithLogLevelAttribute()
{
$request = new Request();
$event = new ExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MAIN_REQUEST, new ImplementingInterfaceWithLogLevelAttribute());
$logger = new TestLogger();
$l = new ErrorListener('not used', $logger);

$l->logKernelException($event);
$l->onKernelException($event);

$this->assertEquals(0, $logger->countErrors());
$this->assertCount(0, $logger->getLogs('critical'));
$this->assertCount(1, $logger->getLogs('warning'));
}

public function testHandleWithLogLevelAttributeAndCustomConfiguration()
{
$request = new Request();
Expand Down Expand Up @@ -298,6 +313,7 @@ public static function exceptionWithAttributeProvider()
yield [new WithCustomUserProvidedAttribute(), 208, ['name' => 'value']];
yield [new WithGeneralAttribute(), 412, ['some' => 'thing']];
yield [new ChildOfWithGeneralAttribute(), 412, ['some' => 'thing']];
yield [new ImplementingInterfaceWithGeneralAttribute(), 412, ['some' => 'thing']];
}
}

Expand Down Expand Up @@ -359,6 +375,20 @@ class WithGeneralAttribute extends \Exception
{
}

#[WithHttpStatus(
statusCode: Response::HTTP_PRECONDITION_FAILED,
headers: [
'some' => 'thing',
]
)]
interface InterfaceWithGeneralAttribute
{
}

class ImplementingInterfaceWithGeneralAttribute extends \Exception implements InterfaceWithGeneralAttribute
{
}

class ChildOfWithGeneralAttribute extends WithGeneralAttribute
{
}
Expand All @@ -371,3 +401,12 @@ class WarningWithLogLevelAttribute extends \Exception
class ChildOfWarningWithLogLevelAttribute extends WarningWithLogLevelAttribute
{
}

#[WithLogLevel(LogLevel::WARNING)]
interface InterfaceWithLogLevelAttribute
{
}

class ImplementingInterfaceWithLogLevelAttribute extends \Exception implements InterfaceWithLogLevelAttribute
{
}