Skip to content

Commit

Permalink
Make the always_expire_wfd_meta_resources config setting work regar…
Browse files Browse the repository at this point in the history
…dless of `kernel.debug` mode (#50)

The `always_expire_wfd_meta_resources` config setting can be used to
make `WfdMetaResource` instances always expire immediately. It is
supposed to be set in Symfony environments like `test` (e. g. in a
`config_test.yml` file or a `when@test` section).

However, until now, it works correctly only for `kernel.debug = true`. 

This is because it used a special `CacheBustingResourceChecker` that
would be used by the "inner" (regular) `ConfigCache` instance only. That
ResourceChecker would invalidate the cache when an instance of a
`WfdMetaResource` is found.

The `ConfigCache`, however, contains a shortcut to never validate the
cache in the first place when `kernel.debug` is disabled.

Thus, we need to move this behavior to the `WfdMetaConfigCache`, which
will run its check also in non-debug mode. There, we want to consider
the setting only in the case that there are actually `WfdMetaResource`s
associated with the cache in question.

Being able to disable `kernel.debug` mode for tests execution can
provide a significant speed-up in environments where you don't actually
modify code between test runs, i. e. a CI pipeline.
  • Loading branch information
mpdude authored Sep 3, 2024
1 parent cf26d07 commit ed0032c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
19 changes: 0 additions & 19 deletions src/Config/CacheBustingResourceChecker.php

This file was deleted.

9 changes: 8 additions & 1 deletion src/Config/WfdMetaConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ class WfdMetaConfigCache implements ConfigCacheInterface
/** @var ConfigCacheInterface */
private $innerCache;

public function __construct($file, ConfigCacheInterface $innerCache, MetaQueryFactory $metaQueryFactory)
private bool $forceExpiration;

public function __construct($file, ConfigCacheInterface $innerCache, MetaQueryFactory $metaQueryFactory, bool $forceExpiration = false)
{
$this->file = $file;
$this->innerCache = $innerCache;
$this->metaQueryFactory = $metaQueryFactory;
$this->forceExpiration = $forceExpiration;
}

public function getPath(): string
Expand Down Expand Up @@ -62,6 +65,10 @@ public function isWfdMetaFresh(): bool
return true;
}

if ($this->forceExpiration) {
return false;
}

$metaQuery = $this->metaQueryFactory->create();

foreach ($wfdMetaResources['resources'] as $wfdMetaResource) {
Expand Down
12 changes: 10 additions & 2 deletions src/Config/WfdMetaConfigCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ class WfdMetaConfigCacheFactory implements ConfigCacheFactoryInterface
*/
private $lockFactory;

public function __construct(ConfigCacheFactoryInterface $configCacheFactory, MetaQueryFactory $metaQueryFactory, LockFactory $lockFactory)
private bool $expireWfdMetaResources;

public function __construct(ConfigCacheFactoryInterface $configCacheFactory, MetaQueryFactory $metaQueryFactory, LockFactory $lockFactory, bool $expireWfdMetaResources = false)
{
$this->configCacheFactory = $configCacheFactory;
$this->metaQueryFactory = $metaQueryFactory;
$this->lockFactory = $lockFactory;
$this->expireWfdMetaResources = $expireWfdMetaResources;
}

public function expireWfdMetaResources(): void
{
$this->expireWfdMetaResources = true;
}

public function cache($file, $callback): ConfigCacheInterface
Expand Down Expand Up @@ -70,7 +78,7 @@ public function cache($file, $callback): ConfigCacheInterface

private function createCache($file, ConfigCacheInterface $innerCache): ConfigCacheInterface
{
return new WfdMetaConfigCache($file, $innerCache, $this->metaQueryFactory);
return new WfdMetaConfigCache($file, $innerCache, $this->metaQueryFactory, $this->expireWfdMetaResources);
}

private function fillCache($callback, ConfigCacheInterface $cache): void
Expand Down
4 changes: 1 addition & 3 deletions src/DependencyInjection/WebfactoryWfdMetaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

if ($config['always_expire_wfd_meta_resources']) {
$yamlLoader->load('cache_busting.yml');
}
$container->setParameter('webfactory_wfd_meta.expire_wfd_meta_resources', $config['always_expire_wfd_meta_resources']);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<tag name="monolog.logger" channel="webfactory_wfd_meta" />
</service>
</argument>
<argument>%webfactory_wfd_meta.expire_wfd_meta_resources%</argument>
</service>
</services>
</container>

0 comments on commit ed0032c

Please sign in to comment.