From 4ab8d49d9caa8f08cf35a40b345e23313cf3590d Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 17 Nov 2024 00:19:56 +0900 Subject: [PATCH] Refactor cache initialization and update composer config. Extract cache adapter initialization into a separate method for clarity and reusability in `src/Injector.php`. Update `composer.json` to allow `bamarni/composer-bin-plugin` for improved plugin management during builds. --- composer.json | 5 +++++ src/Injector.php | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 56919a0d..bff794cc 100644 --- a/composer.json +++ b/composer.json @@ -81,5 +81,10 @@ "phpmd": ["./vendor/bin/phpmd --exclude src/Annotation src text ./phpmd.xml"], "build": ["@cs", "@sa", "@pcov", "@metrics"], "compile": "./bin/bear.compile FakeVendor\\\\HelloWorld prod-app ./tests/Fake/fake-app" + }, + "config": { + "allow-plugins": { + "bamarni/composer-bin-plugin": true + } } } diff --git a/src/Injector.php b/src/Injector.php index a9d3cb5c..5044dc57 100644 --- a/src/Injector.php +++ b/src/Injector.php @@ -52,7 +52,7 @@ public static function getInstance(string $appName, string $context, string $app } $meta = new Meta($appName, $context, $appDir); - $cache = $cache ?? new ChainAdapter([new ApcuAdapter($injectorId), new FilesystemAdapter($injectorId, 0, $meta->tmpDir . '/injector')]); + $cache = self::getAdapter($cache, $injectorId, $meta); assert($cache instanceof AdapterInterface); /** @psalm-suppress all */ [$injector, $fileUpdate] = $cache->getItem($injectorId)->get(); @@ -99,4 +99,17 @@ private static function factory(Meta $meta, string $context, ?AbstractModule $ov return $injector; } + + public static function getAdapter(?CacheInterface $cache, string $injectorId, Meta $meta): CacheInterface + { + if ($cache instanceof CacheInterface) { + return $cache; + } + $filesystemAdapter = new FilesystemAdapter($injectorId, 0, $meta->tmpDir . '/injector'); + if (ApcuAdapter::isSupported()) { + return new ChainAdapter([new ApcuAdapter($injectorId), $filesystemAdapter]); + } + + return $filesystemAdapter; + } }