1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Routing; |
16
|
|
|
|
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
20
|
|
|
use SWP\Bundle\ContentBundle\Model\FileInterface; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageInterface; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Model\PreviewUrlAwareInterface; |
25
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; |
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
27
|
|
|
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; |
28
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
29
|
|
|
use Symfony\Component\Routing\RequestContext; |
30
|
|
|
|
31
|
|
|
class MediaRouter extends Router implements VersatileGeneratorInterface |
32
|
|
|
{ |
33
|
|
|
private $mediaManager; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ContainerInterface $container, |
37
|
|
|
$resource, |
38
|
|
|
array $options = [], |
39
|
|
|
RequestContext $context = null, |
40
|
|
|
ContainerInterface $parameters = null, |
41
|
|
|
LoggerInterface $logger = null, |
42
|
|
|
string $defaultLocale = null |
43
|
|
|
) { |
44
|
|
|
$this->mediaManager = $container->get('swp_content_bundle.manager.media'); |
45
|
|
|
|
46
|
84 |
|
parent::__construct($container, $resource, $options, $context, $parameters, $logger, $defaultLocale); |
47
|
|
|
} |
48
|
84 |
|
|
49
|
|
|
public function getRouteDebugMessage($meta, array $parameters = array()): string |
50
|
|
|
{ |
51
|
|
|
return 'Route for media '.$meta->getValues()->getId().' not found'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function supports($meta): bool |
55
|
|
|
{ |
56
|
|
|
return $meta instanceof Meta && ( |
57
|
|
|
$meta->getValues() instanceof ArticleMediaInterface || |
58
|
|
|
$meta->getValues() instanceof ImageRenditionInterface |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function generate($meta, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
63
|
|
|
{ |
64
|
|
|
if (!$meta instanceof Meta) { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$item = $this->getItem($meta); |
69
|
|
|
if (null === $item) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($meta->getValues() instanceof ImageRenditionInterface && null !== ($previewUrl = $meta->getValues()->getPreviewUrl())) { |
74
|
|
|
return $previewUrl; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($item instanceof PreviewUrlAwareInterface && null !== ($previewUrl = $item->getPreviewUrl())) { |
78
|
|
|
return $previewUrl; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->getUrlWithCorrectExtension($item, $parameters); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getItem(Meta $meta): ?FileInterface |
85
|
|
|
{ |
86
|
|
|
if (($rendition = $meta->getValues()) instanceof ImageRendition) { |
87
|
|
|
return $rendition->getImage(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (($image = $meta->getValues()->getImage()) instanceof ImageInterface) { |
91
|
|
|
return $image; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (($file = $meta->getValues()->getFile()) instanceof FileInterface) { |
95
|
|
|
return $file; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getUrlWithCorrectExtension(FileInterface $item, array $parameters): string |
100
|
|
|
{ |
101
|
|
|
$url = $this->mediaManager->getMediaPublicUrl($item); |
102
|
|
|
|
103
|
|
|
if ( |
104
|
|
|
$item instanceof ImageInterface && |
105
|
|
|
array_key_exists('webp', $parameters) && |
106
|
|
|
true === $parameters['webp'] && |
107
|
|
|
$item->hasVariant(ImageInterface::VARIANT_WEBP) |
108
|
|
|
) { |
109
|
|
|
return str_replace('.'.$item->getFileExtension(), '.webp', $url); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $url; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|