Skip to content

Commit 8f52e36

Browse files
StevenRenauxjmsche
authored andcommitted
Add event
1 parent 7acaf0e commit 8f52e36

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"php": "^8.2",
2929
"doctrine/orm": "^2.10 || ^3.0",
3030
"symfony/deprecation-contracts": "^3.5",
31+
"symfony/event-dispatcher": "^6.4 || ^7.0",
3132
"symfony/form": "^6.4 || ^7.0",
3233
"symfony/framework-bundle": "^6.4 || ^7.0",
3334
"symfony/http-foundation": "^6.4 || ^7.0",

config/services.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
3838
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
3939

40+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
41+
4042
return static function (ContainerConfigurator $container): void {
4143
$container->services()
4244
// Controllers
@@ -78,6 +80,7 @@
7880
->set(FileStorageManager::class)
7981
->arg('$filesystemStorage', service(FilesystemStorage::class))
8082
->arg('$flysystemStorage', service(FlysystemStorage::class))
83+
->arg('$eventDispatcher', service(EventDispatcherInterface::class))
8184

8285
// Form types
8386
->set(FileType::class)

src/Event/UploadedFileEvent.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Leapt\CoreBundle\Event;
6+
7+
use Leapt\CoreBundle\FileStorage\FileUploadConfig;
8+
use Symfony\Contracts\EventDispatcher\Event;
9+
10+
final class UploadedFileEvent extends Event
11+
{
12+
public function __construct(
13+
public \SplFileInfo $splFileInfo,
14+
public FileUploadConfig $fileUploadConfig,
15+
) {
16+
}
17+
}

src/FileStorage/FileStorageManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace Leapt\CoreBundle\FileStorage;
66

7+
use Leapt\CoreBundle\Event\UploadedFileEvent;
78
use Symfony\Component\HttpFoundation\File\File;
9+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
810

911
final class FileStorageManager
1012
{
1113
public function __construct(
1214
private FilesystemStorage $filesystemStorage,
1315
private FlysystemStorage $flysystemStorage,
16+
private EventDispatcherInterface $eventDispatcher,
1417
) {
1518
}
1619

@@ -21,6 +24,8 @@ public function uploadFile(FileUploadConfig $fileUploadConfig, File $uploadedFil
2124
} else {
2225
$this->filesystemStorage->uploadFile($fileUploadConfig, $uploadedFile, $path, $filename);
2326
}
27+
28+
$this->eventDispatcher->dispatch(new UploadedFileEvent(new \SplFileInfo($path . \DIRECTORY_SEPARATOR . $filename), $fileUploadConfig));
2429
}
2530

2631
public function removeFile(FileUploadConfig $fileUploadConfig, string $file): void

tests/Listener/FileSubscriberTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPUnit\Framework\TestCase;
2626
use Symfony\Component\Filesystem\Filesystem;
2727
use Symfony\Component\HttpFoundation\File\File;
28+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2829

2930
class FileSubscriberTest extends TestCase
3031
{
@@ -46,6 +47,7 @@ protected function setUp(): void
4647
$fileStorageManager = new FileStorageManager(
4748
new FilesystemStorage($this->rootDir),
4849
new FlysystemStorage([]),
50+
$this->createMock(EventDispatcherInterface::class),
4951
);
5052
$this->subscriber = new FileSubscriber($fileStorageManager);
5153

0 commit comments

Comments
 (0)