From f6e667700e956795459d1e3fb791ebee24bd9379 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 9 Feb 2022 11:51:52 +0100 Subject: [PATCH] move twig function in runtime --- .../Templating/WopiEditTwigExtension.php | 28 +++---------------- .../WopiEditTwigExtensionRuntime.php | 25 +++++++++++++++++ .../config/services/templating.yaml | 13 +++------ 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtension.php b/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtension.php index 5fb0398bd..600f3cbdd 100644 --- a/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtension.php +++ b/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtension.php @@ -11,8 +11,6 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\Templating; -use ChampsLibres\WopiLib\Contract\Service\Discovery\DiscoveryInterface; -use Chill\DocStoreBundle\Entity\StoredObject; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -20,13 +18,6 @@ use Twig\TwigFunction; class WopiEditTwigExtension extends AbstractExtension { - private DiscoveryInterface $discovery; - - public function __construct(DiscoveryInterface $discovery) - { - $this->discovery = $discovery; - } - public function getFilters(): array { return [ @@ -40,22 +31,11 @@ class WopiEditTwigExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('chill_document_is_editable', [$this, 'isEditable']), + new TwigFunction('chill_document_is_editable', [WopiEditTwigExtensionRuntime::class, 'isEditable'], [ + 'needs_environment' => true, + 'is_safe' => ['html'], + ]), ]; } - public function isEditable(StoredObject $document): bool - { - $mime = $this->discovery->discoverMimeType($document->getType()); - - // TO CHECk is it better condition ? - if (array_key_exists('default', $mime[0]) - && $mime[0]['default'] === 'true' - ) { - return true; - } - - //dump($document, $mime); - return false; - } } \ No newline at end of file diff --git a/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtensionRuntime.php b/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtensionRuntime.php index 81327f521..552ed483e 100644 --- a/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtensionRuntime.php +++ b/src/Bundle/ChillDocStoreBundle/Templating/WopiEditTwigExtensionRuntime.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\Templating; +use ChampsLibres\WopiLib\Contract\Service\Discovery\DiscoveryInterface; use Chill\DocStoreBundle\Entity\StoredObject; use Twig\Environment; use Twig\Extension\RuntimeExtensionInterface; @@ -20,6 +21,13 @@ class WopiEditTwigExtensionRuntime implements RuntimeExtensionInterface public const TEMPLATE = '@ChillDocStore/Button/wopi_edit_document.html.twig'; + private DiscoveryInterface $discovery; + + public function __construct(DiscoveryInterface $discovery) + { + $this->discovery = $discovery; + } + public function renderEditButton(Environment $environment, StoredObject $document, ?Array $options = null): string { return $environment->render(self::TEMPLATE, [ @@ -27,4 +35,21 @@ class WopiEditTwigExtensionRuntime implements RuntimeExtensionInterface 'options' => $options ]); } + + public function isEditable(Environment $environment, StoredObject $document): bool + { + $mime_type = $this->discovery->discoverMimeType($document->getType()); + + if (empty($mime_type)) { + return false; + } + + if (array_key_exists('default', $mime_type[0]) + && $mime_type[0]['default'] === 'true' + ) { + return true; + } + + return false; + } } \ No newline at end of file diff --git a/src/Bundle/ChillDocStoreBundle/config/services/templating.yaml b/src/Bundle/ChillDocStoreBundle/config/services/templating.yaml index 84291d179..426ccd5c1 100644 --- a/src/Bundle/ChillDocStoreBundle/config/services/templating.yaml +++ b/src/Bundle/ChillDocStoreBundle/config/services/templating.yaml @@ -1,11 +1,6 @@ services: - Chill\DocStoreBundle\Templating\WopiEditTwigExtension: - arguments: - $discovery: '@ChampsLibres\WopiLib\Contract\Service\Discovery\DiscoveryInterface' - tags: - - { name: twig.extension } - - Chill\DocStoreBundle\Templating\WopiEditTwigExtensionRuntime: - tags: - - { name: twig.runtime } + Chill\DocStoreBundle\Templating\: + resource: ../../Templating + autoconfigure: true + autowire: true