Skip to content

Commit

Permalink
Merge pull request #28 from peter-gribanov/twig_namespace
Browse files Browse the repository at this point in the history
Use twig/twig 1.34 for easy to migrate to latest stable Twig
  • Loading branch information
peter-gribanov authored Nov 8, 2019
2 parents 7d882cc + 5dd06b5 commit e7b65d9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 79 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/framework-bundle": "~2.3|~3.0|~4.0",
"symfony/expression-language": "~2.3|~3.0|~4.0",
"doctrine/orm": "~2.4|~2.5|~2.6",
"twig/twig": "^1.12|^2.0"
"twig/twig": "^1.34|^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.36",
Expand Down
128 changes: 57 additions & 71 deletions src/Twig/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,71 @@
* @license http://opensource.org/licenses/MIT
*/

// hook for support Twig > 2.7
namespace GpsLab\Bundle\PaginationBundle\Twig\Extension;

namespace
use GpsLab\Bundle\PaginationBundle\Service\Configuration;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class PaginationExtension extends AbstractExtension
{
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* @var string
*/
private $template;

if (!class_exists('Twig_Extension') && class_exists('Twig\Extension\AbstractExtension')) {
class Twig_Extension extends AbstractExtension
{
}
/**
* @param string $template
*/
public function __construct($template)
{
$this->template = $template;
}

if (!class_exists('Twig_SimpleFunction') && class_exists('Twig\TwigFunction')) {
class Twig_SimpleFunction extends TwigFunction
{
}
/**
* @return array
*/
public function getFunctions()
{
return [
new TwigFunction(
'pagination_render',
[$this, 'renderPagination'],
['is_safe' => ['html'], 'needs_environment' => true]
),
];
}
}

namespace GpsLab\Bundle\PaginationBundle\Twig\Extension
{
use GpsLab\Bundle\PaginationBundle\Service\Configuration;
/**
* @param \Twig\Environment $env
* @param Configuration $pagination
* @param string $template
* @param array $view_params
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*
* @return string
*/
public function renderPagination(
Environment $env,
Configuration $pagination,
$template = null,
array $view_params = []
) {
return $env->render(
$template ?: $this->template,
array_merge($view_params, ['pagination' => $pagination->getView()])
);
}

class PaginationExtension extends \Twig_Extension
/**
* @return string
*/
public function getName()
{
/**
* @var string
*/
private $template;

/**
* @param string $template
*/
public function __construct($template)
{
$this->template = $template;
}

/**
* @return array
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction(
'pagination_render',
[$this, 'renderPagination'],
['is_safe' => ['html'], 'needs_environment' => true]
),
];
}

/**
* @param \Twig_Environment $env
* @param Configuration $pagination
* @param string $template
* @param array $view_params
*
* @return string
*/
public function renderPagination(
\Twig_Environment $env,
Configuration $pagination,
$template = null,
array $view_params = []
) {
return $env->render(
$template ?: $this->template,
array_merge($view_params, ['pagination' => $pagination->getView()])
);
}

/**
* @return string
*/
public function getName()
{
return 'gpslab_pagination_extension';
}
return 'gpslab_pagination_extension';
}
}
14 changes: 7 additions & 7 deletions tests/Twig/Extension/PaginationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function testGetFunctions()

self::assertInternalType('array', $functions);
self::assertCount(1, $functions);
self::assertInstanceOf('Twig_SimpleFunction', $functions[0]);
self::assertInstanceOf('Twig\TwigFunction', $functions[0]);
}

public function testRender()
{
$expected = 'bar';
$view = 'baz';
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig_Environment */
$env = $this->getMockNoConstructor('Twig_Environment');
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig\Environment */
$env = $this->getMockNoConstructor('Twig\Environment');
$env
->expects($this->once())
->method('render')
Expand All @@ -70,8 +70,8 @@ public function testRenderChangeTemplate()
$expected = 'bar';
$view = 'baz';
$template = 'my_template';
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig_Environment */
$env = $this->getMockNoConstructor('Twig_Environment');
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig\Environment */
$env = $this->getMockNoConstructor('Twig\Environment');
$env
->expects($this->once())
->method('render')
Expand All @@ -97,8 +97,8 @@ public function testRenderNoOverrideTemplateParams()
{
$expected = 'bar';
$view = 'baz';
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig_Environment */
$env = $this->getMockNoConstructor('Twig_Environment');
/* @var $env \PHPUnit_Framework_MockObject_MockObject|\Twig\Environment */
$env = $this->getMockNoConstructor('Twig\Environment');
$env
->expects($this->once())
->method('render')
Expand Down

0 comments on commit e7b65d9

Please sign in to comment.