Skip to content

Commit

Permalink
Fix container injection during UploadController initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitro-N committed Oct 19, 2021
1 parent c1548ce commit 4e05ab6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
45 changes: 34 additions & 11 deletions Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
use Glavweb\UploaderBundle\Response\Response as UploaderResponse;
use Glavweb\UploaderBundle\Response\ResponseInterface;
use Glavweb\UploaderBundle\UploadEvents;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Component\HttpFoundation\File\File;
Expand All @@ -41,8 +43,10 @@
* @package Glavweb\UploaderBundle
* @author Andrey Nilov <[email protected]>
*/
class UploadController extends AbstractController
class UploadController implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -382,12 +386,13 @@ protected function dispatchPreUploadEvent(UploaderManager $uploaderManager,
$context)
{
$configContext = $uploaderManager->getContextConfig($context);
$dispatcher = $this->get('event_dispatcher');
/** @var EventDispatcher $dispatcher */
$dispatcher = $this->get('event_dispatcher');

// dispatch pre upload event (both the specific and the general)
$event = new PreUploadEvent($uploadedFile, $response, $request, $context, $configContext);
$dispatcher->dispatch(UploadEvents::PRE_UPLOAD, $event);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::PRE_UPLOAD, $context), $event);
$dispatcher->dispatch($event, UploadEvents::PRE_UPLOAD);
$dispatcher->dispatch($event, sprintf('%s.%s', UploadEvents::PRE_UPLOAD, $context));
}

/**
Expand All @@ -409,12 +414,13 @@ protected function dispatchPostEvents(UploaderManager $uploaderManager,
$context)
{
$configContext = $uploaderManager->getContextConfig($context);
$dispatcher = $this->get('event_dispatcher');
/** @var EventDispatcher $dispatcher */
$dispatcher = $this->get('event_dispatcher');

// dispatch post upload event (both the specific and the general)
$event = new PostUploadEvent($uploadedFile, $media, $response, $request, $context, $configContext);
$dispatcher->dispatch(UploadEvents::POST_UPLOAD, $event);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_UPLOAD, $context), $event);
$dispatcher->dispatch($event, UploadEvents::POST_UPLOAD);
$dispatcher->dispatch($event, sprintf('%s.%s', UploadEvents::POST_UPLOAD, $context));
}

/**
Expand All @@ -426,12 +432,13 @@ protected function dispatchPostEvents(UploaderManager $uploaderManager,
protected function validate(UploaderManager $uploaderManager, FileInterface $file, Request $request, $context)
{
$configContext = $uploaderManager->getContextConfig($context);
$dispatcher = $this->get('event_dispatcher');
/** @var EventDispatcher $dispatcher */
$dispatcher = $this->get('event_dispatcher');

// dispatch validation event (both the specific and the general)
$event = new ValidationEvent($file, $request, $configContext, $context);
$dispatcher->dispatch(UploadEvents::VALIDATION, $event);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::VALIDATION, $context), $event);
$dispatcher->dispatch($event, UploadEvents::VALIDATION);
$dispatcher->dispatch($event, sprintf('%s.%s', UploadEvents::VALIDATION, $context));
}

/**
Expand Down Expand Up @@ -492,4 +499,20 @@ public function setErrorHandler($errorHandler)
{
$this->errorHandler = $errorHandler;
}

/**
* @param string $service
* @return object|null
*/
private function get(string $service) {
return $this->container->get($service);
}

/**
* @param string $parameter
* @return array|bool|float|int|string|null
*/
private function getParameter(string $parameter) {
return $this->container->getParameter($parameter);
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/GlavwebUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$this->addAnnotatedClassesToCompile([
'Glavweb\\UploaderBundle\\Controller\\'
]);

$container->setParameter('glavweb_uploader.config', $config);
}

Expand Down

0 comments on commit 4e05ab6

Please sign in to comment.