Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

CacheIdentifierGenerator.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace Ibexa\Core\Persistence\Cache\Identifier;
10
11 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
12
16 final class CacheIdentifierGenerator implements CacheIdentifierGeneratorInterface
17 {
18 private const PLACEHOLDER = '-%s';
19
21 private $prefix;
22
24 private $tagPatterns;
25
27 private $keyPatterns;
28
29 public function __construct(string $prefix, array $tagPatterns, array $keyPatterns)
30 {
31 $this->prefix = $prefix;
32 $this->tagPatterns = $tagPatterns;
33 $this->keyPatterns = $keyPatterns;
34 }
35
39 public function generateTag(string $patternName, array $values = [], bool $withPrefix = false): string
40 {
41 if (!isset($this->tagPatterns[$patternName])) {
42 throw new InvalidArgumentException($patternName, sprintf(
43 'Undefined tag pattern "%s". Known pattern names are: "%s"',
44 $patternName,
45 implode('", "', array_keys($this->tagPatterns))
46 ));
47 }
48
49 return $this->generate($this->tagPatterns[$patternName], $values, $withPrefix);
50 }
51
55 public function generateKey(string $patternName, array $values = [], bool $withPrefix = false): string
56 {
57 if (!isset($this->keyPatterns[$patternName])) {
58 throw new InvalidArgumentException($patternName, sprintf(
59 'Undefined key pattern "%s". Known pattern names are: "%s"',
60 $patternName,
61 implode('", "', array_keys($this->keyPatterns))
62 ));
63 }
64
65 return $this->generate($this->keyPatterns[$patternName], $values, $withPrefix);
66 }
67
68 private function generate(string $pattern, array $values, bool $withPrefix = false): string
69 {
70 if (empty($values)) {
71 $cacheIdentifier = str_replace(self::PLACEHOLDER, '', $pattern);
72 } else {
73 $cacheIdentifier = vsprintf($pattern, $values);
74 }
75
76 if ($withPrefix) {
77 $cacheIdentifier = $this->prefix . $cacheIdentifier;
78 }
79
80 return $cacheIdentifier;
81 }
82 }
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator
Definition: CacheIdentifierGenerator.php:17
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\PLACEHOLDER
const PLACEHOLDER
Definition: CacheIdentifierGenerator.php:18
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\generate
generate(string $pattern, array $values, bool $withPrefix=false)
Definition: CacheIdentifierGenerator.php:68
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\generateKey
generateKey(string $patternName, array $values=[], bool $withPrefix=false)
Definition: CacheIdentifierGenerator.php:55
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\$prefix
$prefix
Definition: CacheIdentifierGenerator.php:21
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\$keyPatterns
$keyPatterns
Definition: CacheIdentifierGenerator.php:27
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\generateTag
generateTag(string $patternName, array $values=[], bool $withPrefix=false)
Definition: CacheIdentifierGenerator.php:39
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\__construct
__construct(string $prefix, array $tagPatterns, array $keyPatterns)
Definition: CacheIdentifierGenerator.php:29
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGenerator\$tagPatterns
$tagPatterns
Definition: CacheIdentifierGenerator.php:24
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
Ibexa\Core\Persistence\Cache\Identifier\CacheIdentifierGeneratorInterface
Definition: CacheIdentifierGeneratorInterface.php:15
Ibexa\Core\Persistence\Cache\Identifier
Definition: CacheIdentifierGenerator.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.