|
13 | 13 |
|
14 | 14 | use Symfony\Component\HttpFoundation\Request;
|
15 | 15 | use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
16 |
| -use Symfony\Component\HttpKernel\Exception\HttpException; |
17 | 16 | use Symfony\Component\Validator\ConstraintViolationList;
|
18 | 17 | use Symfony\Component\Validator\Validator\ValidatorInterface;
|
19 | 18 | use Symfony\Contracts\Service\ServiceProviderInterface;
|
|
23 | 22 | use Zenstruck\Filesystem\FilesystemRegistry;
|
24 | 23 | use Zenstruck\Filesystem\Node;
|
25 | 24 | use Zenstruck\Filesystem\Node\File;
|
26 |
| -use Zenstruck\Filesystem\Node\File\LazyFile; |
27 | 25 | use Zenstruck\Filesystem\Node\File\PendingFile;
|
28 |
| -use Zenstruck\Filesystem\Node\Mapping; |
29 | 26 | use Zenstruck\Filesystem\Node\PathGenerator;
|
30 | 27 | use Zenstruck\Filesystem\Symfony\Exception\IncorrectFileHttpException;
|
31 | 28 |
|
|
36 | 33 | */
|
37 | 34 | trait PendingFileValueResolverTrait
|
38 | 35 | {
|
39 |
| - /** @phpstan-ignore-line */public function __construct(private ServiceProviderInterface $locator) { |
| 36 | + /** @phpstan-ignore-line */ |
| 37 | + public function __construct(private ServiceProviderInterface $locator) |
| 38 | + { |
40 | 39 | }
|
41 | 40 |
|
42 |
| - /** |
| 41 | + /** |
43 | 42 | * @return iterable<File|array|null>
|
44 | 43 | */
|
45 |
| - public function resolve(Request $request, ArgumentMetadata $argument): iterable |
46 |
| - { |
47 |
| - $attribute = PendingUploadedFile::forArgument($argument); |
48 |
| - |
49 |
| - $files = $this->extractor()->extractFilesFromRequest( |
50 |
| - $request, |
51 |
| - (string) $attribute->path, |
52 |
| - 'array' === $argument->getType(), |
53 |
| - (bool) $attribute->image, |
54 |
| - ); |
| 44 | + public function resolve(Request $request, ArgumentMetadata $argument): iterable |
| 45 | + { |
| 46 | + $attribute = PendingUploadedFile::forArgument($argument); |
| 47 | + |
| 48 | + $files = $this->extractor()->extractFilesFromRequest( |
| 49 | + $request, |
| 50 | + (string) $attribute->path, |
| 51 | + 'array' === $argument->getType(), |
| 52 | + (bool) $attribute->image, |
| 53 | + ); |
55 | 54 |
|
56 |
| - if (!$files) { |
57 |
| - return [$files]; |
58 |
| - } |
| 55 | + if (!$files) { |
| 56 | + return [$files]; |
| 57 | + } |
59 | 58 |
|
60 |
| - if ($attribute->constraints) { |
61 |
| - $errors = $this->validator()->validate( |
62 |
| - $files, |
63 |
| - $attribute->constraints |
64 |
| - ); |
| 59 | + if ($attribute->constraints) { |
| 60 | + $errors = $this->validator()->validate( |
| 61 | + $files, |
| 62 | + $attribute->constraints |
| 63 | + ); |
65 | 64 |
|
66 |
| - if (\count($errors)) { |
67 |
| - \assert($errors instanceof ConstraintViolationList); |
| 65 | + if (\count($errors)) { |
| 66 | + \assert($errors instanceof ConstraintViolationList); |
68 | 67 |
|
69 |
| - throw new IncorrectFileHttpException($attribute->errorStatus, (string) $errors); |
| 68 | + throw new IncorrectFileHttpException($attribute->errorStatus, (string) $errors); |
| 69 | + } |
70 | 70 | }
|
71 |
| - } |
72 | 71 |
|
73 |
| - if ($attribute instanceof UploadedFile) { |
74 |
| - if (is_array($files)) { |
75 |
| - $files = array_map( |
76 |
| - fn (PendingFile $file) => $this->saveFile($attribute, $file), |
77 |
| - $files |
78 |
| - ); |
79 |
| - } else { |
80 |
| - $files = $this->saveFile($attribute, $files); |
| 72 | + if ($attribute instanceof UploadedFile) { |
| 73 | + if (\is_array($files)) { |
| 74 | + $files = \array_map( |
| 75 | + fn(PendingFile $file) => $this->saveFile($attribute, $file), |
| 76 | + $files |
| 77 | + ); |
| 78 | + } else { |
| 79 | + $files = $this->saveFile($attribute, $files); |
| 80 | + } |
81 | 81 | }
|
| 82 | + |
| 83 | + return [$files]; |
82 | 84 | }
|
83 | 85 |
|
84 |
| - return [$files]; |
85 |
| - } |
| 86 | + private function saveFile(UploadedFile $uploadedFile, PendingFile $file): File |
| 87 | + { |
| 88 | + $path = $this->generatePath($uploadedFile, $file); |
| 89 | + $file = $this->filesystem($uploadedFile->filesystem) |
| 90 | + ->write($path, $file) |
| 91 | + ; |
86 | 92 |
|
87 |
| - private function saveFile(UploadedFile $uploadedFile, PendingFile $file): File |
88 |
| - { |
89 |
| - $path = $this->generatePath($uploadedFile, $file); |
90 |
| - $file = $this->filesystem($uploadedFile->filesystem) |
91 |
| - ->write($path, $file); |
| 93 | + if ($uploadedFile->image) { |
| 94 | + return $file->ensureImage(); |
| 95 | + } |
92 | 96 |
|
93 |
| - if ($uploadedFile->image) { |
94 |
| - return $file->ensureImage(); |
| 97 | + return $file; |
95 | 98 | }
|
96 | 99 |
|
97 |
| - return $file; |
98 |
| - } |
99 |
| - |
100 |
| - private function extractor(): RequestFilesExtractor |
101 |
| - { |
102 |
| - return $this->locator->get(RequestFilesExtractor::class); |
103 |
| - } |
| 100 | + private function extractor(): RequestFilesExtractor |
| 101 | + { |
| 102 | + return $this->locator->get(RequestFilesExtractor::class); |
| 103 | + } |
104 | 104 |
|
105 |
| - private function filesystem(string $filesystem): Filesystem |
106 |
| - { |
107 |
| - return $this->locator->get(FilesystemRegistry::class)->get($filesystem); |
108 |
| - } |
| 105 | + private function filesystem(string $filesystem): Filesystem |
| 106 | + { |
| 107 | + return $this->locator->get(FilesystemRegistry::class)->get($filesystem); |
| 108 | + } |
109 | 109 |
|
110 |
| - private function generatePath(UploadedFile $uploadedFile, Node $node): string |
111 |
| - { |
112 |
| - return $this->locator->get(PathGenerator::class)->generate( |
113 |
| - $uploadedFile->namer, |
114 |
| - $node |
115 |
| - ); |
116 |
| - } |
| 110 | + private function generatePath(UploadedFile $uploadedFile, Node $node): string |
| 111 | + { |
| 112 | + return $this->locator->get(PathGenerator::class)->generate( |
| 113 | + $uploadedFile->namer, |
| 114 | + $node |
| 115 | + ); |
| 116 | + } |
117 | 117 |
|
118 |
| - private function validator(): ValidatorInterface |
119 |
| - { |
120 |
| - return $this->locator->get(ValidatorInterface::class); |
121 |
| - } |
| 118 | + private function validator(): ValidatorInterface |
| 119 | + { |
| 120 | + return $this->locator->get(ValidatorInterface::class); |
| 121 | + } |
122 | 122 | }
|
0 commit comments