vendor/vich/uploader-bundle/src/Handler/UploadHandler.php line 53

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\Handler;
  3. use Symfony\Component\HttpFoundation\File\UploadedFile;
  4. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  5. use Vich\UploaderBundle\Event\Event;
  6. use Vich\UploaderBundle\Event\Events;
  7. use Vich\UploaderBundle\FileAbstraction\ReplacingFile;
  8. use Vich\UploaderBundle\Injector\FileInjectorInterface;
  9. use Vich\UploaderBundle\Mapping\PropertyMapping;
  10. use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
  11. use Vich\UploaderBundle\Storage\StorageInterface;
  12. /**
  13.  * Upload handler.
  14.  *
  15.  * @author Kévin Gomez <contact@kevingomez.fr>
  16.  * @final
  17.  */
  18. class UploadHandler extends AbstractHandler
  19. {
  20.     /**
  21.      * @var FileInjectorInterface
  22.      */
  23.     protected $injector;
  24.     /**
  25.      * @var EventDispatcherInterface
  26.      */
  27.     protected $dispatcher;
  28.     public function __construct(
  29.         PropertyMappingFactory $factory,
  30.         StorageInterface $storage,
  31.         FileInjectorInterface $injector,
  32.         EventDispatcherInterface $dispatcher
  33.     ) {
  34.         parent::__construct($factory$storage);
  35.         $this->injector $injector;
  36.         $this->dispatcher $dispatcher;
  37.     }
  38.     /**
  39.      * Checks for file to upload.
  40.      *
  41.      * @param object $obj       The object
  42.      * @param string $fieldName The name of the field containing the upload (has to be mapped)
  43.      *
  44.      * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException
  45.      */
  46.     public function upload($objstring $fieldName): void
  47.     {
  48.         $mapping $this->getMapping($obj$fieldName);
  49.         // nothing to upload
  50.         if (!$this->hasUploadedFile($obj$mapping)) {
  51.             return;
  52.         }
  53.         $this->dispatch(Events::PRE_UPLOAD, new Event($obj$mapping));
  54.         $this->storage->upload($obj$mapping);
  55.         $this->injector->injectFile($obj$mapping);
  56.         $this->dispatch(Events::POST_UPLOAD, new Event($obj$mapping));
  57.     }
  58.     public function inject($objstring $fieldName): void
  59.     {
  60.         $mapping $this->getMapping($obj$fieldName);
  61.         $this->dispatch(Events::PRE_INJECT, new Event($obj$mapping));
  62.         $this->injector->injectFile($obj$mapping);
  63.         $this->dispatch(Events::POST_INJECT, new Event($obj$mapping));
  64.     }
  65.     public function clean($objstring $fieldName): void
  66.     {
  67.         $mapping $this->getMapping($obj$fieldName);
  68.         // nothing uploaded, do not remove anything
  69.         if (!$this->hasUploadedFile($obj$mapping)) {
  70.             return;
  71.         }
  72.         $this->remove($obj$fieldName);
  73.     }
  74.     public function remove($objstring $fieldName): void
  75.     {
  76.         $mapping $this->getMapping($obj$fieldName);
  77.         $oldFilename $mapping->getFileName($obj);
  78.         // nothing to remove, avoid dispatching useless events
  79.         if (empty($oldFilename)) {
  80.             return;
  81.         }
  82.         $preEvent = new Event($obj$mapping);
  83.         $this->dispatch(Events::PRE_REMOVE$preEvent);
  84.         if ($preEvent->isCanceled()) {
  85.             return;
  86.         }
  87.         $this->storage->remove($obj$mapping);
  88.         $mapping->erase($obj);
  89.         $this->dispatch(Events::POST_REMOVE, new Event($obj$mapping));
  90.     }
  91.     protected function dispatch(string $eventNameEvent $event): void
  92.     {
  93.         $this->dispatcher->dispatch($event$eventName);
  94.     }
  95.     protected function hasUploadedFile(object $objPropertyMapping $mapping): bool
  96.     {
  97.         $file $mapping->getFile($obj);
  98.         return $file instanceof UploadedFile || $file instanceof ReplacingFile;
  99.     }
  100. }