Skip to content

Commit

Permalink
Simplify PendingFileValueResolver by introducing a common trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Lustmored committed Mar 3, 2023
1 parent 5e21736 commit bb37879
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 83 deletions.
86 changes: 4 additions & 82 deletions src/Filesystem/Symfony/HttpKernel/PendingFileValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Zenstruck\Filesystem\Attribute\UploadedFile;
use Zenstruck\Filesystem\Node\File;
use Zenstruck\Filesystem\Node\File\Image;
use Zenstruck\Filesystem\Node\File\Image\PendingImage;
use Zenstruck\Filesystem\Node\File\PendingFile;

/**
Expand All @@ -30,103 +25,30 @@
if (\interface_exists(ValueResolverInterface::class)) {
class PendingFileValueResolver implements ValueResolverInterface
{
public function __construct(
/** @var ServiceProviderInterface<RequestFilesExtractor> $locator */
private ServiceProviderInterface $locator
) {
use PendingFileValueResolverTrait {
resolve as resolveArgument;
}

/**
* @return iterable<PendingFile|array|null>
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
$attributes = $argument->getAttributes(UploadedFile::class);

if (!RequestFilesExtractor::supports($argument)) {
return [];
}

/** @var UploadedFile|null $attribute */
$attribute = $attributes[0] ?? null;

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

return [
$this->extractor()->extractFilesFromRequest(
$request,
$path,
!\is_a(
$argument->getType() ?? File::class,
File::class,
true
),
$attribute?->image
|| is_a(
$argument->getType() ?? File::class,
Image::class,
true
),
),
];
}

private function extractor(): RequestFilesExtractor
{
return $this->locator->get(RequestFilesExtractor::class);
return $this->resolveArgument($request, $argument);
}
}
} else {
class PendingFileValueResolver implements ArgumentValueResolverInterface
{
public function __construct(
/** @var ServiceProviderInterface<RequestFilesExtractor> $locator */
private ServiceProviderInterface $locator
) {
}
use PendingFileValueResolverTrait;

public function supports(Request $request, ArgumentMetadata $argument): bool
{
return RequestFilesExtractor::supports($argument);
}

/**
* @return iterable<PendingFile|array|null>
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
$attributes = $argument->getAttributes(UploadedFile::class);
\assert(!empty($attributes));

/** @var UploadedFile|null $attribute */
$attribute = $attributes[0] ?? null;

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

return [
$this->extractor()->extractFilesFromRequest(
$request,
$path,
!\is_a(
$argument->getType() ?? File::class,
File::class,
true
),
$attribute?->image
|| is_a(
$argument->getType() ?? File::class,
Image::class,
true
),
)
];
}

private function extractor(): RequestFilesExtractor
{
return $this->locator->get(RequestFilesExtractor::class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

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

namespace Zenstruck\Filesystem\Symfony\HttpKernel;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Zenstruck\Filesystem\Attribute\UploadedFile;
use Zenstruck\Filesystem\Node\File;
use Zenstruck\Filesystem\Node\File\Image;
use Zenstruck\Filesystem\Node\File\PendingFile;

/**
* @author Jakub Caban <[email protected]>
*
* @internal
*/
trait PendingFileValueResolverTrait
{
public function __construct(
/** @var ServiceProviderInterface<RequestFilesExtractor> $locator */
private ServiceProviderInterface $locator
) {
}

/**
* @return iterable<PendingFile|array|null>
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
$attributes = $argument->getAttributes(UploadedFile::class);

/** @var UploadedFile|null $attribute */
$attribute = $attributes[0] ?? null;

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

return [
$this->extractor()->extractFilesFromRequest(
$request,
$path,
!\is_a(
$argument->getType() ?? File::class,
File::class,
true
),
$attribute?->image
|| \is_a(
$argument->getType() ?? File::class,
Image::class,
true
),
),
];
}

private function extractor(): RequestFilesExtractor
{
return $this->locator->get(RequestFilesExtractor::class);
}
}
1 change: 0 additions & 1 deletion tests/Fixtures/Controller/ArgumentResolverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Routing\Annotation\Route;
use Zenstruck\Filesystem\Attribute\UploadedFile;
use Zenstruck\Filesystem\Node\File;
use Zenstruck\Filesystem\Node\File\PendingFile;

/**
* @author Jakub Caban <[email protected]>
Expand Down

0 comments on commit bb37879

Please sign in to comment.