Skip to content

Commit

Permalink
Make fastly and cloudflare clients lazy loaded to support symfony sec…
Browse files Browse the repository at this point in the history
…rets

author DemigodCode <info@bl-websolutions.de> 1702912036 +0100
committer David Buchmann <david.buchmann@liip.ch> 1702989567 +0100

If one wants to use secrets as parameters for fastly or cloudflare, the compiling of the container will fail.
After compiling the container the termination event will cause this error cause it's not necessary to have the decrypt key available when compiling the container and the service can't be initialized.

This fix creates the services as lazy services which will not make the options necessary on compile time of the container.
  • Loading branch information
DemigodCode authored and dbu committed Dec 19, 2023
1 parent 6d6f849 commit 45af40c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

3.x
===

* Make `fastly` and `cloudflare` clients lazy loaded to support Symfony secrets that are only available at runtime, but
not yet when the container is built.

2.x
===

Expand Down
23 changes: 23 additions & 0 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FOS\HttpCache\CacheInvalidator;
use FOS\HttpCache\ProxyClient\ProxyClient;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;

/**
* The CacheManager is a CacheInvalidator but adds symfony Route support and
Expand All @@ -23,6 +24,11 @@
*/
class CacheManager extends CacheInvalidator
{
/**
* @var ProxyClient
*/
private $cache;

/**
* @var UrlGeneratorInterface
*/
Expand All @@ -44,6 +50,7 @@ class CacheManager extends CacheInvalidator
public function __construct(ProxyClient $cache, UrlGeneratorInterface $urlGenerator)
{
parent::__construct($cache);
$this->cache = $cache;
$this->urlGenerator = $urlGenerator;
}

Expand Down Expand Up @@ -88,4 +95,20 @@ public function refreshRoute($route, array $parameters = [], array $headers = []

return $this;
}

/**
* Send all pending invalidation requests.
*
* @return int the number of cache invalidations performed per caching server
*
* @throws \FOS\HttpCache\Exception\ExceptionCollection
*/
public function flush()
{
if (!$this->cache instanceof LazyObjectInterface || $this->cache->isLazyObjectInitialized()) {
return parent::flush();
}

return 0;
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/cloudflare.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<services>
<service id="fos_http_cache.proxy_client.cloudflare"
class="FOS\HttpCache\ProxyClient\Cloudflare"
public="true">
public="true"
lazy="true">
<argument type="service" id="fos_http_cache.proxy_client.cloudflare.http_dispatcher"/>
<argument>%fos_http_cache.proxy_client.cloudflare.options%</argument>
</service>
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/fastly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<services>
<service id="fos_http_cache.proxy_client.fastly"
class="FOS\HttpCache\ProxyClient\Fastly"
public="false">
public="false"
lazy="true">
<argument type="service" id="fos_http_cache.proxy_client.fastly.http_dispatcher"/>
<argument>%fos_http_cache.proxy_client.fastly.options%</argument>
</service>
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace FOS\HttpCacheBundle\Tests\Unit;

use FOS\HttpCache\ProxyClient\HttpProxyClient;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
use FOS\HttpCache\ProxyClient\ProxyClient;
use FOS\HttpCacheBundle\CacheManager;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;

class CacheManagerTest extends TestCase
{
Expand Down Expand Up @@ -84,4 +86,44 @@ public function testRefreshRoute()
->refreshRoute('route_with_params', ['id' => 123], ['X-Foo' => 'bar'])
;
}

public function testSkipFlushOnEmptyInvalidationsAndLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class)
->shouldNotReceive('flush')
->shouldReceive('isLazyObjectInitialized')->andReturn(false)
->getMock();

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}

public function testFlushOnNotLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class)
->shouldReceive('flush')->andReturn(0)
->shouldNotReceive('isLazyObjectInitialized')
->getMock();

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}

public function testFlushOnLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class, PurgeCapable::class);
$proxyClient->shouldReceive('flush')->andReturn(1);
$proxyClient->shouldReceive('purge');
$proxyClient->shouldReceive('isLazyObjectInitialized')->andReturn(true);

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$cacheInvalidator->invalidatePath('/foo');
$this->assertEquals(1, $cacheInvalidator->flush());
}
}

0 comments on commit 45af40c

Please sign in to comment.