professional-php-sample-code icon indicating copy to clipboard operation
professional-php-sample-code copied to clipboard

Class 'Twig_Loader_Filesystem' not found

Open AleCx4 opened this issue 5 years ago • 4 comments

As the title says, I have followed the code line by line up till page 69 of the book and I get hit by the above statement.

AleCx4 avatar Jan 17 '21 09:01 AleCx4

Found the issue, the use of the Twig_Loader_Filesystem has been deprecated since Twig version 2.7, is there anyway the book can be updated to reflect on this issue? I do not expect to troubleshoot such items when working through a book. Specially one as good as this one.

AleCx4 avatar Jan 17 '21 10:01 AleCx4

Add the same issue.

Solved it with:

src/Framework/Rendering/TwigTemplateRenderer.php

<?php declare(strict_types=1);

namespace SocialNews\Framework\Rendering;

use \Twig\Environment;

final class TwigTemplateRenderer implements TemplateRenderer
{
    private $twigEnvironment;

    public function __construct(Environment $twigEnvironment)
    {
        $this->twigEnvironment = $twigEnvironment;
    }

    public function render(string $template, array $data = []): string
    {
        return $this->twigEnvironment->render($template, $data);
    }
}

src/Framework/Rendering/TwigTemplateRendererFactory.php

<?php declare(strict_types=1);

namespace SocialNews\Framework\Rendering;

use \Twig\Loader\FilesystemLoader;
use \Twig\Environment;

final class TwigTemplateRendererFactory
{
    public function create(): TwigTemplateRenderer
    {
        $loader = new FilesystemLoader();
        $twigEnviroment = new Environment($loader);
        return new TwigTemplateRenderer($twigEnviroment);
    }
}

Hope it helps someone else.

NCSantos avatar Feb 27 '21 00:02 NCSantos

@NCSantos was a huge help but I still was having issues (might be a me thing). Looking at Twig it required the template directory as a string. In src/Framework/Rendering/TwigTemplateRendererFactory.php I added the toString()function from TemplateDirectory.php to the argument variable:

public function create(): TwigTemplateRenderer
{
        $loader = new FilesystemLoader($this->templateDirectory->toString());
....
}

Maybe a better way to do it, but it fixed it for me. This is an addition to what @NCSantos posted above.

themagicteeth avatar Jul 14 '21 23:07 themagicteeth

@themagicteeth thanks much, solved it for me. I was stuck on page 83 (value objects).

Wondarar avatar Nov 29 '21 20:11 Wondarar