From b3592c72a509a67c031275ee597780cbceb5f6a8 Mon Sep 17 00:00:00 2001 From: Yann Le Scouarnec Date: Tue, 19 Jun 2018 12:50:27 +0200 Subject: [PATCH 01/12] README.md Typo fix Fixed "profle" to "profile" typo in route name. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70e6ab9..dcb8af5 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ You can use `path_for` to generate complete URLs to any Slim application named r {% block body %}

User List

{% endblock %} From e8bb6aada9ee72e50919318ed70745de1318cbd8 Mon Sep 17 00:00:00 2001 From: Michael Bolli Date: Mon, 19 Nov 2018 17:44:15 +0100 Subject: [PATCH 02/12] annotate twig exceptions in fetch method --- src/Twig.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Twig.php b/src/Twig.php index 5db67b3..fbdfb58 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -79,6 +79,10 @@ public function addExtension(\Twig_ExtensionInterface $extension) * @param string $template Template pathname relative to templates directory * @param array $data Associative array of template variables * + * @throws Twig_Error_Loader When the template cannot be found + * @throws Twig_Error_Syntax When an error occurred during compilation + * @throws Twig_Error_Runtime When an error occurred during rendering + * * @return string */ public function fetch($template, $data = []) From 92a7255a0a8cd8cbfebc00eaabc44bf0dfe9141d Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 13 Mar 2019 08:53:01 +0100 Subject: [PATCH 03/12] Replace deprecated methods as of Twig 2.7 --- src/Twig.php | 18 +++++++++--------- src/TwigExtension.php | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Twig.php b/src/Twig.php index 5db67b3..007e483 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -24,14 +24,14 @@ class Twig implements \ArrayAccess /** * Twig loader * - * @var \Twig_LoaderInterface + * @var \Twig\Loader\LoaderInterface */ protected $loader; /** * Twig environment * - * @var \Twig_Environment + * @var \Twig\Environment */ protected $environment; @@ -55,7 +55,7 @@ class Twig implements \ArrayAccess public function __construct($path, $settings = []) { $this->loader = $this->createLoader(is_string($path) ? [$path] : $path); - $this->environment = new \Twig_Environment($this->loader, $settings); + $this->environment = new \Twig\Environment($this->loader, $settings); } /******************************************************************************** @@ -65,9 +65,9 @@ public function __construct($path, $settings = []) /** * Proxy method to add an extension to the Twig environment * - * @param \Twig_ExtensionInterface $extension A single extension instance or an array of instances + * @param \Twig\Extension\ExtensionInterface $extension A single extension instance or an array of instances */ - public function addExtension(\Twig_ExtensionInterface $extension) + public function addExtension(\Twig\Extension\ExtensionInterface $extension) { $this->environment->addExtension($extension); } @@ -138,11 +138,11 @@ public function render(ResponseInterface $response, $template, $data = []) * Create a loader with the given path * * @param array $paths - * @return \Twig_Loader_Filesystem + * @return \Twig\Loader\FilesystemLoader */ private function createLoader(array $paths) { - $loader = new \Twig_Loader_Filesystem(); + $loader = new \Twig\Loader\FilesystemLoader(); foreach ($paths as $namespace => $path) { if (is_string($namespace)) { @@ -162,7 +162,7 @@ private function createLoader(array $paths) /** * Return Twig loader * - * @return \Twig_LoaderInterface + * @return \Twig\Loader\LoaderInterface */ public function getLoader() { @@ -172,7 +172,7 @@ public function getLoader() /** * Return Twig environment * - * @return \Twig_Environment + * @return \Twig\Environment */ public function getEnvironment() { diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 806de14..e81e863 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -8,7 +8,7 @@ */ namespace Slim\Views; -class TwigExtension extends \Twig_Extension +class TwigExtension extends \Twig\Extension\AbstractExtension { /** * @var \Slim\Interfaces\RouterInterface @@ -34,10 +34,10 @@ public function getName() public function getFunctions() { return [ - new \Twig_SimpleFunction('path_for', array($this, 'pathFor')), - new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')), - new \Twig_SimpleFunction('is_current_path', array($this, 'isCurrentPath')), - new \Twig_SimpleFunction('current_path', array($this, 'currentPath')), + new \Twig\TwigFunction('path_for', array($this, 'pathFor')), + new \Twig\TwigFunction('base_url', array($this, 'baseUrl')), + new \Twig\TwigFunction('is_current_path', array($this, 'isCurrentPath')), + new \Twig\TwigFunction('current_path', array($this, 'currentPath')), ]; } From bb74df50130b2d062d10320143e1ce3e364e5536 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 20 Mar 2019 20:28:45 +0100 Subject: [PATCH 04/12] Update Twig versions in composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7faa51f..1653ff3 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "require": { "php": ">=5.5.0", - "twig/twig": "^1.18|^2.0", + "twig/twig": "^1.38|^2.7", "psr/http-message": "^1.0" }, "require-dev": { From 6a7b4f862e50c4ee0be228492d4fba83ae827c9d Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 7 Aug 2018 11:28:05 +0200 Subject: [PATCH 05/12] implement url_for() fixes #101 --- src/TwigExtension.php | 32 ++++++++++++++++++++++++++++++++ tests/TwigExtensionTest.php | 14 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index e81e863..5af8504 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -8,6 +8,8 @@ */ namespace Slim\Views; +use Slim\Http\Uri; + class TwigExtension extends \Twig\Extension\AbstractExtension { /** @@ -35,6 +37,7 @@ public function getFunctions() { return [ new \Twig\TwigFunction('path_for', array($this, 'pathFor')), + new \Twig\TwigFunction('url_for', array($this, 'urlFor')), new \Twig\TwigFunction('base_url', array($this, 'baseUrl')), new \Twig\TwigFunction('is_current_path', array($this, 'isCurrentPath')), new \Twig\TwigFunction('current_path', array($this, 'currentPath')), @@ -46,6 +49,35 @@ public function pathFor($name, $data = [], $queryParams = [], $appName = 'defaul return $this->router->pathFor($name, $data, $queryParams); } + /** + * Similar to pathFor but returns a fully qualified URL + * + * @param string $name The name of the route + * @param array $data Route placeholders + * @param array $queryParams + * @param string $appName + * @return string fully qualified URL + */ + public function urlFor($name, $data = [], $queryParams = [], $appName = 'default') + { + $path = $this->pathFor($name, $data, $queryParams, $appName); + + /** @var Uri $uri */ + if(is_string($this->uri)) { + $uri = Uri::createFromString($this->uri); + } else { + $uri = $this->uri; + } + + $scheme = $uri->getScheme(); + $authority = $uri->getAuthority(); + + $host = ($scheme ? $scheme . ':' : '') + . ($authority ? '//' . $authority : ''); + + return $host.$path; + } + public function baseUrl() { if (is_string($this->uri)) { diff --git a/tests/TwigExtensionTest.php b/tests/TwigExtensionTest.php index fe04c04..548ef75 100644 --- a/tests/TwigExtensionTest.php +++ b/tests/TwigExtensionTest.php @@ -74,4 +74,18 @@ public function testCurrentPath($router, $uri, $withQueryString, $expected) $this->assertEquals($expected, $result); } + + public function testUrlFor() + { + $router = new Router(); + $router->setBasePath('/app'); + $router->map(['GET'], '/activate/{token}', null)->setName('activate'); + $uri = Uri::createFromString('http://example.com/app/hello/world?a=b'); + + $extension = new TwigExtension($router, $uri); + $result = $extension->urlFor('activate', ['token' => 'foo']); + + $expected = 'http://example.com/app/activate/foo'; + $this->assertEquals($expected, $result); + } } From ddf84a370cc8eda6d486db833667ba18e60006c2 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sat, 6 Apr 2019 17:23:37 +0100 Subject: [PATCH 06/12] Rename to full_url_for --- src/TwigExtension.php | 6 +++--- tests/TwigExtensionTest.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 5af8504..e4ca999 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -37,7 +37,7 @@ public function getFunctions() { return [ new \Twig\TwigFunction('path_for', array($this, 'pathFor')), - new \Twig\TwigFunction('url_for', array($this, 'urlFor')), + new \Twig\TwigFunction('full_url_for', array($this, 'fullUrlFor')), new \Twig\TwigFunction('base_url', array($this, 'baseUrl')), new \Twig\TwigFunction('is_current_path', array($this, 'isCurrentPath')), new \Twig\TwigFunction('current_path', array($this, 'currentPath')), @@ -58,12 +58,12 @@ public function pathFor($name, $data = [], $queryParams = [], $appName = 'defaul * @param string $appName * @return string fully qualified URL */ - public function urlFor($name, $data = [], $queryParams = [], $appName = 'default') + public function fullUrlFor($name, $data = [], $queryParams = [], $appName = 'default') { $path = $this->pathFor($name, $data, $queryParams, $appName); /** @var Uri $uri */ - if(is_string($this->uri)) { + if (is_string($this->uri)) { $uri = Uri::createFromString($this->uri); } else { $uri = $this->uri; diff --git a/tests/TwigExtensionTest.php b/tests/TwigExtensionTest.php index 548ef75..a69a4fe 100644 --- a/tests/TwigExtensionTest.php +++ b/tests/TwigExtensionTest.php @@ -75,7 +75,7 @@ public function testCurrentPath($router, $uri, $withQueryString, $expected) $this->assertEquals($expected, $result); } - public function testUrlFor() + public function testFullUrlFor() { $router = new Router(); $router->setBasePath('/app'); @@ -83,7 +83,7 @@ public function testUrlFor() $uri = Uri::createFromString('http://example.com/app/hello/world?a=b'); $extension = new TwigExtension($router, $uri); - $result = $extension->urlFor('activate', ['token' => 'foo']); + $result = $extension->fullUrlFor('activate', ['token' => 'foo']); $expected = 'http://example.com/app/activate/foo'; $this->assertEquals($expected, $result); From c1db60e43ce436516bdc29af712ddf62721f32f7 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sat, 6 Apr 2019 17:33:15 +0100 Subject: [PATCH 07/12] Update fetch() annotation to use namespaced errors --- src/Twig.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig.php b/src/Twig.php index fbdfb58..59ac17f 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -79,9 +79,9 @@ public function addExtension(\Twig_ExtensionInterface $extension) * @param string $template Template pathname relative to templates directory * @param array $data Associative array of template variables * - * @throws Twig_Error_Loader When the template cannot be found - * @throws Twig_Error_Syntax When an error occurred during compilation - * @throws Twig_Error_Runtime When an error occurred during rendering + * @throws \Twig\Error\LoaderError When the template cannot be found + * @throws \Twig_Error\SyntaxError When an error occurred during compilation + * @throws \Twig_Error\RuntimeError When an error occurred during rendering * * @return string */ From 7d337de7456ef9fd83d73059c66b4ab9ec16e7a3 Mon Sep 17 00:00:00 2001 From: Adam Averay Date: Thu, 28 Nov 2019 08:33:10 +1000 Subject: [PATCH 08/12] Support installing Twig 3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1653ff3..61c507f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "require": { "php": ">=5.5.0", - "twig/twig": "^1.38|^2.7", + "twig/twig": "^1.38|^2.7|^3.0", "psr/http-message": "^1.0" }, "require-dev": { From 917f58f40050cc3aa3224d0e2aacf4742e9fa0d3 Mon Sep 17 00:00:00 2001 From: Adam Averay Date: Thu, 28 Nov 2019 08:41:07 +1000 Subject: [PATCH 09/12] Replace internal method call with public API --- src/Twig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Twig.php b/src/Twig.php index b0ffd90..22a6b49 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -105,7 +105,7 @@ public function fetchBlock($template, $block, $data = []) { $data = array_merge($this->defaultVariables, $data); - return $this->environment->loadTemplate($template)->renderBlock($block, $data); + return $this->environment->load($template)->renderBlock($block, $data); } /** From 94ae7bd9f9ba5336bdc2c932e2451c059bcf6608 Mon Sep 17 00:00:00 2001 From: Adam Averay Date: Thu, 28 Nov 2019 08:53:38 +1000 Subject: [PATCH 10/12] Replace CI test of HHVM with PHP 7.2 & 7.3 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 65a5b39..bdc180c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,8 @@ php: - 5.6 - 7.0 - 7.1 - - hhvm + - 7.2 + - 7.3 before_script: composer install From 9ca22858de84d2b2fa3268b8bc44c32c29e6a1c8 Mon Sep 17 00:00:00 2001 From: Steven Tin Chee Eng Date: Wed, 20 Nov 2024 17:11:58 -0800 Subject: [PATCH 11/12] Use non-deprecated namespacing format for Twig environment --- src/Twig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Twig.php b/src/Twig.php index 9d9a049..8a55c7f 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -54,7 +54,7 @@ class Twig implements \ArrayAccess */ public function __construct($loader, $settings = []) { - $this->environment = new \Twig_Environment($loader, $settings); + $this->environment = new \Twig\Environment($loader, $settings); $this->loader = $loader; } From d9d6a0c7f04c926a0c5958ed0acf93a3dfe21f96 Mon Sep 17 00:00:00 2001 From: Steven Tin Chee Eng Date: Thu, 21 Nov 2024 15:45:25 -0800 Subject: [PATCH 12/12] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 70672ae..3b65b98 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/slimphp/Twig-View.svg?branch=master)](https://travis-ci.org/slimphp/Twig-View) -This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application. It works with Twig 1.18+ (PHP5.5+) and with Twig 2 (PHP7). +This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application. It works with Twig 1.18+ (PHP5.5+), Twig 2 (PHP7) and Twig 3. Requires Slim Framework 3 and PHP 5.5.0 or newer.