From b465180041e3cb5905c46abc36b8daa809cad725 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Thu, 15 Oct 2026 14:55:41 +0200 Subject: [PATCH] feat(registry) #49 Add a class alias to autowire the AdapterRegistry The CleverAge\CacheProcessBundle\Registry\AdapterRegistry alias (private) targets the cleverage_cache_process.registry.adapter service, so that the registry can be autowired, e.g. in a custom cache task extending AbstractCacheTask declared by the default App\ resource of an application. The custom task example of the documentation no longer passes the registry explicitly. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 1 + config/services/registry.yaml | 4 +++ docs/index.md | 6 +++-- .../CleverAgeCacheProcessExtensionTest.php | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6038b98..463f323 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Latest ------ ### Changes +* [#49](https://github.com/cleverage/cache-process-bundle/issues/49) Add a `CleverAge\CacheProcessBundle\Registry\AdapterRegistry` alias of the `cleverage_cache_process.registry.adapter` service: the registry can be autowired, e.g. in a custom cache task extending `AbstractCacheTask`. Update documentation, add tests. * [#43](https://github.com/cleverage/cache-process-bundle/issues/43) Give a code to the tasks ([cleverage/process-bundle#91](https://github.com/cleverage/process-bundle/issues/91)): `cache_get` and `cache_set` (`task: cache_get`, with `cleverage/process-bundle` 5.4 or later, referencing them by `service` being deprecated there). The tasks have their class name as service id; fetching them directly from the service container is deprecated (`container.private`), they will be private in 3.0. The former ids `cleverage_cache_process.task.get` / `cleverage_cache_process.task.set` are deprecated aliases, removed in 3.0. Update documentation, add tests. * [#41](https://github.com/cleverage/cache-process-bundle/issues/41) CI: test the lowest versions allowed by `composer.json` (`--prefer-lowest`, PHP 8.2). * [#39](https://github.com/cleverage/cache-process-bundle/issues/39) Add a `.gitattributes`: the development files (`.docker/`, `.github/`, `tests/`, quality tools and PHPUnit configurations, `Makefile`) are no longer part of the archive installed by Composer (`dist`). diff --git a/config/services/registry.yaml b/config/services/registry.yaml index 2de9a05..84ea97c 100644 --- a/config/services/registry.yaml +++ b/config/services/registry.yaml @@ -2,3 +2,7 @@ services: cleverage_cache_process.registry.adapter: class: CleverAge\CacheProcessBundle\Registry\AdapterRegistry public: false + + CleverAge\CacheProcessBundle\Registry\AdapterRegistry: + alias: cleverage_cache_process.registry.adapter + public: false diff --git a/docs/index.md b/docs/index.md index 3462551..efe1f06 100644 --- a/docs/index.md +++ b/docs/index.md @@ -81,14 +81,16 @@ class DeleteTask extends AbstractCacheTask Its code is given by the `#[AsTask]` attribute (`cleverage/process-bundle` 5.4 or later), prefixed to avoid conflicts with the codes of the bundles. With autoconfiguration, the task is also non-shared and logs in the -`cleverage_process_task` channel: +`cleverage_process_task` channel. The registry is autowired by its class (since 2.3; before, pass +`'@cleverage_cache_process.registry.adapter'` as argument), so the default `App\:` resource of a Symfony application is +enough, or explicitly: ```yaml # config/services.yaml services: App\Task\DeleteTask: + autowire: true autoconfigure: true - arguments: ['@cleverage_cache_process.registry.adapter'] ``` ```yaml diff --git a/tests/DependencyInjection/CleverAgeCacheProcessExtensionTest.php b/tests/DependencyInjection/CleverAgeCacheProcessExtensionTest.php index 9f6e6cb..a9d93b2 100644 --- a/tests/DependencyInjection/CleverAgeCacheProcessExtensionTest.php +++ b/tests/DependencyInjection/CleverAgeCacheProcessExtensionTest.php @@ -19,11 +19,13 @@ use CleverAge\CacheProcessBundle\Task\SetTask; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; #[CoversClass(CleverAgeCacheProcessExtension::class)] +#[UsesClass(AdapterRegistry::class)] class CleverAgeCacheProcessExtensionTest extends TestCase { public function testRegistryIsRegistered(): void @@ -36,6 +38,24 @@ public function testRegistryIsRegistered(): void self::assertTrue($definition->isShared()); } + public function testRegistryCanBeAutowired(): void + { + $container = new ContainerBuilder(); + (new CleverAgeCacheProcessExtension())->load([], $container); + + $alias = $container->getAlias(AdapterRegistry::class); + self::assertSame('cleverage_cache_process.registry.adapter', (string) $alias); + self::assertFalse($alias->isPublic()); + + // e.g. a custom cache task extending AbstractCacheTask, autowired by the application + $container->register('app.service', AutowiredRegistryConsumer::class)->setAutowired(true)->setPublic(true); + $container->compile(true); + + $service = $container->get('app.service'); + self::assertInstanceOf(AutowiredRegistryConsumer::class, $service); + self::assertInstanceOf(AdapterRegistry::class, $service->registry); + } + /** * @return iterable */ @@ -76,3 +96,10 @@ public function testTaskIsRegistered(string $class, string $code, string $deprec ); } } + +final readonly class AutowiredRegistryConsumer +{ + public function __construct(public AdapterRegistry $registry) + { + } +}