johannschopplich / kirby-highlighter

🌐 Server-side syntax highlighting for the code block & KirbyText
https://kirbyseo.com
MIT License
20 stars 4 forks source link

Add compatibility with Kirby Twig plugin #13

Closed FabienYt closed 2 years ago

FabienYt commented 2 years ago

I would like to use your plugin with kirby-twig, unfortunately I have a fatal error when running.

Environment:

Error:


Whoops \ Exception \ ErrorException (E_ERROR)
During inheritance of DOMDocument: Uncaught Whoops\Exception\ErrorException: Return type of KirbyExtended\HTML5DOMDocument::saveHTML(?DOMNode $node = null, bool $entities = false) should either be compatible with DOMDocument::saveHTML(?DOMNode $node = null): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\kirby\src\plugins\kirby-highlighter\classes\KirbyExtended\HTML5DOMDocument.php:73 Stack trace: #0 C:\kirby\vendor\composer\ClassLoader.php(571): Whoops\Run->handleError(8192, 'Return type of ...', 'C:\\kirby...', 73) #1 C:\kirby\vendor\composer\ClassLoader.php(571): include() #2 C:\kirby\vendor\composer\ClassLoader.php(428): Composer\Autoload\includeFile('C:\\kirby...') #3 C:\kirby\src\plugins\kirby-highlighter\extensions\hooks.php(9): Composer\Autoload\ClassLoader->loadClass('KirbyExtended\\H...') #4 [internal function]: Kirby\Cms\App->{closure}('<p>\xC2\xA9 Site...') #5 C:\kirby\vendor\getkirby\cms\src\Toolkit\Controller.php(50): Closure->call(Object(Kirby\Cms\App), '<p>\xC2\xA9 Site...') #6 C:\kirby\vendor\getkirby\cms\src\Cms\Event.php(169): Kirby\Toolkit\Controller->call(Object(Kirby\Cms\App), Array) #7 C:\kirby\vendor\getkirby\cms\src\Cms\App.php(213): Kirby\Cms\Event->call(Object(Kirby\Cms\App), Object(Kirby\Toolkit\Controller)) #8 C:\kirby\vendor\getkirby\cms\src\Cms\App.php(827): Kirby\Cms\App->apply('kirbytext:after', Array, 'text') #9 C:\kirby\vendor\getkirby\cms\config\methods.php(402): Kirby\Cms\App->kirbytext('<p>\xC2\xA9 Site...', Array) #10 C:\kirby\vendor\getkirby\cms\src\Cms\Field.php(78): Kirby\Cms\Core->{closure}(Object(Kirby\Cms\Field)) #11 C:\kirby\vendor\twig\twig\src\Extension\CoreExtension.php(1607): Kirby\Cms\Field->__call('kirbytext', Array) #12 C:\kirby\vendor\twig\twig\src\Environment.php(358) : eval()'d code(156): twig_get_attribute(Object(Twig\Environment), Object(Twig\Source), Object(Kirby\Cms\Field), 'kirbytext', Array, 'any', false, false, false, 59) #13 C:\kirby\vendor\twig\twig\src\Template.php(394): __TwigTemplate_97dbdf1022badedbfc864a46325a7f34->doDisplay(Array, Array) #14 C:\kirby\vendor\twig\twig\src\Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array) #15 C:\kirby\vendor\twig\twig\src\Environment.php(358) : eval()'d code(46): Twig\Template->display(Array, Array) #16 C:\kirby\vendor\twig\twig\src\Template.php(394): __TwigTemplate_69c8c177297fddb406c3d415c7cdc5bd->doDisplay(Array, Array) #17 C:\kirby\vendor\twig\twig\src\Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array) #18 C:\kirby\vendor\twig\twig\src\Template.php(379): Twig\Template->display(Array) #19 C:\kirby\vendor\twig\twig\src\TemplateWrapper.php(40): Twig\Template->render(Array, Array) #20 C:\kirby\vendor\twig\twig\src\Environment.php(277): Twig\TemplateWrapper->render(Array) #21 C:\kirby\src\plugins\kirby-twig\src\classes\Environment.php(261): Twig\Environment->render('home.twig', Array) #22 C:\kirby\src\plugins\kirby-twig\src\classes\Template.php(114): amteich\Twig\Environment->renderPath('home.twig', Array, true) #23 C:\kirby\vendor\getkirby\cms\src\Cms\Page.php(1061): amteich\Twig\Template->render(Array) #24 C:\kirby\vendor\getkirby\cms\src\Cms\App.php(725): Kirby\Cms\Page->render() #25 C:\kirby\vendor\getkirby\cms\src\Cms\App.php(1145): Kirby\Cms\App->io(Object(Kirby\Cms\Page)) #26 C:\kirby\public\index.php(23): Kirby\Cms\App->render() #27 C:\kirby\vendor\getkirby\cms\router.php(14): require('C:\\kirby...') #28 {main}

Stack frames (3)
2
Whoops\Exception\ErrorException
…\src\plugins\kirby-highlighter\classes\KirbyExtended\HTML5DOMDocument.php5
1
Whoops\Run handleError
…\vendor\filp\whoops\src\Whoops\Run.php486
0
Whoops\Run handleShutdown
[internal]0
C:\kirby\src\plugins\kirby-highlighter\classes\KirbyExtended\HTML5DOMDocument.php```
johannschopplich commented 2 years ago

No idea what's going on here. Haven't tested with the Twig plugin. PR welcome!

FabienYt commented 2 years ago

I just found the problem, it comes from PHP 8.1 and not from Kirby Twig as I thought at first. I push the PR quickly. (Done: #15 )

https://wiki.php.net/rfc/internal_method_return_types

Unfortunately, union return types impose a compatibility challenge for libraries: as this construct is only supported since PHP 8.0, libraries would have to accept the fact by default that their code triggers E_DEPRECATED notices on PHP 8.1 if they also want to support PHP versions below 8.0. As a remedy, this RFC proposes to add a ReturnTypeWillChange attribute which could be used to suppress the related E_DEPRECATED notices. Thanks to the backward compatible syntax of attributes, this can be done in code which is compatible with PHP 7 and below.

johannschopplich commented 2 years ago

Nice one. Thanks! Fixed in v2.1.0.