-
-
Save onurguven/2a069186c8a878750d19eb1e5e52e757 to your computer and use it in GitHub Desktop.
If you need to get the SonataMedia public urls exposed through the api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Sonata\MediaBundle\Listener; | |
use Application\Sonata\MediaBundle\Entity\Media; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
use Sonata\MediaBundle\Provider\MediaProviderInterface; | |
use Sonata\MediaBundle\Provider\Pool; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class InjectMediaUrlsListener | |
{ | |
/** | |
* @var ContainerInterface | |
*/ | |
private $container; | |
/** | |
* @param ContainerInterface $container | |
*/ | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
/** | |
* @param LifecycleEventArgs $args | |
*/ | |
public function postLoad(LifecycleEventArgs $args) | |
{ | |
$entity = $args->getEntity(); | |
if ($entity instanceof Media) { | |
$this->injectMediaUrls($entity); | |
} | |
} | |
/** | |
* Generate and inject public URLs for the given Media entity. | |
* | |
* @param Media $media | |
*/ | |
private function injectMediaUrls(Media $media) | |
{ | |
$pool = $this->getPool(); | |
$context = $media->getContext(); | |
$formats = $pool->getFormatNamesByContext($context); | |
if (null === $formats) { | |
return; | |
} | |
$provider = $this->getProvider($media->getProviderName()); | |
$publicUrls = array(); | |
foreach ($formats as $format => $options) { | |
$format = $provider->getFormatName($media, $format); | |
$options['url'] = $provider->generatePublicUrl($media, $format); | |
$publicUrls[str_replace($context.'_', '', $format)] = $options; | |
} | |
if ('sonata.media.provider.file' === $media->getProviderName()) { | |
$publicUrls['reference'] = $provider->generatePublicUrl($media, 'reference'); | |
} | |
$media->setPublicUrls($publicUrls); | |
} | |
/** | |
* Lazy-load the media pool from the container. | |
* | |
* @return Pool | |
*/ | |
private function getPool() | |
{ | |
return $this->container->get('sonata.media.pool'); | |
} | |
/** | |
* Get the provider service by name. | |
* | |
* @param string $name | |
* @return MediaProviderInterface | |
*/ | |
private function getProvider($name) | |
{ | |
return $this->container->get($name); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Sonata\MediaBundle\Entity; | |
use AppBundle\Interfaces\EtagProviderInterface; | |
use AppBundle\Traits\EtagProviderTrait; | |
use AppBundle\Traits\SerializeModelNameTrait; | |
use Hateoas\Configuration\Annotation as Hateoas; | |
use JMS\Serializer\Annotation as Serializer; | |
use Sonata\MediaBundle\Entity\BaseMedia as BaseMedia; | |
/** | |
* This file has been generated by the Sonata EasyExtends bundle ( https://sonata-project.org/bundles/easy-extends ). | |
* | |
* References : | |
* working with object : http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en | |
* | |
* @Serializer\AccessorOrder("alphabetical") | |
* | |
* @Hateoas\Relation( | |
* "self", | |
* href = @Hateoas\Route("api_get_media", parameters = {"id" = "expr(object.getId())"}, absolute = true) | |
* ) | |
*/ | |
class Media extends BaseMedia implements EtagProviderInterface | |
{ | |
/** | |
* The unique identifier of the Media. | |
* | |
* @var int | |
*/ | |
protected $id; | |
/** | |
* @var array | |
*/ | |
private $publicUrls = array(); | |
/** | |
* Get id. | |
* | |
* @return int $id | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @param $publicUrls array | |
* | |
* @return self | |
*/ | |
public function setPublicUrls(array $publicUrls) | |
{ | |
$this->publicUrls = $publicUrls; | |
return $this; | |
} | |
/** | |
* @return array | |
*/ | |
public function getPublicUrls() | |
{ | |
return $this->publicUrls; | |
} | |
/** | |
* @param $format string | |
* | |
* @return null|string | |
*/ | |
public function getPublicUrlByFormat($format) | |
{ | |
if (!isset($this->publicUrls[$format])) { | |
return null; | |
} | |
return $this->publicUrls[$format]['url']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment