From 74554b87bb1099b7918c653c7ba6b0a6efef2907 Mon Sep 17 00:00:00 2001 From: Tyler Sommer Date: Fri, 24 Nov 2023 21:08:19 -0700 Subject: [PATCH 1/3] Update to support php 8.1 and symfony 6.3 --- composer.json | 16 +++++++------- src/Extension/TwigConfiguration.php | 4 ++-- src/Twig/RouterExtension.php | 21 ++++++++----------- .../RegisterTwigExtensionsPassTest.php | 9 ++++++-- tests/Extension/TwigExtensionTest.php | 3 ++- tests/Twig/AssetExtensionTest.php | 3 ++- tests/Twig/RouterExtensionTest.php | 18 ++-------------- 7 files changed, 32 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index 52ac32b..ed869a2 100644 --- a/composer.json +++ b/composer.json @@ -3,16 +3,16 @@ "description": "Integrates Twig with Nice PHP framework", "license": "MIT", "require": { - "php": ">=5.4.0", - "nice/templating": "1.0.x-dev", - "twig/twig": "~1.15", - "symfony/config": "~2.3", - "symfony/dependency-injection": "~2.3" + "php": ">=8.1.0", + "nice/templating": "dev-php-and-symfony-update", + "twig/twig": "~2.13", + "symfony/config": "~6.3", + "symfony/dependency-injection": "~6.3" }, "require-dev": { - "phpunit/phpunit": "~3.7", - "symfony/http-kernel": "~2.3", - "nice/framework": "1.0.x-dev" + "phpunit/phpunit": "~10.3", + "symfony/http-kernel": "~6.3", + "nice/framework": "dev-php-and-symfony-update" }, "autoload": { "psr-4": { diff --git a/src/Extension/TwigConfiguration.php b/src/Extension/TwigConfiguration.php index 1b8a28b..787e419 100644 --- a/src/Extension/TwigConfiguration.php +++ b/src/Extension/TwigConfiguration.php @@ -21,8 +21,8 @@ class TwigConfiguration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('twig'); + $treeBuilder = new TreeBuilder('twig'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/src/Twig/RouterExtension.php b/src/Twig/RouterExtension.php index 6b18362..0270ac9 100644 --- a/src/Twig/RouterExtension.php +++ b/src/Twig/RouterExtension.php @@ -11,6 +11,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; +use Twig\TwigFunction; class RouterExtension extends \Twig_Extension { @@ -62,13 +63,13 @@ public function getGlobals() public function getFunctions() { return array( - 'current_controller' => new \Twig_Function_Method($this, 'getController'), - 'current_action' => new \Twig_Function_Method($this, 'getAction'), - 'current_route' => new \Twig_Function_Method($this, 'getRoute'), - 'is_current_controller' => new \Twig_Function_Method($this, 'isCurrentController'), - 'is_current_route' => new \Twig_Function_Method($this, 'isCurrentRoute'), - 'path' => new \Twig_Function_Method($this, 'generatePath'), - 'url' => new \Twig_Function_Method($this, 'generateUrl') + 'current_controller' => new TwigFunction('getController', [$this, 'getController']), + 'current_action' => new TwigFunction('getAction', [$this, 'getAction']), + 'current_route' => new TwigFunction('getRoute', [$this, 'getRoute']), + 'is_current_controller' => new TwigFunction('isCurrentController', [$this, 'isCurrentController']), + 'is_current_route' => new TwigFunction('isCurrentRoute', [$this, 'isCurrentRoute']), + 'path' => new TwigFunction('generatePath', [$this, 'generatePath']), + 'url' => new TwigFunction('generateUrl', [$this, 'generateUrl']) ); } @@ -173,11 +174,7 @@ public function generateUrl($name, array $parameters = array()) protected function getCurrentRequest() { if (!$this->request) { - if ($this->container->isScopeActive('request')) { - $this->request = $this->container->get('request'); - } else { - throw new \RuntimeException('Unable to get "request" service'); - } + $this->request = $this->container->get('request'); } return $this->request; diff --git a/tests/DependencyInjection/Compiler/RegisterTwigExtensionsPassTest.php b/tests/DependencyInjection/Compiler/RegisterTwigExtensionsPassTest.php index a7c43f6..7b98823 100644 --- a/tests/DependencyInjection/Compiler/RegisterTwigExtensionsPassTest.php +++ b/tests/DependencyInjection/Compiler/RegisterTwigExtensionsPassTest.php @@ -10,10 +10,11 @@ namespace Nice\Tests\Extension; use Nice\DependencyInjection\Compiler\RegisterTwigExtensionsPass; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; -class RegisterTwigExtensionsPassTest extends \PHPUnit_Framework_TestCase +class RegisterTwigExtensionsPassTest extends TestCase { /** * Test the RegisterTwigExtensionsPass @@ -43,7 +44,11 @@ public function testProcess() */ public function testSilentFailWhenTwigServiceDoesntExist() { - $container = new ContainerBuilder(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') + ->onlyMethods(['getDefinition']) + ->getMock(); + + $container->expects($this->never())->method('getDefinition')->with(['twig']); $container->register('fake.extension', 'FakeExtension') ->addTag('twig.extension'); diff --git a/tests/Extension/TwigExtensionTest.php b/tests/Extension/TwigExtensionTest.php index 87f7d73..2c6a62a 100644 --- a/tests/Extension/TwigExtensionTest.php +++ b/tests/Extension/TwigExtensionTest.php @@ -10,9 +10,10 @@ namespace Nice\Tests\Extension; use Nice\Extension\TwigExtension; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; -class TwigExtensionTest extends \PHPUnit_Framework_TestCase +class TwigExtensionTest extends TestCase { /** * Test the TwigExtension diff --git a/tests/Twig/AssetExtensionTest.php b/tests/Twig/AssetExtensionTest.php index 54c7205..2da7827 100644 --- a/tests/Twig/AssetExtensionTest.php +++ b/tests/Twig/AssetExtensionTest.php @@ -10,10 +10,11 @@ namespace Nice\Tests\Twig; use Nice\Twig\AssetExtension; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; -class AssetExtensionTest extends \PHPUnit_Framework_TestCase +class AssetExtensionTest extends TestCase { /** * Test a simple example diff --git a/tests/Twig/RouterExtensionTest.php b/tests/Twig/RouterExtensionTest.php index c062c40..3f69e3e 100644 --- a/tests/Twig/RouterExtensionTest.php +++ b/tests/Twig/RouterExtensionTest.php @@ -10,11 +10,12 @@ namespace Nice\Tests\Twig; use Nice\Twig\RouterExtension; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Scope; use Symfony\Component\HttpFoundation\Request; -class RouterExtensionTest extends \PHPUnit_Framework_TestCase +class RouterExtensionTest extends TestCase { /** * Tests getCurrentController @@ -138,19 +139,6 @@ public function testBasicMethods() $this->assertEquals('router', $extension->getName()); } - /** - * Tests that an exception is thrown when the request service is unavailable - */ - public function testCannotGetRequest() - { - $container = new Container(); - $extension = new RouterExtension($container); - - $this->setExpectedException('RuntimeException', 'Unable to get "request" service'); - - $extension->getController(); - } - /** * @param string $uri The URI to give to the request * @@ -164,8 +152,6 @@ public function getExtension($uri = '/', $controller = 'HomeController::indexAct $container = $container ?: new Container(); $container->set('request', $request); $container->set('app', new \stdClass()); - $container->addScope(new Scope('request')); - $container->enterScope('request'); return new RouterExtension($container); } From 476a5f13aa1134e1aae338d1fa415f4ec7d2f5a3 Mon Sep 17 00:00:00 2001 From: Tyler Sommer Date: Fri, 24 Nov 2023 21:15:29 -0700 Subject: [PATCH 2/3] Make twig service public --- src/Extension/TwigExtension.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Extension/TwigExtension.php b/src/Extension/TwigExtension.php index 67d177a..fcb0e4e 100644 --- a/src/Extension/TwigExtension.php +++ b/src/Extension/TwigExtension.php @@ -78,7 +78,8 @@ public function load(array $config, ContainerBuilder $container) ->addArgument(array('%twig.template_dir%')); $container->register('twig', 'Twig_Environment') - ->addArgument(new Reference('twig.loader')); + ->addArgument(new Reference('twig.loader')) + ->setPublic(true); $container->register('templating.engine.twig', 'Nice\Templating\TwigEngine') ->addArgument(new Reference('twig')) From 8c7c5b4f9ef47f173b8d5c7226a116cb41ef1480 Mon Sep 17 00:00:00 2001 From: Tyler Sommer Date: Fri, 24 Nov 2023 21:17:51 -0700 Subject: [PATCH 3/3] Adjust function names in Asset and RouterExtension --- src/Twig/AssetExtension.php | 3 ++- src/Twig/RouterExtension.php | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Twig/AssetExtension.php b/src/Twig/AssetExtension.php index 990705e..701e643 100644 --- a/src/Twig/AssetExtension.php +++ b/src/Twig/AssetExtension.php @@ -10,6 +10,7 @@ namespace Nice\Twig; use Symfony\Component\DependencyInjection\ContainerInterface; +use Twig\TwigFunction; class AssetExtension extends \Twig_Extension { @@ -36,7 +37,7 @@ public function __construct(ContainerInterface $container) public function getFunctions() { return array( - new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')) + new TwigFunction('asset', array($this, 'getAssetUrl')) ); } diff --git a/src/Twig/RouterExtension.php b/src/Twig/RouterExtension.php index 0270ac9..e476969 100644 --- a/src/Twig/RouterExtension.php +++ b/src/Twig/RouterExtension.php @@ -63,13 +63,13 @@ public function getGlobals() public function getFunctions() { return array( - 'current_controller' => new TwigFunction('getController', [$this, 'getController']), - 'current_action' => new TwigFunction('getAction', [$this, 'getAction']), - 'current_route' => new TwigFunction('getRoute', [$this, 'getRoute']), - 'is_current_controller' => new TwigFunction('isCurrentController', [$this, 'isCurrentController']), - 'is_current_route' => new TwigFunction('isCurrentRoute', [$this, 'isCurrentRoute']), - 'path' => new TwigFunction('generatePath', [$this, 'generatePath']), - 'url' => new TwigFunction('generateUrl', [$this, 'generateUrl']) + new TwigFunction('current_controller', [$this, 'getController']), + new TwigFunction('current_action', [$this, 'getAction']), + new TwigFunction('current_route', [$this, 'getRoute']), + new TwigFunction('is_current_controller', [$this, 'isCurrentController']), + new TwigFunction('is_current_route', [$this, 'isCurrentRoute']), + new TwigFunction('path', [$this, 'generatePath']), + new TwigFunction('url', [$this, 'generateUrl']) ); }