Skip to content

[RateLimiter] [WIP] add attribute for controllers methods #59920

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

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CHANGELOG

7.3
---

* Add `rate_limiter` controller attribute
* Add `errors.php` and `webhook.php` routing configuration files (use them instead of their XML equivalent)

Before:
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/RateLimiter/Attribute/RateLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\Attribute;

/**
* Rate limit the controller.
*
* @see https://symfony.com/doc/current/rate_limiter.html
*
* @author Raziel Rodrigues <raziel.rodrigues@outlook.pt>
*/
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
final class RateLimit
{
/**
* @param string $limiter The configured limiter name
* @param string[] $methods Request methods to apply the rate limit (`[]` for all)
*/
public function __construct(
public string $limiter,
public array $methods = []
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\RateLimiter\Attribute\RateLimit;

/**
* Rate limit attribute listener for controller
*
* @see https://symfony.com/doc/current/rate_limiter.html
*
* @author Raziel Rodrigues <raziel.rodrigues@outlook.pt>
*/
class RateLimitAttributeListener implements EventSubscriberInterface
{
public function __construct(
private ?ExpressionLanguage $expressionLanguage = null,
) {}

public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
/** @var RateLimit[] $attributes */
if (!\is_array($attributes = $event->getAttributes()[RateLimit::class] ?? null)) {
return;
}
}

public static function getSubscribedEvents(): array
{
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 20]];
}
}