|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cache package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Daniel González |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Daniel González <[email protected]> |
|
11
|
|
|
* @author Arnold Daniels <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
declare(strict_types=1); |
|
15
|
|
|
|
|
16
|
|
|
namespace Desarrolla2\Cache\File; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create a path for a key as prefix tree directory structure. |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://en.wikipedia.org/wiki/Trie |
|
22
|
|
|
*/ |
|
23
|
|
|
class TrieFilename |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $format; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $levels; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $hash; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* TrieFilename constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $format |
|
45
|
|
|
* @param int $levels The depth of the structure |
|
46
|
|
|
* @param bool $hash MD5 hash the key to get a better spread |
|
47
|
|
|
*/ |
|
48
|
197 |
|
public function __construct(string $format, int $levels = 1, bool $hash = false) |
|
49
|
|
|
{ |
|
50
|
197 |
|
$this->format = $format; |
|
51
|
197 |
|
$this->levels = $levels; |
|
52
|
197 |
|
$this->hash = $hash; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the format |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getFormat(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->format; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the depth of the structure |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getLevels(): int |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->levels; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Will the key be hashed to create the trie. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function isHashed(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->hash; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create the path for a key |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
197 |
|
public function __invoke(string $key): string |
|
93
|
|
|
{ |
|
94
|
197 |
|
if (empty($key)) { |
|
95
|
1 |
|
return $this->wildcardPath(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
197 |
|
$dirname = $this->hash ? base_convert(md5($key), 16, 36) : $key; |
|
99
|
197 |
|
$filename = sprintf($this->format, $key); |
|
100
|
|
|
|
|
101
|
197 |
|
$path = ''; |
|
102
|
|
|
|
|
103
|
197 |
|
for ($length = 1; $length <= $this->levels; $length++) { |
|
104
|
197 |
|
$path .= substr($dirname, 0, $length) . DIRECTORY_SEPARATOR; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
197 |
|
return $path . $filename; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get a path for all files (using glob) |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
1 |
|
protected function wildcardPath(): string |
|
116
|
|
|
{ |
|
117
|
1 |
|
$filename = sprintf($this->format, '*'); |
|
118
|
|
|
|
|
119
|
1 |
|
return str_repeat('*' . DIRECTORY_SEPARATOR, $this->levels) . $filename; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|