drupal/cacheable_types

1.x-dev 2022-08-17 13:10 UTC

This package is auto-updated.

Last update: 2022-09-17 13:20:47 UTC


README

Immutable classes and builders that make doing it wrong harder.

CacheableBool

# Never forget cacheability.
CacheableBool::create($bool, $cacheability)
CacheableBool::createTrue()
CacheableBool::createFalse()
$cacheableBool->value()
assert($cacheableBool instanceof CacheableDependencyInterface)
CacheableBool::fromAccessResult($accessResult, $useNotForbidden = FALSE)
$cacheableBool->toAccessResult($useNotForbidden = FALSE)
CacheableBool::not()
CacheableBool::and(...$cacheableBoolItems)
CacheableBool::or(...$cacheableBoolItems)

ROADMAP

This is TODO and NOT implemented yet.

# Immutable cacheability with type-checked constructor.
Cacheability::create($cacheableDependency)
assert($cacheability instanceof Cacheability)
assert($cacheability instanceof CacheableDependencyInterface)
# Cacheability builder.
Cacheability::builder($cacheableDependency)
assert($cacheability instanceof CacheabilityBuilder)
# Can not set arbitrary cacheability, only add.
$cacheabilityBuilder->addCacheableDependency($cacheableDependency)
$cacheabilityBuilder->addCacheTags(...)
$cacheabilityBuilder->addCacheContexts(...)
$cacheabilityBuilder->addCacheMaxAge(...)
# Get the value only after freezing.
assert(!$cacheability instanceof CacheableDependencyInterface)
assert($cacheabilityBuilder->toImmutable() instanceof CacheableDependencyInterface)
# Immutable cacheable Integer.
# Do we need a builder? Currently, not.
# Do we need ->equals and ->greaterOrEqual and friends?
CacheableInt::create($int, $cacheability)
assert($cacheableInt instanceof CacheableDependencyInterface)
$cacheableInt->value()
# Immutable collection.
# To enforce integer keys use CacheableList.
# Not iterable to encourage functional approaches.
# Subclass this to typehint items.
# Use psalm generics, T instanceof CacheableValue
CacheableCollection::create($listCacheability, T ...$items)
assert($cacheableCollection instanceof CacheableDependencyInterface)
$cacheableCollection->value(): CacheableCollection
$cacheableCollection->hasItems(): CacheableBool
$cacheableCollection->map(fn(T $item): T, $cacheabilityToAdd): CacheableCollection
# No surprises, re-key must be explicit.
$cacheableCollection->mapAndRekey(fn(T $item, string &$key): T): CacheableCollection
$cacheableCollection->filter(fn(T $item): CacheableBool): CacheableCollection
# Leverage provided list cacheability.
$cacheableCollection->any(fn(T $item): CacheableBool): CacheableBool
$cacheableCollection->every(fn(T $item): CacheableBool): CacheableBool