Skip to content

Commit

Permalink
Add a config setting to make wfd_meta-based caches expire immediately (
Browse files Browse the repository at this point in the history
…#28)

This adds a new configuration setting:

```yml
# config_test.yml
webfactory_wfd_meta:
    always_expire_wfd_meta_resources: true
```

With this setting, `ConfigCache` instances (Symfony Router, Translator, ... maybe?) that include `WfdMetaResource` instances will expire immediately.

This is helpful e. g. in functional tests, where you have database-backed routes, translations or similar: You can change the database values and no longer need to think about the `ConfigCaches` or poke `wfd_meta` to make the changes effective.
  • Loading branch information
mpdude authored Nov 30, 2022
1 parent dacfddb commit c7a2e2b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/Config/CacheBustingResourceChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Webfactory\Bundle\WfdMetaBundle\Config;

use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Config\ResourceCheckerInterface;

class CacheBustingResourceChecker implements ResourceCheckerInterface
{
public function supports(ResourceInterface $metadata): bool
{
return $metadata instanceof WfdMetaResource;
}

public function isFresh(ResourceInterface $resource, int $timestamp): bool
{
return false;
}
}
22 changes: 22 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Webfactory\Bundle\WfdMetaBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('webfactory_wfd_meta');

$treeBuilder->getRootNode()
->children()
->booleanNode('always_expire_wfd_meta_resources')
->defaultFalse()
->info('When set to "true", ConfigCache instances that depend on "\Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaResource" will be refreshed every time; useful during functional tests to reload routes etc.');

return $treeBuilder;
}
}
9 changes: 9 additions & 0 deletions src/DependencyInjection/WebfactoryWfdMetaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@ public function load(array $configs, ContainerBuilder $container): void

$yamlLoader = new YamlFileLoader($container, $fileLocator);
$yamlLoader->load('legacy_aliases.yml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

if ($config['always_expire_wfd_meta_resources']) {
$yamlLoader->load('cache_busting.yml');
} else {
$xmlLoader->load('config_cache_factory.xml');
}
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/cache_busting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:

_defaults:
public: false

Webfactory\Bundle\WfdMetaBundle\Config\CacheBustingResourceChecker:
tags: ['config_cache.resource_checker']
32 changes: 32 additions & 0 deletions src/Resources/config/config_cache_factory.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" ?>

<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<defaults autoconfigure="true" autowire="true" public="false" />

<service id="Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory" decorates="config_cache_factory">
<argument type="service" id="Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory.inner" />
<argument type="service" id="Webfactory\Bundle\WfdMetaBundle\MetaQueryFactory" />
<argument type="service">
<service class="Symfony\Component\Lock\LockFactory">
<argument type="service">
<service class="Symfony\Component\Lock\Store\FlockStore" />
</argument>
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore" />
</call>
<tag name="monolog.logger" channel="webfactory_wfd_meta" />
</service>
</argument>
</service>

<service public="true" id="webfactory_wfd_meta.config_cache_factory" alias="Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory">
<deprecated>The "%alias_id%" alias is deprecated</deprecated>
</service>

</services>
</container>
4 changes: 0 additions & 4 deletions src/Resources/config/legacy_aliases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,3 @@ services:
webfactory_wfd_meta.controller.template:
alias: 'Webfactory\Bundle\WfdMetaBundle\Controller\TemplateController'
deprecated: ~

webfactory_wfd_meta.config_cache_factory:
alias: 'Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory'
deprecated: ~
16 changes: 0 additions & 16 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,5 @@
<argument>%kernel.debug%</argument>
</service>

<service id="Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory" decorates="config_cache_factory">
<argument type="service" id="Webfactory\Bundle\WfdMetaBundle\Config\WfdMetaConfigCacheFactory.inner" />
<argument type="service" id="Webfactory\Bundle\WfdMetaBundle\MetaQueryFactory" />
<argument type="service">
<service class="Symfony\Component\Lock\LockFactory">
<argument type="service">
<service class="Symfony\Component\Lock\Store\FlockStore" />
</argument>
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore" />
</call>
<tag name="monolog.logger" channel="webfactory_wfd_meta" />
</service>
</argument>
</service>

</services>
</container>

2 comments on commit c7a2e2b

@relthyg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpdude Dieser Commit hat die Kompatibilität zu symfony/config in Version 4.4 kaputtgemacht, weil dort die Methode ResourceCheckerInterface::isFresh() noch keinen Type-Hint auf dem zweiten Argument timestamp hat: https://github.com/symfony/config/blob/ed42f8f9da528d2c6cae36fe1f380b0c1d8f0658/ResourceCheckerInterface.php#L44. Können/wollen/müssen wir das fixen?

@mpdude
Copy link
Member Author

@mpdude mpdude commented on c7a2e2b Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

41d743f hat es ja erstmal repariert, weil es Symfony >=5.1 fordert (5.0 wäre ausreichend). Also mit dem Type-Hint jetzt alles gut für Nutzer, die Symfoy 5.1 oder besser können.

Damit haben wir aber Nutzer mit Symfony < 5.0 mit einer kaputten Version zurückgelassen. Weil Du aber mit dem Hochsetzen der Dependencies auch ein Minor Release gemacht hast, habe ich noch genug Beinfreiheit für einen Fix mit einer Version 3.16.2.

Please # to comment.