Skip to content

Commit 62f8441

Browse files
committed
Modify the API of the events to handle competing listeners
The generated pathname can now only be set once and all further attempts are rejected. Listeners are expected to test this with `hasFile()` prior to taking any action.
1 parent c189423 commit 62f8441

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

wcfsetup/install/files/lib/event/file/GenerateThumbnail.class.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,29 @@
1616
*/
1717
final class GenerateThumbnail implements IPsr14Event
1818
{
19-
/**
20-
* The absolute path to the generated WebP image.
21-
*/
22-
public ?string $filename = null;
19+
private string $pathname;
2320

2421
public function __construct(
2522
public readonly File $file,
2623
public readonly ThumbnailFormat $thumbnailFormat,
2724
) {}
25+
26+
/**
27+
* Sets the pathname of the generated image unless it has already been set
28+
* in which case the call will throw an exception. You must check the result
29+
* of `hasFile()` first.
30+
*/
31+
public function setGeneratedFile(string $pathname): void
32+
{
33+
if (isset($this->pathname)) {
34+
throw new \BadMethodCallException("Cannot set the generated file, a value has already been set.");
35+
}
36+
37+
$this->pathname = $pathname;
38+
}
39+
40+
public function getPathname(): ?string
41+
{
42+
return $this->pathname ?? null;
43+
}
2844
}

wcfsetup/install/files/lib/event/file/GenerateWebpVariant.class.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace wcf\event\file;
44

5+
use BadMethodCallException;
56
use wcf\data\file\File;
67
use wcf\event\IPsr14Event;
78

89
/**
9-
* Requests the generation of a WebP variant of the provided file.
10+
* Requests the generation of a WebP variant for the provided image.
1011
*
1112
* @author Alexander Ebert
1213
* @copyright 2001-2024 WoltLab GmbH
@@ -15,12 +16,37 @@
1516
*/
1617
final class GenerateWebpVariant implements IPsr14Event
1718
{
18-
/**
19-
* The absolute path to the generated WebP image.
20-
*/
21-
public ?string $filename = null;
19+
private string $pathname;
2220

2321
public function __construct(
2422
public readonly File $file
2523
) {}
24+
25+
/**
26+
* Returns true if a file has already been set and no further files are
27+
* being accepted.
28+
*/
29+
public function hasFile(): bool
30+
{
31+
return isset($this->pathname);
32+
}
33+
34+
/**
35+
* Sets the pathname of the generated image unless it has already been set
36+
* in which case the call will throw an exception. You must check the result
37+
* of `hasFile()` first.
38+
*/
39+
public function setGeneratedFile(string $pathname): void
40+
{
41+
if (isset($this->pathname)) {
42+
throw new \BadMethodCallException("Cannot set the generated file, a value has already been set.");
43+
}
44+
45+
$this->pathname = $pathname;
46+
}
47+
48+
public function getPathname(): ?string
49+
{
50+
return $this->pathname ?? null;
51+
}
2652
}

wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function generateWebpVariant(File $file): void
165165
$event = new GenerateWebpVariant($file);
166166
EventHandler::getInstance()->fire($event);
167167

168-
$filename = $event->filename;
168+
$filename = $event->getPathname();
169169
if ($filename === null) {
170170
$imageAdapter = ImageHandler::getInstance()->getAdapter();
171171
if (!$imageAdapter->checkMemoryLimit($file->width, $file->height, $file->mimeType)) {
@@ -263,7 +263,7 @@ public function generateThumbnails(File $file): void
263263
$event = new GenerateThumbnail($file, $format);
264264
EventHandler::getInstance()->fire($event);
265265

266-
$filename = $event->filename;
266+
$filename = $event->getPathname();
267267
if ($filename === null) {
268268
if ($imageAdapter === null) {
269269
$imageAdapter = ImageHandler::getInstance()->getAdapter();

0 commit comments

Comments
 (0)