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

Fix deprecation notice in EventDispatcher in Symfony 4.3+ #362

Merged
merged 1 commit into from Sep 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions Controller/AbstractChunkedController.php
Expand Up @@ -90,11 +90,8 @@ protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $re
*/
protected function dispatchChunkEvents($uploaded, ResponseInterface $response, Request $request, $isLast)
{
$dispatcher = $this->container->get('event_dispatcher');

// dispatch post upload event (both the specific and the general)
$postUploadEvent = new PostChunkUploadEvent($uploaded, $response, $request, $isLast, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::POST_CHUNK_UPLOAD, $postUploadEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_CHUNK_UPLOAD, $this->type), $postUploadEvent);
$this->dispatchEvent($postUploadEvent, UploadEvents::POST_CHUNK_UPLOAD);
}
}
37 changes: 24 additions & 13 deletions Controller/AbstractController.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class AbstractController
{
Expand Down Expand Up @@ -136,12 +137,9 @@ protected function handleUpload($file, ResponseInterface $response, Request $req
*/
protected function dispatchPreUploadEvent(FileInterface $uploaded, ResponseInterface $response, Request $request)
{
$dispatcher = $this->container->get('event_dispatcher');

// dispatch pre upload event (both the specific and the general)
$preUploadEvent = new PreUploadEvent($uploaded, $response, $request, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::PRE_UPLOAD, $preUploadEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::PRE_UPLOAD, $this->type), $preUploadEvent);
$this->dispatchEvent($preUploadEvent, UploadEvents::PRE_UPLOAD);
}

/**
Expand All @@ -154,28 +152,22 @@ protected function dispatchPreUploadEvent(FileInterface $uploaded, ResponseInter
*/
protected function dispatchPostEvents($uploaded, ResponseInterface $response, Request $request)
{
$dispatcher = $this->container->get('event_dispatcher');

// dispatch post upload event (both the specific and the general)
$postUploadEvent = new PostUploadEvent($uploaded, $response, $request, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::POST_UPLOAD, $postUploadEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_UPLOAD, $this->type), $postUploadEvent);
$this->dispatchEvent($postUploadEvent, UploadEvents::POST_UPLOAD);

if (!$this->config['use_orphanage']) {
// dispatch post persist event (both the specific and the general)
$postPersistEvent = new PostPersistEvent($uploaded, $response, $request, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::POST_PERSIST, $postPersistEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_PERSIST, $this->type), $postPersistEvent);
$this->dispatchEvent($postPersistEvent, UploadEvents::POST_PERSIST);
}
}

protected function validate(FileInterface $file, Request $request, ResponseInterface $response = null)
{
$dispatcher = $this->container->get('event_dispatcher');
$event = new ValidationEvent($file, $request, $this->config, $this->type, $response);

$dispatcher->dispatch(UploadEvents::VALIDATION, $event);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::VALIDATION, $this->type), $event);
$this->dispatchEvent($event, UploadEvents::VALIDATION);
}

/**
Expand Down Expand Up @@ -215,4 +207,23 @@ protected function getRequest()

return $this->container->get('request_stack')->getMasterRequest();
}

/**
* Event dispatch proxy that avoids using deprecated interfaces.
*
* @param $event
* @param string $eventName
*/
protected function dispatchEvent($event, string $eventName)
{
$dispatcher = $this->container->get('event_dispatcher');

if ($dispatcher instanceof EventDispatcherInterface) {
$dispatcher->dispatch($event, $eventName);
$dispatcher->dispatch($event, sprintf('%s.%s', $eventName, $this->type));
} else {
$dispatcher->dispatch($eventName, $event);
$dispatcher->dispatch(sprintf('%s.%s', $eventName, $this->type), $event);
}
}
}