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

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