1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bram Gerritsen [email protected] |
4
|
|
|
* @author Freek Gruntjes [email protected] |
5
|
|
|
* @copyright (c) Bram Gerritsen 2013 |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace StrokerCache\Storage\Adapter; |
10
|
|
|
|
11
|
|
|
use SplFileInfo; |
12
|
|
|
use Zend\Cache\Storage\Adapter\AbstractAdapter; |
13
|
|
|
|
14
|
|
|
class File extends AbstractAdapter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
*/ |
19
|
|
|
public function setOptions($options) |
20
|
|
|
{ |
21
|
|
|
if (!$options instanceof FileOptions && is_array($options)) { |
22
|
|
|
$options = new FileOptions($options); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return parent::setOptions($options); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $url |
30
|
|
|
* @return SplFileInfo |
31
|
|
|
*/ |
32
|
|
|
protected function getFileForUrl($url) |
33
|
|
|
{ |
34
|
|
|
$urlParts = parse_url($url); |
35
|
|
|
|
36
|
|
|
/** @var FileOptions $options */ |
37
|
|
|
$options = $this->getOptions(); |
38
|
|
|
|
39
|
|
|
$path = $options->getBaseDirectory() . DIRECTORY_SEPARATOR . |
40
|
|
|
$urlParts['scheme'] . DIRECTORY_SEPARATOR . |
41
|
|
|
$urlParts['host'] . DIRECTORY_SEPARATOR; |
42
|
|
|
|
43
|
|
|
if (isset($urlParts['path'])) { |
44
|
|
|
$path .= ltrim($urlParts['path'], '/'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (substr($path, -1) == '/') { |
48
|
|
|
$path .= 'index'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (isset($urlParts['query'])) { |
52
|
|
|
$path .= '-' . $urlParts['query']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new SplFileInfo($path . '.html'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) |
62
|
|
|
{ |
63
|
|
|
$file = $this->getFileForUrl($normalizedKey); |
64
|
|
|
if (!$file->isReadable()) { |
65
|
|
|
$success = false; |
66
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
$success = true; |
70
|
|
|
|
71
|
|
|
return file_get_contents($file->getPathname()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
protected function internalSetItem(& $normalizedKey, & $value) |
78
|
|
|
{ |
79
|
|
|
$file = $this->getFileForUrl($normalizedKey); |
80
|
|
|
$dirname = dirname($file->getPathname()); |
81
|
|
|
if (!is_dir($dirname)) { |
82
|
|
|
mkdir($dirname, 0777, true); |
83
|
|
|
} |
84
|
|
|
file_put_contents($file, $value); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
protected function internalRemoveItem(& $normalizedKey) |
93
|
|
|
{ |
94
|
|
|
$file = $this->getFileForUrl($normalizedKey); |
95
|
|
|
if (!$file->isWritable()) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
unlink($file->getPathname()); |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|