-
-
Notifications
You must be signed in to change notification settings - Fork 3
Value resolver #95
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
Value resolver #95
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
da3addf
Import value resolver from document-library
Lustmored 2d260bf
Refactor FilesExtractor to handle PendingFile subclasses in supports()
Lustmored fb83336
Handle PendingImage in value resolving
Lustmored 1c3464f
Fix Phpstan warnings and run CS fixer
Lustmored 78ffb28
Add missing functional tests
Lustmored 60ae750
Allow File/Image typehint for value resolver
Lustmored 45a5848
Simplify PendingFileValueResolver by introducing a common trait
Lustmored 92fe778
Add simple image injection functional tests
Lustmored e8dee6c
Utilize UploadedFile::forArgument() for a centralized value resolver …
Lustmored a0e63ca
Extend UploadedFile to constraints and errorStatus
Lustmored 45dd655
Add file validation logic to pendingfileValueResolver
Lustmored 402f986
Add simple test case for invalid image
Lustmored 669b297
Add valueResolver exception tests
Lustmored f52409c
CS run
Lustmored 0e8a3cc
Trailing comma is a no-go in PHP8 :)
Lustmored 7a2252a
Make phpstan happy
Lustmored 81e1b15
Trivial suggestions
Lustmored 0636c54
Disable logger on tests
Lustmored 52d320d
Add custom exception for incorrect file
Lustmored 588a906
Revert controller loader to annotation for older Sf versions
Lustmored eefd7a4
Hide PHP 8.1+ test under if
Lustmored c5723bc
Move constraints logic to `forArgument`
Lustmored 68bb715
UploadedFile::$multiple is no more
Lustmored 0afd4c9
Move image to the last position of UploadedFile constructor
Lustmored d9a7902
Introduce UploadedFile as extender of PendingUploadedFile attribute (…
Lustmored 8e793cd
Handle storing files with #[UploadedFile] attribute
Lustmored d00ee79
Fix typehint
Lustmored c27ea4a
Update src/Filesystem/Symfony/HttpKernel/RequestFilesExtractor.php
Lustmored 479cd43
Move IncorrectFileHttpException
Lustmored 70e0203
Remove phpstan ignore
Lustmored e716dba
Add phpstan-ignore-line and add @readonly to attributes
Lustmored 2ea5cb3
CS fixer run
Lustmored 0dcf1a5
Fix tests
Lustmored 1874fb5
code style again
Lustmored c4e9731
Add pointless docblock to silence phpstan
Lustmored 9229221
Apply suggestions from code review
Lustmored 37892c5
Mark PendingUploadedFile constructor as consistent for phpstan
Lustmored File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?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\Attribute; | ||
|
||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Validator\Constraints\All; | ||
use Zenstruck\Filesystem\Node\File; | ||
use Zenstruck\Filesystem\Node\File\Image; | ||
use Zenstruck\Filesystem\Symfony\Validator\PendingImageConstraint; | ||
|
||
/** | ||
* @author Jakub Caban <[email protected]> | ||
* | ||
* @phpstan-consistent-constructor | ||
* @readonly | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] | ||
class PendingUploadedFile | ||
{ | ||
public function __construct( | ||
public ?string $path = null, | ||
public ?array $constraints = null, | ||
public ?bool $image = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function forArgument(ArgumentMetadata $argument): self | ||
Lustmored marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$attributes = $argument->getAttributes(self::class, ArgumentMetadata::IS_INSTANCEOF); | ||
|
||
if (!empty($attributes)) { | ||
$attribute = $attributes[0]; | ||
\assert($attribute instanceof self); | ||
} else { | ||
$attribute = new static(); | ||
} | ||
|
||
$attribute->path ??= $argument->getName(); | ||
|
||
$attribute->image ??= \is_a( | ||
$argument->getType() ?? File::class, | ||
Image::class, | ||
true | ||
); | ||
|
||
if (null === $attribute->constraints && $attribute->image) { | ||
if ('array' === $argument->getType()) { | ||
$attribute->constraints = [ | ||
new All([new PendingImageConstraint()]), | ||
]; | ||
} else { | ||
$attribute->constraints = [new PendingImageConstraint()]; | ||
} | ||
} | ||
|
||
return $attribute; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Attribute; | ||
|
||
use Zenstruck\Filesystem\Node\Path\Expression; | ||
use Zenstruck\Filesystem\Node\Path\Namer; | ||
|
||
/** | ||
* @author Jakub Caban <[email protected]> | ||
Lustmored marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @readonly | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] | ||
final class UploadedFile extends PendingUploadedFile | ||
{ | ||
public string|Namer $namer; | ||
|
||
public function __construct( | ||
public string $filesystem, | ||
string|Namer|null $namer = null, | ||
?string $path = null, | ||
?array $constraints = null, | ||
?bool $image = null, | ||
) { | ||
parent::__construct($path, $constraints, $image); | ||
|
||
$this->namer = $namer ?? new Expression('{checksum}/{name}{ext}'); | ||
kbond marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Filesystem/Symfony/Exception/IncorrectFileHttpException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\Exception; | ||
|
||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; | ||
|
||
/** | ||
* @author Jakub Caban <[email protected]> | ||
*/ | ||
class IncorrectFileHttpException extends UnprocessableEntityHttpException | ||
{ | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Filesystem/Symfony/HttpKernel/PendingFileValueResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?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\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Zenstruck\Filesystem\Node\File\PendingFile; | ||
|
||
/** | ||
* @author Jakub Caban <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
if (\interface_exists(ValueResolverInterface::class)) { | ||
class PendingFileValueResolver implements ValueResolverInterface | ||
{ | ||
use PendingFileValueResolverTrait { | ||
resolve as resolveArgument; | ||
} | ||
|
||
/** | ||
* @return iterable<PendingFile|array|null> | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
if (!RequestFilesExtractor::supports($argument)) { | ||
return []; | ||
} | ||
|
||
return $this->resolveArgument($request, $argument); | ||
} | ||
} | ||
} else { | ||
class PendingFileValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
use PendingFileValueResolverTrait; | ||
|
||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
return RequestFilesExtractor::supports($argument); | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/Filesystem/Symfony/HttpKernel/PendingFileValueResolverTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?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\Component\Validator\ConstraintViolationList; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
use Zenstruck\Filesystem; | ||
use Zenstruck\Filesystem\Attribute\PendingUploadedFile; | ||
use Zenstruck\Filesystem\Attribute\UploadedFile; | ||
use Zenstruck\Filesystem\FilesystemRegistry; | ||
use Zenstruck\Filesystem\Node; | ||
use Zenstruck\Filesystem\Node\File; | ||
use Zenstruck\Filesystem\Node\File\PendingFile; | ||
use Zenstruck\Filesystem\Node\PathGenerator; | ||
use Zenstruck\Filesystem\Symfony\Exception\IncorrectFileHttpException; | ||
|
||
/** | ||
* @author Jakub Caban <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
trait PendingFileValueResolverTrait | ||
{ | ||
/** | ||
* @param ServiceProviderInterface<mixed> $locator | ||
*/ | ||
public function __construct(private ServiceProviderInterface $locator) | ||
{ | ||
} | ||
|
||
/** | ||
* @return iterable<File|array|null> | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
$attribute = PendingUploadedFile::forArgument($argument); | ||
|
||
$files = $this->extractor()->extractFilesFromRequest( | ||
$request, | ||
(string) $attribute->path, | ||
'array' === $argument->getType(), | ||
(bool) $attribute->image, | ||
); | ||
|
||
if (!$files) { | ||
return [$files]; | ||
} | ||
|
||
if ($attribute->constraints) { | ||
$errors = $this->validator()->validate( | ||
$files, | ||
$attribute->constraints | ||
); | ||
|
||
if (\count($errors)) { | ||
\assert($errors instanceof ConstraintViolationList); | ||
|
||
throw new IncorrectFileHttpException((string) $errors); | ||
} | ||
} | ||
|
||
if ($attribute instanceof UploadedFile) { | ||
if (\is_array($files)) { | ||
$files = \array_map( | ||
fn(PendingFile $file) => $this->saveFile($attribute, $file), | ||
$files | ||
); | ||
} else { | ||
$files = $this->saveFile($attribute, $files); | ||
} | ||
} | ||
|
||
return [$files]; | ||
} | ||
|
||
private function saveFile(UploadedFile $uploadedFile, PendingFile $file): File | ||
{ | ||
$path = $this->generatePath($uploadedFile, $file); | ||
$file = $this->filesystem($uploadedFile->filesystem) | ||
->write($path, $file) | ||
; | ||
|
||
if ($uploadedFile->image) { | ||
return $file->ensureImage(); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
private function extractor(): RequestFilesExtractor | ||
{ | ||
return $this->locator->get(RequestFilesExtractor::class); | ||
} | ||
|
||
private function filesystem(string $filesystem): Filesystem | ||
{ | ||
return $this->locator->get(FilesystemRegistry::class)->get($filesystem); | ||
} | ||
|
||
private function generatePath(UploadedFile $uploadedFile, Node $node): string | ||
{ | ||
return $this->locator->get(PathGenerator::class)->generate( | ||
$uploadedFile->namer, | ||
$node | ||
); | ||
} | ||
|
||
private function validator(): ValidatorInterface | ||
{ | ||
return $this->locator->get(ValidatorInterface::class); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.