Api-platform: Disable cache invalidation via ENV variables

Created on 6 Jan 2019  路  3Comments  路  Source: api-platform/api-platform

When I try:

api_platform:
    http_cache:
        invalidation:
            enabled: '%env(bool:VARNISH_ENABLED)%'

and

VARNISH_ENABLED=false

I get:

(1/1) EnvParameterException
Environment variables "bool:VARNISH_ENABLED" are never used. Please, check your container's configuration.

According to https://github.com/symfony/symfony/issues/29276 it's not a bug in Symfony? I tried false, FALSE, true, 1, 0, but won't parse to a boolean. Casting it to an int with env(int:) works but then the configuration still won't accept the value obviously, since it expects a boolean.

Maybe I'm missing something?

question

Most helpful comment

You can add the env variable in services parameters and that "never used" exception is gone but this fixes nothing since the variable is not resolved when building the container resulting in error with boolean node (api_platform.http_cache.invalidation.enabled) getting string value casted to boolean (always true there). I'm guessing it's the issue with runtime env processor that will not be fixed any time soon, if ever.

What we did to solve this issue is to create a decorator for Varnish purger.

API Platform cache invalidation is set to true in config.

Decorator is as following:

<?php

declare(strict_types=1);

namespace App\HttpCache;

use ApiPlatform\Core\HttpCache\PurgerInterface;

class VarnishPurger implements PurgerInterface
{
    private PurgerInterface $decorated;
    private bool $enableVarnish;

    /**
     * @param PurgerInterface $decorated
     * @param mixed $enableVarnish
     */
    public function __construct(PurgerInterface $decorated, $enableVarnish)
    {
        $this->decorated = $decorated;
        $this->enableVarnish = (bool)$enableVarnish;
    }

    public function purge(array $iris): void
    {
        if ($this->enableVarnish === false) {
            return;
        }

        $this->decorated->purge($iris);
    }
}

and it's configured in services:

    App\HttpCache\VarnishPurger:
        decorates: 'api_platform.http_cache.purger.varnish'
        arguments:
            $enableVarnish: '%env(VARNISH_ENABLED)%'

All 3 comments

I've got the same issue while trying to disable email delivery in Swiftmailer configuration.

I ended up creating a new environment with the required configuration hardcoded in it. Feels a bit like an overkill.

You can add the env variable in services parameters and that "never used" exception is gone but this fixes nothing since the variable is not resolved when building the container resulting in error with boolean node (api_platform.http_cache.invalidation.enabled) getting string value casted to boolean (always true there). I'm guessing it's the issue with runtime env processor that will not be fixed any time soon, if ever.

What we did to solve this issue is to create a decorator for Varnish purger.

API Platform cache invalidation is set to true in config.

Decorator is as following:

<?php

declare(strict_types=1);

namespace App\HttpCache;

use ApiPlatform\Core\HttpCache\PurgerInterface;

class VarnishPurger implements PurgerInterface
{
    private PurgerInterface $decorated;
    private bool $enableVarnish;

    /**
     * @param PurgerInterface $decorated
     * @param mixed $enableVarnish
     */
    public function __construct(PurgerInterface $decorated, $enableVarnish)
    {
        $this->decorated = $decorated;
        $this->enableVarnish = (bool)$enableVarnish;
    }

    public function purge(array $iris): void
    {
        if ($this->enableVarnish === false) {
            return;
        }

        $this->decorated->purge($iris);
    }
}

and it's configured in services:

    App\HttpCache\VarnishPurger:
        decorates: 'api_platform.http_cache.purger.varnish'
        arguments:
            $enableVarnish: '%env(VARNISH_ENABLED)%'

It's not possible for the same reason as explained in https://github.com/symfony/symfony/issues/29276#issuecomment-440954207

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ajaunasse picture ajaunasse  路  3Comments

dunglas picture dunglas  路  3Comments

benbender picture benbender  路  4Comments

kswzr picture kswzr  路  3Comments

irmantas picture irmantas  路  4Comments