vendor/sonata-project/media-bundle/src/Twig/MediaRuntime.php line 104

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\MediaBundle\Twig;
  12. use Sonata\MediaBundle\Model\MediaInterface;
  13. use Sonata\MediaBundle\Model\MediaManagerInterface;
  14. use Sonata\MediaBundle\Provider\Pool;
  15. use Twig\Environment;
  16. use Twig\Extension\RuntimeExtensionInterface;
  17. final class MediaRuntime implements RuntimeExtensionInterface
  18. {
  19.     public function __construct(
  20.         private Pool $pool,
  21.         private MediaManagerInterface $mediaManager,
  22.         private Environment $twig,
  23.     ) {
  24.     }
  25.     /**
  26.      * @param array<string, mixed> $options
  27.      */
  28.     public function media(int|string|MediaInterface $mediastring $format, array $options = []): string
  29.     {
  30.         $media $this->getMedia($media);
  31.         if (null === $media) {
  32.             return '';
  33.         }
  34.         $provider $this->pool->getProvider($media->getProviderName());
  35.         $format $provider->getFormatName($media$format);
  36.         $options $provider->getHelperProperties($media$format$options);
  37.         $template $provider->getTemplate('helper_view');
  38.         if (null === $template) {
  39.             return '';
  40.         }
  41.         return $this->twig->render($template, [
  42.             'media' => $media,
  43.             'format' => $format,
  44.             'options' => $options,
  45.         ]);
  46.     }
  47.     /**
  48.      * Returns the thumbnail for the provided media.
  49.      *
  50.      * @param array<string, mixed> $options
  51.      */
  52.     public function thumbnail(int|string|MediaInterface $mediastring $format, array $options = []): string
  53.     {
  54.         $media $this->getMedia($media);
  55.         if (null === $media) {
  56.             return '';
  57.         }
  58.         $provider $this->pool->getProvider($media->getProviderName());
  59.         $format $provider->getFormatName($media$format);
  60.         $formatDefinition $provider->getFormat($format);
  61.         $template $provider->getTemplate('helper_thumbnail');
  62.         if (null === $template) {
  63.             return '';
  64.         }
  65.         // build option
  66.         $defaultOptions = [
  67.             'title' => $media->getName(),
  68.             'alt' => $media->getName(),
  69.         ];
  70.         if (false !== $formatDefinition && isset($formatDefinition['width'])) {
  71.             $defaultOptions['width'] = $formatDefinition['width'];
  72.         }
  73.         if (false !== $formatDefinition && isset($formatDefinition['height'])) {
  74.             $defaultOptions['height'] = $formatDefinition['height'];
  75.         }
  76.         $options array_merge($defaultOptions$options);
  77.         $options['src'] = $provider->generatePublicUrl($media$format);
  78.         return $this->twig->render($template, [
  79.             'media' => $media,
  80.             'options' => $options,
  81.         ]);
  82.     }
  83.     public function path(int|string|MediaInterface $mediastring $format): string
  84.     {
  85.         $media $this->getMedia($media);
  86.         if (null === $media) {
  87.             return '';
  88.         }
  89.         $provider $this->pool->getProvider($media->getProviderName());
  90.         $format $provider->getFormatName($media$format);
  91.         return $provider->generatePublicUrl($media$format);
  92.     }
  93.     private function getMedia(int|string|MediaInterface $media): ?MediaInterface
  94.     {
  95.         if (!$media instanceof MediaInterface) {
  96.             $media $this->mediaManager->find($media);
  97.         }
  98.         if (!$media instanceof MediaInterface) {
  99.             return null;
  100.         }
  101.         if (MediaInterface::STATUS_OK !== $media->getProviderStatus()) {
  102.             return null;
  103.         }
  104.         return $media;
  105.     }
  106. }