From 614f75d54039d14ec6a763f62782384671fea820 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 5 Jul 2024 15:43:02 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- nur_src/b/io/CacheManager.php | 26 ++++++++++++++++++++++++-- nur_tests/io/CacheManagerTest.php | 14 +++++++------- src/cl.php | 13 +++++++++++++ src/web/params/G.php | 4 ++-- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/nur_src/b/io/CacheManager.php b/nur_src/b/io/CacheManager.php index a0278ac..547a854 100644 --- a/nur_src/b/io/CacheManager.php +++ b/nur_src/b/io/CacheManager.php @@ -12,9 +12,11 @@ use nur\A; * une session de travail */ class CacheManager { - function __construct() { + function __construct(?array $includes=null, ?array $excludes=null) { $this->caches = []; $this->defaultCache = true; + $this->includes = $includes; + $this->excludes = $excludes; } /** @@ -29,13 +31,33 @@ class CacheManager { */ protected $defaultCache; + /** + * @var array|null groupes à toujours inclure dans le cache. pour les + * identifiants de ces groupe, {@link self::shouldCache()} retourne toujours + * true. + * + * $excludes est prioritaire par rapport à $includes + */ + protected $includes; + + /** + * @var array|null groupes à exclure de la mise en cache. la mise en cache est + * toujours calculée pour les identifiants de ces groupes. + */ + protected $excludes; + function setNoCache(bool $noCache=true, bool $reset=true): self { if ($reset) $this->caches = []; $this->defaultCache = !$noCache; return $this; } - function shouldCache(string $id, bool $reset=true): bool { + function shouldCache(string $id, ?string $groupId=null, bool $reset=true): bool { + $includes = $this->includes; + $shouldInclude = $includes !== null && in_array($groupId, $includes); + $excludes = $this->excludes; + $shouldExclude = $excludes !== null && in_array($groupId, $excludes); + if ($shouldInclude && !$shouldExclude) return true; $cache = A::get($this->caches, $id, $this->defaultCache); $this->caches[$id] = $reset?: $cache; return $cache; diff --git a/nur_tests/io/CacheManagerTest.php b/nur_tests/io/CacheManagerTest.php index bae2b02..adda3c9 100644 --- a/nur_tests/io/CacheManagerTest.php +++ b/nur_tests/io/CacheManagerTest.php @@ -13,20 +13,20 @@ class CacheManagerTest extends TestCase { self::assertTrue($cm->shouldCache("a")); self::assertTrue($cm->shouldCache("a")); - $cm->setNoCache(); + $cm->setNoCache(true); self::assertFalse($cm->shouldCache("b")); self::assertTrue($cm->shouldCache("b")); self::assertTrue($cm->shouldCache("b")); - $cm->setNoCache(); - self::assertFalse($cm->shouldCache("c", false)); - self::assertFalse($cm->shouldCache("c", false)); - self::assertFalse($cm->shouldCache("c", false)); + $cm->setNoCache(true); + self::assertFalse($cm->shouldCache("c", null, false)); + self::assertFalse($cm->shouldCache("c", null, false)); + self::assertFalse($cm->shouldCache("c", null, false)); - $cm->setNoCache(); + $cm->setNoCache(true); self::assertFalse($cm->shouldCache("d")); self::assertTrue($cm->shouldCache("d")); - $cm->setNoCache(); + $cm->setNoCache(true); self::assertFalse($cm->shouldCache("d")); self::assertTrue($cm->shouldCache("d")); } diff --git a/src/cl.php b/src/cl.php index 6de78c2..c3afe7b 100644 --- a/src/cl.php +++ b/src/cl.php @@ -315,6 +315,19 @@ class cl { ############################################################################# + static final function map(callable $callback, ?iterable $array): array { + $result = []; + if ($array !== null) { + $ctx = func::_prepare($callback); + foreach ($array as $key => $value) { + $result[$key] = func::_call($ctx, [$value, $key]); + } + } + return $result; + } + + ############################################################################# + /** * vérifier que le chemin $keys existe dans le tableau $array * diff --git a/src/web/params/G.php b/src/web/params/G.php index c819bb4..28a5403 100644 --- a/src/web/params/G.php +++ b/src/web/params/G.php @@ -26,7 +26,7 @@ class G { $_GET[$name] = $value; } - static final function xselect(?array $includes, ?array $excludes=null): array { - return cl::xselect($_GET, $includes, $excludes); + static final function xselect(?array $includes, ?array $excludes=null, ?array $merge=null): array { + return cl::merge(cl::xselect($_GET, $includes, $excludes), $merge); } }