Browse Source

Passage de l'extension twig avec un RuntimeExtension

Sangfroid 2 months ago
parent
commit
6f727d4366
2 changed files with 23 additions and 15 deletions
  1. 21 0
      src/Twig/AppRuntimeExtension.php
  2. 2 15
      src/Twig/MarkdownExtension.php

+ 21 - 0
src/Twig/AppRuntimeExtension.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Twig;
+
+use App\Service\MarkdownParser;
+use Twig\Extension\RuntimeExtensionInterface;
+
+class AppRuntimeExtension implements RuntimeExtensionInterface
+{
+    public function __construct(
+        protected readonly MarkdownParser $markdownParser
+    )
+
+    {
+    }
+
+    public function parse(string $content): string
+    {
+        return $this->markdownParser->convertToHtml($content);
+    }
+}

+ 2 - 15
src/Twig/MarkdownExtension.php

@@ -2,28 +2,15 @@
 
 namespace App\Twig;
 
-use App\Service\MarkdownParser;
 use Twig\Extension\AbstractExtension;
 use Twig\TwigFilter;
 
 class MarkdownExtension extends AbstractExtension
 {
-    public function __construct(
-        protected readonly MarkdownParser $markdownParser
-    )
-
-    {
-        
-    }
     public function getFilters()
     {
         return [
-            new TwigFilter('markdown', [$this, 'parse'], ['is_safe' => ['html']])
+            new TwigFilter('markdown', [AppRuntimeExtension::class, 'parse'], ['is_safe' => ['html']])
         ];
-    }
-
-    public function parse(string $content): string
-    {
-        return $this->markdownParser->convertToHtml($content);
-    }
+    }    
 }