vendor/vich/uploader-bundle/src/Form/Type/VichImageType.php line 20

Open in your IDE?
  1. <?php
  2. namespace Vich\UploaderBundle\Form\Type;
  3. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  4. use Symfony\Component\Form\FormInterface;
  5. use Symfony\Component\Form\FormView;
  6. use Symfony\Component\OptionsResolver\Options;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  9. use Vich\UploaderBundle\Handler\UploadHandler;
  10. use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
  11. use Vich\UploaderBundle\Storage\StorageInterface;
  12. /**
  13.  * @author Kévin Gomez <contact@kevingomez.fr>
  14.  * @author Konstantin Myakshin <koc-dp@yandex.ru>
  15.  * @author Massimiliano Arione <max.arione@gmail.com>
  16.  */
  17. class VichImageType extends VichFileType
  18. {
  19.     public const STORAGE_RESOLVE_URI 0;
  20.     public const STORAGE_RESOLVE_PATH_ABSOLUTE 1;
  21.     public const STORAGE_RESOLVE_PATH_RELATIVE 2;
  22.     /**
  23.      * @var CacheManager|null
  24.      */
  25.     private $cacheManager;
  26.     public function __construct(
  27.         StorageInterface $storage,
  28.         UploadHandler $handler,
  29.         PropertyMappingFactory $factory,
  30.         PropertyAccessorInterface $propertyAccessor null,
  31.         CacheManager $cacheManager null
  32.     ) {
  33.         parent::__construct($storage$handler$factory$propertyAccessor);
  34.         $this->cacheManager $cacheManager;
  35.     }
  36.     public function configureOptions(OptionsResolver $resolver): void
  37.     {
  38.         parent::configureOptions($resolver);
  39.         $resolver->setDefaults([
  40.             'image_uri' => true,
  41.             'imagine_pattern' => null,
  42.             'storage_resolve_method' => static::STORAGE_RESOLVE_URI,
  43.         ]);
  44.         $resolver->setAllowedValues(
  45.             'storage_resolve_method',
  46.             [
  47.                 static::STORAGE_RESOLVE_URI,
  48.                 static::STORAGE_RESOLVE_PATH_RELATIVE,
  49.                 static::STORAGE_RESOLVE_PATH_ABSOLUTE,
  50.             ]
  51.         );
  52.         $resolver->setAllowedTypes('image_uri', ['bool''string''callable']);
  53.         $imageUriNormalizer = static function (Options $options$imageUri) {
  54.             return $imageUri ?? $options['download_uri'];
  55.         };
  56.         $resolver->setNormalizer('image_uri'$imageUriNormalizer);
  57.     }
  58.     public function buildView(FormView $viewFormInterface $form, array $options): void
  59.     {
  60.         $object $form->getParent()->getData();
  61.         $view->vars['object'] = $object;
  62.         $view->vars['image_uri'] = null;
  63.         $view->vars['download_uri'] = null;
  64.         if (null !== $object) {
  65.             if ($options['imagine_pattern']) {
  66.                 if (null === $this->cacheManager) {
  67.                     throw new \RuntimeException('LiipImagineBundle must be installed and configured for using "imagine_pattern" option.');
  68.                 }
  69.                 $path $this->resolvePath($options['storage_resolve_method'], $object$form);
  70.                 if (null !== $path) {
  71.                     $view->vars['image_uri'] = $this->cacheManager->getBrowserPath($path$options['imagine_pattern']);
  72.                 }
  73.             } else {
  74.                 $view->vars['image_uri'] = $this->resolveUriOption($options['image_uri'], $object$form);
  75.             }
  76.             $view->vars = \array_replace(
  77.                 $view->vars,
  78.                 $this->resolveDownloadLabel($options['download_label'], $object$form)
  79.             );
  80.             $view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object$form);
  81.         }
  82.         // required for BC
  83.         // TODO: remove for 2.0
  84.         $view->vars['show_download_link'] = !empty($view->vars['download_uri']);
  85.         $view->vars['asset_helper'] = $options['asset_helper'];
  86.     }
  87.     public function getBlockPrefix(): string
  88.     {
  89.         return 'vich_image';
  90.     }
  91.     private function resolvePath(int $storageResolveMethodobject $objectFormInterface $form): ?string
  92.     {
  93.         if (static::STORAGE_RESOLVE_URI === $storageResolveMethod) {
  94.             return $this->storage->resolveUri($object$form->getName());
  95.         }
  96.         if (static::STORAGE_RESOLVE_PATH_ABSOLUTE === $storageResolveMethod) {
  97.             return $this->storage->resolvePath($object$form->getName());
  98.         }
  99.         if (static::STORAGE_RESOLVE_PATH_RELATIVE === $storageResolveMethod) {
  100.             return $this->storage->resolvePath($object$form->getName(), nulltrue);
  101.         }
  102.         return null;
  103.     }
  104. }