Skip to content

Commit

Permalink
Merge pull request #609 from FriendsOfSymfony/fastly-cloudflare-lazy
Browse files Browse the repository at this point in the history
Make fastly and cloudflare clients lazy loaded to support symfony secrets
  • Loading branch information
dbu committed Dec 19, 2023
2 parents 6d6f849 + 45af40c commit 0deeee9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -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
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
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
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
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 0deeee9

Please sign in to comment.