Skip to content

Commit

Permalink
Fix deprecation notice in EventDispatcher in Symfony 4.3+ (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
TiGR authored and bytehead committed Sep 3, 2019
1 parent 1fb5be7 commit ddc0c39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
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);
}
}
}

0 comments on commit ddc0c39

Please sign in to comment.