Skip to content
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

FileElement: Fix that uploaded files are not preserved across requests #128

Merged
merged 1 commit into from
Aug 28, 2023
Merged
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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,6 @@ parameters:
count: 1
path: src/FormElement/FileElement.php

-
message: "#^Only iterables can be unpacked, array\\<array\\<Psr\\\\Http\\\\Message\\\\UploadedFileInterface\\>\\|Psr\\\\Http\\\\Message\\\\UploadedFileInterface\\>\\|Psr\\\\Http\\\\Message\\\\UploadedFileInterface given in argument \\#1\\.$#"
count: 1
path: src/FormElement/FileElement.php

-
message: "#^Parameter \\#1 \\$content of static method ipl\\\\Html\\\\Text\\:\\:create\\(\\) expects string, string\\|null given\\.$#"
count: 1
Expand Down
51 changes: 26 additions & 25 deletions src/FormElement/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,6 @@ public function hasValue()
return $this->value !== null;
}

public function getValue()
{
if (! $this->hasValue()) {
return null;
}

if (! $this->hasFiles()) {
$files = $this->value;
if (! $this->isMultiple()) {
$files = [$files];
}

$storedFiles = $this->storeFiles(...$files);
if (! $this->isMultiple()) {
$storedFiles = $storedFiles[0];
}

$this->value = $storedFiles;
}

return $this->value;
}

public function setValue($value)
{
if (! empty($value)) {
Expand All @@ -154,7 +131,21 @@ public function setValue($value)
}

if ($fileToTest->getError() === UPLOAD_ERR_NO_FILE && ! $fileToTest->getClientFilename()) {
// This is checked here as it's only about file elements for which no value has been chosen
$value = null;
} else {
$files = $value;
if (! $this->isMultiple()) {
$files = [$files];
}

/** @var UploadedFileInterface[] $files */
$storedFiles = $this->storeFiles(...$files);
if (! $this->isMultiple()) {
$storedFiles = $storedFiles[0] ?? null;
}

$value = $storedFiles;
}
} else {
$value = null;
Expand Down Expand Up @@ -222,23 +213,33 @@ protected function storeFiles(UploadedFileInterface ...$files): array
return $files;
}

$storedFiles = [];
foreach ($files as $file) {
$name = $file->getClientFilename();
$path = $this->getFilePath($name);

if ($file->getError() !== UPLOAD_ERR_OK) {
// The file is still returned as otherwise it won't be validated
$storedFiles[] = $file;
continue;
}

$file->moveTo($path);

// Re-created to ensure moveTo() still works if called externally
$this->files[$name] = new UploadedFile(
$file = new UploadedFile(
$path,
$file->getSize(),
0,
$name,
$file->getClientMediaType()
);

$this->files[$name] = $file;
$storedFiles[] = $file;
}

return array_values($this->files);
return $storedFiles;
}

/**
Expand Down