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

Skip to content

[HttpKernel] Create #[UploadedFile] Attribute to map UploadedFile objects to Controller Arguments #49976

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 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\UidValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\UploadedFileValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
use Symfony\Component\HttpKernel\Controller\ErrorController;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
Expand Down Expand Up @@ -64,6 +65,9 @@
->set('argument_resolver.request_attribute', RequestAttributeValueResolver::class)
->tag('controller.argument_value_resolver', ['priority' => 100, 'name' => RequestAttributeValueResolver::class])

->set('argument_resolver.uploaded_file', UploadedFileValueResolver::class)
->tag('controller.targeted_value_resolver', ['name' => UploadedFileValueResolver::class])

->set('argument_resolver.request', RequestValueResolver::class)
->tag('controller.argument_value_resolver', ['priority' => 50, 'name' => RequestValueResolver::class])

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

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

namespace Symfony\Component\HttpKernel\Attribute;

use Symfony\Component\HttpKernel\Controller\ArgumentResolver\UploadedFileValueResolver;

/**
* Controller parameter tag to map uploaded files.
*
* @author Konstantin Myakshin <[email protected]>
*/
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
final class MapUploadedFile extends ValueResolver
{
public function __construct(
public readonly ?string $name = null,
string $resolver = UploadedFileValueResolver::class,
) {
parent::__construct($resolver);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Add `#[WithLogLevel]` for defining log levels for exceptions
* Add `skip_response_headers` to the `HttpCache` options
* Introduce targeted value resolvers with `#[ValueResolver]` and `#[AsTargetedValueResolver]`
* Add `#[MapUploadedFiles]` to map uploaded files to controller arguments

6.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

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

namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @author Konstantin Myakshin <[email protected]>
*/
final class UploadedFileValueResolver implements ValueResolverInterface
{
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
if (!$attribute = $argument->getAttributesOfType(MapUploadedFile::class)[0] ?? null) {
return [];
}

$name = $attribute->name ?? $argument->getName();

if (!$request->files->has($name)) {
if ($argument->isNullable() || $argument->hasDefaultValue()) {
return [];
}

if ('array' === $argument->getType()) {
return [[]];
}

throw new NotFoundHttpException(sprintf('Missing uploaded file "%s".', $name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a 422 Unprocessable Entity.

}

$value = $request->files->all()[$name];
if ('array' === $argument->getType()) {
$value = (array) $value;
}

return $argument->isVariadic() ? $value : [$value];
}
}