Skip to content

Commit

Permalink
Merge pull request #764 from KnpLabs/lazy-twig-extension
Browse files Browse the repository at this point in the history
improve Twig extension
  • Loading branch information
garak authored Mar 25, 2023
2 parents a9f4d77 + 160d417 commit 8da698f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit75Migration:risky' => true,
'@PHP80Migration:risky' => true,
'@PHPUnit84Migration:risky' => true,
'ordered_imports' => true,
'declare_strict_types' => false,
'native_function_invocation' => ['include' => ['@all']],
Expand Down
6 changes: 5 additions & 1 deletion config/paginator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
</service>

<service id="knp_paginator.twig.extension.pagination" class="Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension">
<argument type="service" id="knp_paginator.helper.processor" />
<tag name="twig.extension" />
</service>

<service id="Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationRuntime">
<argument type="service" id="knp_paginator.helper.processor" />
<tag name="twig.runtime" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ parameters:
-
message: "#^Parameter \\#1 \\$name of method Twig\\\\Environment\\:\\:render\\(\\) expects string\\|Twig\\\\TemplateWrapper, string\\|null given\\.$#"
count: 3
path: src/Twig/Extension/PaginationExtension.php
path: src/Twig/Extension/PaginationRuntime.php

75 changes: 5 additions & 70 deletions src/Twig/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,19 @@

namespace Knp\Bundle\PaginatorBundle\Twig\Extension;

use Knp\Bundle\PaginatorBundle\Helper\Processor;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class PaginationExtension extends AbstractExtension
{
private Processor $processor;

public function __construct(Processor $processor)
{
$this->processor = $processor;
}

public function getFunctions(): array
{
$options = ['is_safe' => ['html'], 'needs_environment' => true];

return [
new TwigFunction('knp_pagination_render', [$this, 'render'], ['is_safe' => ['html'], 'needs_environment' => true]),
new TwigFunction('knp_pagination_sortable', [$this, 'sortable'], ['is_safe' => ['html'], 'needs_environment' => true]),
new TwigFunction('knp_pagination_filter', [$this, 'filter'], ['is_safe' => ['html'], 'needs_environment' => true]),
new TwigFunction('knp_pagination_render', [PaginationRuntime::class, 'render'], $options),
new TwigFunction('knp_pagination_sortable', [PaginationRuntime::class, 'sortable'], $options),
new TwigFunction('knp_pagination_filter', [PaginationRuntime::class, 'filter'], $options),
];
}

/**
* Renders the pagination template.
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param array<string, mixed> $queryParams
* @param array<string, mixed> $viewParams
*/
public function render(Environment $env, SlidingPaginationInterface $pagination, ?string $template = null, ?array $queryParams = [], ?array $viewParams = []): string
{
return $env->render(
$template ?: $pagination->getTemplate(),
$this->processor->render($pagination, $queryParams ?? [], $viewParams ?? [])
);
}

/**
* Create a sort url for the field named $title
* and identified by $key which consists of
* alias and field. $options holds all link
* parameters like "alt, class" and so on.
*
* $key example: "article.title"
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param string|array<string, mixed> $key
* @param array<string, mixed> $options
* @param array<string, mixed> $params
*/
public function sortable(Environment $env, SlidingPaginationInterface $pagination, string $title, $key, array $options = [], array $params = [], ?string $template = null): string
{
return $env->render(
$template ?: $pagination->getSortableTemplate(),
$this->processor->sortable($pagination, $title, $key, $options, $params)
);
}

/**
* Create a filter url for the field named $title
* and identified by $key which consists of
* alias and field. $options holds all link
* parameters like "alt, class" and so on.
*
* $key example: "article.title"
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param array<string, mixed> $fields
* @param array<string, mixed> $options
* @param array<string, mixed>|null $params
*/
public function filter(Environment $env, SlidingPaginationInterface $pagination, array $fields, ?array $options = [], ?array $params = [], ?string $template = null): string
{
return $env->render(
$template ?: $pagination->getFiltrationTemplate(),
$this->processor->filter($pagination, $fields, $options ?? [], $params ?? [])
);
}
}
89 changes: 89 additions & 0 deletions src/Twig/Extension/PaginationRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Knp\Bundle\PaginatorBundle\Twig\Extension;

use Knp\Bundle\PaginatorBundle\Helper\Processor;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;

final class PaginationRuntime implements RuntimeExtensionInterface
{
private Processor $processor;

public function __construct(Processor $processor)
{
$this->processor = $processor;
}

/**
* Renders the pagination template.
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param array<string, mixed> $queryParams
* @param array<string, mixed> $viewParams
*/
public function render(
Environment $env,
SlidingPaginationInterface $pagination,
?string $template = null,
?array $queryParams = [],
?array $viewParams = [],
): string {
return $env->render(
$template ?: $pagination->getTemplate(),
$this->processor->render($pagination, $queryParams ?? [], $viewParams ?? [])
);
}

/**
* Create a sort url for the field named $title and identified by $key which consists of
* alias and field. $options holds all link parameters like "alt, class" and so on.
*
* $key example: "article.title"
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param string|array<string, mixed> $key
* @param array<string, mixed> $options
* @param array<string, mixed> $params
*/
public function sortable(
Environment $env,
SlidingPaginationInterface $pagination,
string $title,
array|string $key,
array $options = [],
array $params = [],
?string $template = null,
): string {
return $env->render(
$template ?: $pagination->getSortableTemplate(),
$this->processor->sortable($pagination, $title, $key, $options, $params)
);
}

/**
* Create a filter url for the field named $title and identified
* by $key which consists of alias and field.
* $options holds all link parameters like "alt, class" and so on.
*
* $key example: "article.title"
*
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination
* @param array<string, mixed> $fields
* @param array<string, mixed> $options
* @param array<string, mixed>|null $params
*/
public function filter(
Environment $env,
SlidingPaginationInterface $pagination,
array $fields, ?array $options = [],
?array $params = [],
?string $template = null,
): string {
return $env->render(
$template ?: $pagination->getFiltrationTemplate(),
$this->processor->filter($pagination, $fields, $options ?? [], $params ?? [])
);
}
}

0 comments on commit 8da698f

Please sign in to comment.