|
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\Option; |
|
17
|
|
|
|
|
18
|
|
|
use TypeError; |
|
19
|
|
|
use Desarrolla2\Cache\File\BasicFilename; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Use filename generator |
|
23
|
|
|
*/ |
|
24
|
|
|
trait FilenameTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var callable |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $filename; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Filename format or callable. |
|
34
|
|
|
* The filename format will be applied using sprintf, replacing `%s` with the key. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|callable $filename |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
197 |
|
protected function setFilenameOption($filename): void |
|
40
|
|
|
{ |
|
41
|
197 |
|
if (is_string($filename)) { |
|
42
|
|
|
$filename = new BasicFilename($filename); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
197 |
|
if (!is_callable($filename)) { |
|
46
|
|
|
throw new TypeError("Filename should be a string or callable"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
197 |
|
$this->filename = $filename; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the filename callable |
|
54
|
|
|
* |
|
55
|
|
|
* @return callable |
|
56
|
|
|
*/ |
|
57
|
591 |
|
protected function getFilenameOption(): callable |
|
58
|
|
|
{ |
|
59
|
591 |
|
if (!isset($this->filename)) { |
|
60
|
394 |
|
$this->filename = new BasicFilename('%s.' . $this->getPacker()->getType()); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
591 |
|
return $this->filename; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create a filename based on the key |
|
68
|
|
|
* |
|
69
|
|
|
* @param string|mixed $key |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
756 |
|
protected function getFilename($key): string |
|
73
|
|
|
{ |
|
74
|
756 |
|
$id = $this->keyToId($key); |
|
|
|
|
|
|
75
|
468 |
|
$generator = $this->getFilenameOption(); |
|
76
|
|
|
|
|
77
|
468 |
|
return $this->cacheDir . DIRECTORY_SEPARATOR . $generator($id); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get a wildcard for all files |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getWildcard(): string |
|
86
|
|
|
{ |
|
87
|
|
|
$generator = $this->getFilenameOption(); |
|
88
|
|
|
|
|
89
|
|
|
return $this->cacheDir . DIRECTORY_SEPARATOR . $generator(''); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|