Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use twigphp/cache-extra? #415

Open
event15 opened this issue Oct 14, 2021 · 2 comments
Open

How to use twigphp/cache-extra? #415

event15 opened this issue Oct 14, 2021 · 2 comments

Comments

@event15
Copy link

event15 commented Oct 14, 2021

Hey,
I am trying to use the twigphp/cache-extra (https://twig.symfony.com/doc/3.x/tags/cache.html) extension in TwigBridge.

In app/config/twigbridge.php I added in extensions->enabled section an entry:

TwigExtraCacheExtension::class,

When I try to use the cache block, I get an error:

ErrorException (E_ERROR)
Unable to load the "Twig\Extra\Cache\CacheRuntime" runtime.

The twig documentation says:

If you are not using Symfony, you must also register the extension runtime:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Twig\Extra\Cache\CacheRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;

$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
    public function load($class) {
        if (CacheRuntime::class === $class) {
            return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter()));
        }
    }
});

However, I have no idea where in the application to add such a loader. From what I see, extensions are added in this file:
vendor/barryvdh/laravel-form-bridge/src/ServiceProvider.php

Does this mean I should overwrite this service provider and use a custom one?

@adrienne
Copy link

@rcrowe any help available here?

@webard
Copy link

webard commented Dec 27, 2022

Just add it to AppServiceProvider for example:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Twig\Extra\Cache\CacheRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
...
class AppServiceProvider extends ServiceProvider
{
	public function boot() {
			$twig = app(\Twig\Environment::class);
	        $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface
	        {
	            public function load($class)
	            {
	                if (CacheRuntime::class === $class) {
	                    return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter()));
	                }
	            }
	        });
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants