From cd377c62a8c5f16b977291f83e23abac838135d8 Mon Sep 17 00:00:00 2001 From: Jakub Caban Date: Thu, 21 Nov 2019 12:38:04 +0100 Subject: [PATCH] Update twig extension to use supported Twig classes instead of legacy ones (#374) --- src/Twig/Extension/FMElfinderExtension.php | 45 +++++++++++-------- .../Extension/FMElfinderExtensionTest.php | 36 ++++++--------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/Twig/Extension/FMElfinderExtension.php b/src/Twig/Extension/FMElfinderExtension.php index 517766c..3048eec 100644 --- a/src/Twig/Extension/FMElfinderExtension.php +++ b/src/Twig/Extension/FMElfinderExtension.php @@ -2,20 +2,27 @@ namespace FM\ElfinderBundle\Twig\Extension; +use Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + /** * Class FMElfinderExtension. */ -class FMElfinderExtension extends \Twig_Extension +class FMElfinderExtension extends AbstractExtension { /** - * @var \Twig_Environment + * @var Environment */ protected $twig; /** - * @param \Twig_Environment $twig + * @param Environment $twig */ - public function __construct(\Twig_Environment $twig) + public function __construct(Environment $twig) { $this->twig = $twig; } @@ -30,9 +37,9 @@ public function getFunctions() $options = array('is_safe' => array('html')); return array( - new \Twig_SimpleFunction('elfinder_tinymce_init', array($this, 'tinymce'), $options), - new \Twig_SimpleFunction('elfinder_tinymce_init4', array($this, 'tinymce4'), $options), - new \Twig_SimpleFunction('elfinder_summernote_init', array($this, 'summernote'), $options), + new TwigFunction('elfinder_tinymce_init', array($this, 'tinymce'), $options), + new TwigFunction('elfinder_tinymce_init4', array($this, 'tinymce4'), $options), + new TwigFunction('elfinder_summernote_init', array($this, 'summernote'), $options), ); } @@ -42,14 +49,14 @@ public function getFunctions() * * @return mixed * - * @throws \Twig_Error_Loader - * @throws \Twig_Error_Runtime - * @throws \Twig_Error_Syntax + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function tinymce($instance = 'default', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0')) { if (!is_string($instance)) { - throw new \Twig_Error_Runtime('The function can be applied to strings only.'); + throw new RuntimeError('The function can be applied to strings only.'); } return $this->twig->render('@FMElfinder/Elfinder/helper/_tinymce.html.twig', @@ -67,14 +74,14 @@ public function tinymce($instance = 'default', $parameters = array('width' => 90 * * @return mixed * - * @throws \Twig_Error_Loader - * @throws \Twig_Error_Runtime - * @throws \Twig_Error_Syntax + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function tinymce4($instance = 'default', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0')) { if (!is_string($instance)) { - throw new \Twig_Error_Runtime('The function can be applied to strings only.'); + throw new RuntimeError('The function can be applied to strings only.'); } return $this->twig->render('@FMElfinder/Elfinder/helper/_tinymce4.html.twig', @@ -93,14 +100,14 @@ public function tinymce4($instance = 'default', $parameters = array('width' => 9 * * @return mixed * - * @throws \Twig_Error_Loader - * @throws \Twig_Error_Runtime - * @throws \Twig_Error_Syntax + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function summernote($instance = 'default', $selector = '.summenote', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0')) { if (!is_string($instance)) { - throw new \Twig_Error_Runtime('The function can be applied to strings only.'); + throw new RuntimeError('The function can be applied to strings only.'); } return $this->twig->render('@FMElfinder/Elfinder/helper/_summernote.html.twig', diff --git a/tests/Twig/Extension/FMElfinderExtensionTest.php b/tests/Twig/Extension/FMElfinderExtensionTest.php index a96a4d5..2bfb30c 100644 --- a/tests/Twig/Extension/FMElfinderExtensionTest.php +++ b/tests/Twig/Extension/FMElfinderExtensionTest.php @@ -9,6 +9,9 @@ use Symfony\Component\Routing\Loader\YamlFileLoader; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; +use Twig\Template; class FMElfinderExtensionTest extends \PHPUnit\Framework\TestCase { @@ -21,15 +24,15 @@ class FMElfinderExtensionTest extends \PHPUnit\Framework\TestCase /** @var \FM\ElfinderBundle\Twig\Extension\FMElFinderExtension */ protected $extension; - /** @var \Twig_Environment */ + /** @var Environment */ protected $twig; - /** @var \Twig_Template */ + /** @var Template */ protected $template; protected function setUp() { - $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__.'/../../../src/Resources/views/Elfinder/helper'))); + $this->twig = new Environment(new FilesystemLoader(array(__DIR__.'/../../../src/Resources/views/Elfinder/helper'))); $this->extension = new FMElfinderExtension($this->twig); $this->twig->addExtension($this->extension); $loader = new YamlFileLoader(new FileLocator(__DIR__.'/../../../src/Resources/config')); @@ -41,8 +44,7 @@ protected function setUp() public function testRenderTinyMCE3() { - $this->template = $this->twig->loadTemplate('_tinymce.html.twig'); - $testData = $this->renderTemplate(array('instance' => 'minimal')); + $testData = $this->twig->render('_tinymce.html.twig', array('instance' => 'minimal')); $expected = <<<'EOF'