Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use SumoCoders\FrameworkCoreBundle\Twig\PaginatorRuntime;
use SumoCoders\FrameworkCoreBundle\Twig\PaginatorExtension;
use SumoCoders\FrameworkCoreBundle\Twig\FrameworkExtension;
use SumoCoders\FrameworkCoreBundle\Service\BreadcrumbTrail;
Expand Down Expand Up @@ -102,16 +101,13 @@
* Twig extensions
*/
->set('framework.framework_extension', FrameworkExtension::class)
->tag('twig.extension')
->tag('twig.attribute_extension')

->set('framework.paginator_extension', PaginatorExtension::class)
->tag('twig.extension')

->set('framework.paginator_runtime', PaginatorRuntime::class)
->tag('twig.runtime')
->tag('twig.attribute_extension')

->set('framework.content_extension', ContentExtension::class)
->tag('twig.extension')
->tag('twig.attribute_extension')

/*
* Breadcrumbs
Expand Down
19 changes: 7 additions & 12 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
namespace SumoCoders\FrameworkCoreBundle\Twig;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Symfony\Component\Filesystem\Filesystem;
use Twig\Attribute\AsTwigFunction;

class ContentExtension extends AbstractExtension
readonly class ContentExtension
{
public function __construct(
#[Autowire('%kernel.project_dir%/public')]
private string $publicFolder
private string $publicFolder,
private Filesystem $filesystem
) {
}

public function getFunctions(): array
{
return [
new TwigFunction('content', [$this, 'getContent'], ['is_safe' => ['html']]),
];
}

#[AsTwigFunction('content', isSafe: ['html'])]
public function getContent(string $path): string
{
return file_get_contents($this->publicFolder . $path);
return $this->filesystem->readFile($this->publicFolder . $path);
}
}
25 changes: 8 additions & 17 deletions src/Twig/FrameworkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,22 @@
namespace SumoCoders\FrameworkCoreBundle\Twig;

use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\Attribute\AsTwigFilter;
use Twig\Attribute\AsTwigFunction;

class FrameworkExtension extends AbstractExtension
readonly class FrameworkExtension
{
public function __construct(private RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

public function getFilters(): array
#[AsTwigFilter('ucfirst')]
public static function ucfirst(string $string): string
{
return [
new TwigFilter('ucfirst', 'ucfirst'),
];
}

public function getFunctions(): array
{
return [
new TwigFunction('theme', [$this, 'determineTheme']),
new TwigFunction('sidebarIsOpen', [$this, 'sidebarIsOpen']),
];
return ucfirst($string);
}

#[AsTwigFunction('theme')]
public function determineTheme(): string
{
if (is_null($this->requestStack->getCurrentRequest())) {
Expand All @@ -42,6 +32,7 @@ public function determineTheme(): string
return 'theme-' . $this->requestStack->getCurrentRequest()->cookies->get('theme');
}

#[AsTwigFunction('sidebarIsOpen')]
public function sidebarIsOpen(): bool
{
if (is_null($this->requestStack->getCurrentRequest())) {
Expand Down
50 changes: 43 additions & 7 deletions src/Twig/PaginatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,51 @@

namespace SumoCoders\FrameworkCoreBundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use SumoCoders\FrameworkCoreBundle\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Attribute\AsTwigFunction;
use Twig\Environment;

final class PaginatorExtension extends AbstractExtension
readonly class PaginatorExtension
{
public function getFunctions(): array
public function __construct(
private RequestStack $requestStack,
) {
}

#[AsTwigFunction('pagination', needsEnvironment: true, isSafe: ['html'])]
public function renderPagination(Environment $env, Paginator $paginator): string
{
$request = $this->getRequest();

if (null !== $this->requestStack->getParentRequest()) {
throw new \RuntimeException('We can not guess the route when used in a sub-request');
}

$route = $request->attributes->get('_route');

// Make sure we read the route parameters from the passed option array
$routeParams = array_merge($request->query->all(), $request->attributes->get('_route_params', []));

$paginator->calculateStartAndEndPage();

return $env->load('@SumoCodersFrameworkCore/Twig/pagination.html.twig')->renderBlock(
'pager',
[
'paginator' => $paginator,
'route' => $route,
'routeParams' => $routeParams,
'current_page' => $paginator->getCurrentPage(),
'start_page' => $paginator->getStartPage(),
'end_page' => $paginator->getEndPage(),
'page_count' => $paginator->getNumberOfPages(),
]
);
}

private function getRequest(): ?Request
{
return [
new TwigFunction('pagination', [PaginatorRuntime::class, 'renderPagination'], ['is_safe' => ['html']]),
];
return $this->requestStack->getCurrentRequest();
}
}
52 changes: 0 additions & 52 deletions src/Twig/PaginatorRuntime.php

This file was deleted.