|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Attributes\Internal\Key; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A generator that hashes the key. It can be used when the cache driver |
|
16
|
|
|
* cannot accept string keys containing special characters (that is, not |
|
17
|
|
|
* containing letters and numbers). |
|
18
|
|
|
* |
|
19
|
|
|
* @internal HashKeyGenerator is an internal library class, please do not use it in your code. |
|
20
|
|
|
* @psalm-internal Spiral\Attributes |
|
21
|
|
|
*/ |
|
22
|
|
|
final class HashKeyGenerator implements KeyGeneratorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Hashing using md5 is the fastest 128-bit algorithm. |
|
26
|
|
|
* |
|
27
|
|
|
* However, in the case of small projects, it is recommended to use |
|
28
|
|
|
* crc32, which significantly increases performance, but cannot provide |
|
29
|
|
|
* a similarly variable result. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private const DEFAULT_HASH_ALGO = 'md5'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var KeyGeneratorInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $generator; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $algo; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param KeyGeneratorInterface|null $base |
|
47
|
|
|
* @param string $algo |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(KeyGeneratorInterface $base, string $algo = self::DEFAULT_HASH_ALGO) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->algo = $algo; |
|
52
|
|
|
$this->generator = $base; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function forClass(\ReflectionClass $class): string |
|
59
|
|
|
{ |
|
60
|
|
|
return \hash($this->algo, $this->generator->forClass($class)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritDoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function forProperty(\ReflectionProperty $prop): string |
|
67
|
|
|
{ |
|
68
|
|
|
return \hash($this->algo, $this->generator->forProperty($prop)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function forConstant(\ReflectionClassConstant $const): string |
|
75
|
|
|
{ |
|
76
|
|
|
return \hash($this->algo, $this->generator->forConstant($const)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritDoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function forFunction(\ReflectionFunctionAbstract $fn): string |
|
83
|
|
|
{ |
|
84
|
|
|
return \hash($this->algo, $this->generator->forFunction($fn)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function forParameter(\ReflectionParameter $param): string |
|
91
|
|
|
{ |
|
92
|
|
|
return \hash($this->algo, $this->generator->forParameter($param)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|