Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] [WIP] Validate controller arguments using constraints #47425

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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added ConstraintAttributeListener
  • Loading branch information
artyuum committed Aug 29, 2022
commit 55ecfec3e6a989b7b2a7388f36ac8cac5aa7e891
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Constraints\ExpressionValidator;
use Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\EventListener\ConstraintAttributeListener;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down Expand Up @@ -102,5 +103,11 @@
service('property_info'),
])
->tag('validator.auto_mapper')

->set('controller.is_granted_attribute_listener', ConstraintAttributeListener::class)
->args([
service('validator'),
])
->tag('kernel.event_subscriber')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\Validator\EventListener;

use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* Handles the validator constraint attributes on controller's arguments.
*
* @author Dynèsh Hassanaly <artyum@protonmail.com>
*/
class ConstraintAttributeListener
{
public function __construct(private readonly ValidatorInterface $validator) {
}

private function getReflectionMethod(callable $controller): \ReflectionMethod
{
if (is_array($controller)) {
$class = $controller[0];
$method = $controller[1];
} else {
/** @var object $controller */
$class = $controller;
$method = '__invoke';
}

return new \ReflectionMethod($class, $method);
}

public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
$controller = $event->getController();
$arguments = $event->getArguments();
$reflectionMethod = $this->getReflectionMethod($controller);

foreach ($reflectionMethod->getParameters() as $index => $reflectionParameter) {
$reflectionAttributes = $reflectionParameter->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF);

if (!$reflectionAttributes) {
continue;
}

foreach ($reflectionAttributes as $reflectionAttribute) {
/** @var Constraint $constraint */
$constraint = $reflectionAttribute->newInstance();
$value = $arguments[$index];

$violations = $this->validator->validate($value, $constraint);

if ($violations->count() > 0) {
throw new ValidationFailedException($value, $violations);
}
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.