CoolCMS API

CategoryNameTranslator extends AbstractTranslator
in package

Base class of route translators: keeps the list of route factories that use a translator.

Table of Contents

Constants

SLUG_MAP_NAME  : mixed = 'blog.category'
Name of the map in {@see SlugMapCache} (part of the Redis key).
SLUG_MAP_TAGS  : mixed = [\App\UI\Api\Blog\Models\Entities\Category::cla...
Writing these Api entities invalidates the map (root and translations; `BaseManager` bumps it).

Properties

$categoryManager  : CategoryManager
$idToNameCache  : array<int, array<int, string>>
$localeResolver  : LocaleResolver
$nameToIdCache  : array<int, array<string|int, int>>
$slugMapCache  : SlugMapCache
$warmedUp  : bool

Methods

__construct()  : mixed
addTargetFactory()  : void
Registers a route factory that uses this translator.
clearCache()  : void
Clears the cache.
getTargetFactories()  : array<string|int, string>
Returns the route factories that use this translator.
idToName()  : string|null
nameToId()  : int|null
warmupCache()  : void
Loads all categories with their translations in a single query.
warmupFromEntities()  : void
Fills the cache from already loaded entities (no DB query).\n *
fromCacheWithLanguageFallback()  : TValue|null
Returns a value from a translation cache, falling back to the other languages.
loadFromDb()  : void
DB path of the warmup (through the Api manager, bypassing the query cache).

Constants

SLUG_MAP_NAME

Name of the map in {@see SlugMapCache} (part of the Redis key).

private mixed SLUG_MAP_NAME = 'blog.category'

SLUG_MAP_TAGS

Writing these Api entities invalidates the map (root and translations; `BaseManager` bumps it).

private mixed SLUG_MAP_TAGS = [\App\UI\Api\Blog\Models\Entities\Category::class, \App\UI\Api\Blog\Models\Entities\CategoryText::class]

Properties

$idToNameCache

private array<int, array<int, string>> $idToNameCache = []

Cache id->name [languageId][categoryId] = name

$nameToIdCache

private array<int, array<string|int, int>> $nameToIdCache = []

Cache name->id [languageId][name] = categoryId

Methods

addTargetFactory()

Registers a route factory that uses this translator.

public addTargetFactory(string $factoryClass) : void

Called from the factory constructor as addTargetFactory($this::class).

Parameters
$factoryClass : string

fully qualified factory class name

getTargetFactories()

Returns the route factories that use this translator.

public getTargetFactories() : array<string|int, string>
Return values
array<string|int, string> —

factory class names without namespace

idToName()

public idToName(int $id, string $loc) : string|null
Parameters
$id : int
$loc : string
Return values
string|null

nameToId()

public nameToId(string $name, string $loc) : int|null
Parameters
$name : string
$loc : string
Return values
int|null

warmupCache()

Loads all categories with their translations in a single query.

public warmupCache([array<string|int, int>|null $categoryIds = null ]) : void
Parameters
$categoryIds : array<string|int, int>|null = null

when null, loads all categories; otherwise only the given ones

warmupFromEntities()

Fills the cache from already loaded entities (no DB query).\n *

public warmupFromEntities(array<string|int, mixed> $categories) : void
Parameters
$categories : array<string|int, mixed>

Category entities with loaded translations

fromCacheWithLanguageFallback()

Returns a value from a translation cache, falling back to the other languages.

protected fromCacheWithLanguageFallback(array<string|int, mixed> $cache, int $languageId, TKey $key) : TValue|null

IMPORTANT: without the fallback a translator returns null when the translation for the language is missing, and the route cannot build the URL at all ("No route for ..."). One untranslated category then breaks the whole foreign-language page, not just its own link.

Untranslated content shows in the main language instead of disappearing: the URL carries the main-language slug under /en/, until someone adds the translation.

Parameters
$cache : array<string|int, mixed>
$languageId : int
$key : TKey
Tags
template
Return values
TValue|null

loadFromDb()

DB path of the warmup (through the Api manager, bypassing the query cache).

private loadFromDb(array<int, int>|null $categoryIds) : void
Parameters
$categoryIds : array<int, int>|null

null loads all categories

On this page

Search results