Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ContentDownloadRouteReferenceListener.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
8
9 use eZ\Publish\API\Repository\Values\Content\Field;
10 use eZ\Publish\Core\Helper\TranslationHelper;
11 use eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent;
12 use eZ\Publish\Core\MVC\Symfony\MVCEvents;
13 use InvalidArgumentException;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 use Symfony\Component\OptionsResolver\Options;
16 use Symfony\Component\OptionsResolver\OptionsResolver;
17
18 class ContentDownloadRouteReferenceListener implements EventSubscriberInterface
19 {
20 const ROUTE_NAME = 'ez_content_download';
21 const OPT_FIELD_IDENTIFIER = 'fieldIdentifier';
22 const OPT_CONTENT = 'content';
23 const OPT_CONTENT_ID = 'contentId';
24 const OPT_DOWNLOAD_NAME = 'filename';
25 const OPT_DOWNLOAD_LANGUAGE = 'inLanguage';
26 const OPT_SITEACCESS_LANGUAGE = 'language';
27 const OPT_SITEACCESS = 'siteaccess';
28 const OPT_VERSION = 'version';
29
31 private $translationHelper;
32
33 public function __construct(TranslationHelper $translationHelper)
34 {
35 $this->translationHelper = $translationHelper;
36 }
37
38 public static function getSubscribedEvents()
39 {
40 return [
41 MVCEvents::ROUTE_REFERENCE_GENERATION => 'onRouteReferenceGeneration',
42 ];
43 }
44
48 public function onRouteReferenceGeneration(RouteReferenceGenerationEvent $event)
49 {
50 $routeReference = $event->getRouteReference();
51
52 if ($routeReference->getRoute() != self::ROUTE_NAME) {
53 return;
54 }
55
56 $options = $routeReference->getParams();
57
58 $resolver = new OptionsResolver();
59 $this->configureOptions($resolver);
60 $options = $resolver->resolve($options);
61
62 if (isset($options[self::OPT_DOWNLOAD_LANGUAGE])) {
63 $routeReference->set(self::OPT_DOWNLOAD_LANGUAGE, $options[self::OPT_DOWNLOAD_LANGUAGE]);
64 }
65
66 if (isset($options[self::OPT_VERSION])) {
67 $routeReference->set(self::OPT_VERSION, $options[self::OPT_VERSION]);
68 }
69
70 $routeReference->set(self::OPT_CONTENT_ID, $options[self::OPT_CONTENT_ID]);
71 $routeReference->set(self::OPT_DOWNLOAD_NAME, $options[self::OPT_DOWNLOAD_NAME]);
72 }
73
77 protected function configureOptions(OptionsResolver $resolver)
78 {
79 $resolver->setRequired([self::OPT_CONTENT, self::OPT_FIELD_IDENTIFIER]);
80
81 $resolver->setDefaults(
82 [
83 self::OPT_VERSION => null,
84 self::OPT_DOWNLOAD_LANGUAGE => null,
85 self::OPT_SITEACCESS_LANGUAGE => null,
86 self::OPT_SITEACCESS => null,
87 ]
88 );
89
90 $resolver->setAllowedTypes(self::OPT_CONTENT, 'eZ\Publish\API\Repository\Values\Content\Content');
91 $resolver->setAllowedTypes(self::OPT_FIELD_IDENTIFIER, 'string');
92
93 $resolver->setDefault(
94 self::OPT_CONTENT_ID,
95 function (Options $options) {
96 return $options[self::OPT_CONTENT]->id;
97 }
98 );
99
100 $resolver->setDefault(
101 self::OPT_DOWNLOAD_NAME,
102 function (Options $options) {
103 $field = $this->translationHelper->getTranslatedField(
104 $options[self::OPT_CONTENT],
105 $options[self::OPT_FIELD_IDENTIFIER],
106 $options[self::OPT_DOWNLOAD_LANGUAGE]
107 );
108 if (!$field instanceof Field) {
109 throw new InvalidArgumentException(
110 sprintf(
111 "The '%s' parameter did not match a known Field",
112 self::OPT_FIELD_IDENTIFIER
113 )
114 );
115 }
116
117 return $field->value->fileName;
118 }
119 );
120 }
121 }
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener
Definition: ContentDownloadRouteReferenceListener.php:19
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\ROUTE_NAME
const ROUTE_NAME
Definition: ContentDownloadRouteReferenceListener.php:20
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_DOWNLOAD_NAME
const OPT_DOWNLOAD_NAME
Definition: ContentDownloadRouteReferenceListener.php:24
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_DOWNLOAD_LANGUAGE
const OPT_DOWNLOAD_LANGUAGE
Definition: ContentDownloadRouteReferenceListener.php:25
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\$translationHelper
$translationHelper
Definition: ContentDownloadRouteReferenceListener.php:31
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_CONTENT_ID
const OPT_CONTENT_ID
Definition: ContentDownloadRouteReferenceListener.php:23
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_FIELD_IDENTIFIER
const OPT_FIELD_IDENTIFIER
Definition: ContentDownloadRouteReferenceListener.php:21
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\__construct
__construct(TranslationHelper $translationHelper)
Definition: ContentDownloadRouteReferenceListener.php:33
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_SITEACCESS_LANGUAGE
const OPT_SITEACCESS_LANGUAGE
Definition: ContentDownloadRouteReferenceListener.php:26
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\getSubscribedEvents
static getSubscribedEvents()
Definition: ContentDownloadRouteReferenceListener.php:38
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_CONTENT
const OPT_CONTENT
Definition: ContentDownloadRouteReferenceListener.php:22
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\onRouteReferenceGeneration
onRouteReferenceGeneration(RouteReferenceGenerationEvent $event)
Definition: ContentDownloadRouteReferenceListener.php:48
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_VERSION
const OPT_VERSION
Definition: ContentDownloadRouteReferenceListener.php:28
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\configureOptions
configureOptions(OptionsResolver $resolver)
Definition: ContentDownloadRouteReferenceListener.php:77
eZ\Bundle\EzPublishCoreBundle\EventListener\ContentDownloadRouteReferenceListener\OPT_SITEACCESS
const OPT_SITEACCESS
Definition: ContentDownloadRouteReferenceListener.php:27
eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: API/Repository/Exceptions/InvalidArgumentException.php:13
eZ\Publish\API\Repository\Values\Content\Field
Definition: API/Repository/Values/Content/Field.php:21
eZ\Publish\Core\Helper\TranslationHelper
Helper class for translation.
Definition: TranslationHelper.php:25
eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent
Event dispatched when generating a RouteReference.
Definition: RouteReferenceGenerationEvent.php:17
eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent\getRouteReference
getRouteReference()
Definition: RouteReferenceGenerationEvent.php:41
eZ\Publish\Core\MVC\Symfony\MVCEvents
Definition: MVCEvents.php:10
eZ\Publish\Core\MVC\Symfony\MVCEvents\ROUTE_REFERENCE_GENERATION
const ROUTE_REFERENCE_GENERATION
ROUTE_REFERENCE_GENERATION event occurs when a RouteReference is generated, and gives an opportunity ...
Definition: MVCEvents.php:77
eZ\Bundle\EzPublishCoreBundle\EventListener
Definition: BackgroundIndexingTerminateListener.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.