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

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.  * @final
  17.  */
  18. class VichImageType extends VichFileType
  19. {
  20.     public const STORAGE_RESOLVE_URI 0;
  21.     public const STORAGE_RESOLVE_PATH_ABSOLUTE 1;
  22.     public const STORAGE_RESOLVE_PATH_RELATIVE 2;
  23.     /**
  24.      * @var CacheManager|null
  25.      */
  26.     private $cacheManager;
  27.     public function __construct(
  28.         StorageInterface $storage,
  29.         UploadHandler $handler,
  30.         PropertyMappingFactory $factory,
  31.         PropertyAccessorInterface $propertyAccessor null,
  32.         CacheManager $cacheManager null
  33.     ) {
  34.         parent::__construct($storage$handler$factory$propertyAccessor);
  35.         $this->cacheManager $cacheManager;
  36.     }
  37.     public function configureOptions(OptionsResolver $resolver): void
  38.     {
  39.         parent::configureOptions($resolver);
  40.         $resolver->setDefaults([
  41.             'image_uri' => true,
  42.             'imagine_pattern' => null,
  43.             'storage_resolve_method' => static::STORAGE_RESOLVE_URI,
  44.         ]);
  45.         $resolver->setAllowedValues(
  46.             'storage_resolve_method',
  47.             [
  48.                 static::STORAGE_RESOLVE_URI,
  49.                 static::STORAGE_RESOLVE_PATH_RELATIVE,
  50.                 static::STORAGE_RESOLVE_PATH_ABSOLUTE,
  51.             ]
  52.         );
  53.         $resolver->setAllowedTypes('image_uri', ['bool''string''callable']);
  54.         $imageUriNormalizer = static function (Options $options$imageUri) {
  55.             return $imageUri ?? $options['download_uri'];
  56.         };
  57.         $resolver->setNormalizer('image_uri'$imageUriNormalizer);
  58.     }
  59.     public function buildView(FormView $viewFormInterface $form, array $options): void
  60.     {
  61.         $object $form->getParent()->getData();
  62.         $view->vars['object'] = $object;
  63.         $view->vars['image_uri'] = null;
  64.         $view->vars['download_uri'] = null;
  65.         if (null !== $object) {
  66.             if ($options['imagine_pattern']) {
  67.                 if (null === $this->cacheManager) {
  68.                     throw new \RuntimeException('LiipImagineBundle must be installed and configured for using "imagine_pattern" option.');
  69.                 }
  70.                 $path $this->resolvePath($options['storage_resolve_method'], $object$form);
  71.                 if (null !== $path) {
  72.                     $view->vars['image_uri'] = $this->cacheManager->getBrowserPath($path$options['imagine_pattern']);
  73.                 }
  74.             } else {
  75.                 $view->vars['image_uri'] = $this->resolveUriOption($options['image_uri'], $object$form);
  76.             }
  77.             $view->vars = \array_replace(
  78.                 $view->vars,
  79.                 $this->resolveDownloadLabel($options['download_label'], $object$form)
  80.             );
  81.             $view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object$form);
  82.         }
  83.         // required for BC
  84.         // TODO: remove for 2.0
  85.         $view->vars['show_download_link'] = !empty($view->vars['download_uri']);
  86.         $view->vars['asset_helper'] = $options['asset_helper'];
  87.     }
  88.     public function getBlockPrefix(): string
  89.     {
  90.         return 'vich_image';
  91.     }
  92.     private function resolvePath(int $storageResolveMethodobject $objectFormInterface $form): ?string
  93.     {
  94.         if (static::STORAGE_RESOLVE_URI === $storageResolveMethod) {
  95.             return $this->storage->resolveUri($object$this->getFieldName($form));
  96.         }
  97.         if (static::STORAGE_RESOLVE_PATH_ABSOLUTE === $storageResolveMethod) {
  98.             return $this->storage->resolvePath($object$this->getFieldName($form));
  99.         }
  100.         if (static::STORAGE_RESOLVE_PATH_RELATIVE === $storageResolveMethod) {
  101.             return $this->storage->resolvePath($object$this->getFieldName($form), nulltrue);
  102.         }
  103.         return null;
  104.     }
  105. }