phpsymfonycachingfoshttpcachebundle

FOSHttpCache with Symfony reverse proxy


So I'm trying to configure the FOSHttpCacheBundle (latest release ^2.0) with the default Symfony HttpCache.

Here is the content of my AppCache.php according to the docs:

<?php

use FOS\HttpCache\SymfonyCache\CacheInvalidation;
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\DebugListener;
use FOS\HttpCache\SymfonyCache\CustomTtlListener;
use FOS\HttpCache\SymfonyCache\PurgeListener;
use FOS\HttpCache\SymfonyCache\RefreshListener;
use FOS\HttpCache\SymfonyCache\UserContextListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;

class AppCache extends HttpCache implements CacheInvalidation
{
    use EventDispatchingHttpCache;

    /**
     * Overwrite constructor to register event listeners for FOSHttpCache.
     */
    public function __construct(
        HttpKernelInterface $kernel,
        StoreInterface $store,
        SurrogateInterface $surrogate = null,
        array $options = []
    ) {
        parent::__construct($kernel, $store, $surrogate, $options);

        $this->addSubscriber(new CustomTtlListener());
        $this->addSubscriber(new PurgeListener());
        $this->addSubscriber(new RefreshListener());
        $this->addSubscriber(new UserContextListener());
        if (isset($options['debug']) && $options['debug']) {
            $this->addSubscriber(new DebugListener());
        }
    }

    /**
     * Made public to allow event listeners to do refresh operations.
     *
     * {@inheritDoc}
     */
    public function fetch(Request $request, $catch = false)
    {
        return parent::fetch($request, $catch);
    }
}

Now according to the Symfony docs, enabling the caching proxy is just a matter of setting the app.php file as follow (uncommenting that last line):

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);

Which gives me a nice:

PHP message: PHP Catchable fatal error: Argument 2 passed to Symfony\Component\HttpKernel\HttpCache\HttpCache::__construct() must be an instance of Symfony\Component\HttpKernel\HttpCache\StoreInterface, none given, called in /php/api/current/web/app.php on line 11 and defined in /php/api/current/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php on line 78

Which makes perfect sense given the HttpCache class constructor.

So the question is, is it the doc that's not up to date, or is it just me missing something really obvious?


Solution

  • use Symfony\Component\HttpKernel\HttpCache\Store;
    
    $kernel = new AppKernel('dev', true);
    $kernel->loadClassCache();
    $store = new Store('app/cache');
    $kernel = new AppCache($kernel, $store);