1 | <?php |
||
19 | class FileCache implements CacheInterface |
||
20 | { |
||
21 | /** |
||
22 | * Default write directory |
||
23 | * |
||
24 | * @var string |
||
25 | * @access protected |
||
26 | */ |
||
27 | protected $directory; |
||
28 | |||
29 | /** |
||
30 | * Default write extension |
||
31 | * |
||
32 | * @var string |
||
33 | * @access protected |
||
34 | */ |
||
35 | protected $extension; |
||
36 | |||
37 | /** |
||
38 | * Internal constructor. |
||
39 | * |
||
40 | * @access public |
||
41 | * @param string $directory |
||
42 | * @param string $extension |
||
43 | */ |
||
44 | 22 | public function __construct($directory, $extension) |
|
45 | { |
||
46 | 22 | $this->directory = rtrim($directory, DIRECTORY_SEPARATOR); |
|
47 | 22 | $this->extension = $extension; |
|
48 | 22 | } |
|
49 | |||
50 | /** |
||
51 | * Write data to storage. |
||
52 | * |
||
53 | * @access public |
||
54 | * @param string $id |
||
55 | * @param string $data |
||
56 | * @return string |
||
57 | * @throws \JonnyW\PhantomJs\Exception\NotWritableException |
||
58 | */ |
||
59 | 45 | public function save($id, $data) |
|
60 | { |
||
61 | 45 | $file = $this->getFilename($id); |
|
62 | |||
63 | 44 | if (!$this->isWritable($file)) { |
|
64 | 2 | throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file)); |
|
65 | } |
||
66 | |||
67 | 42 | if ($this->writeData($file, $data) === false) { |
|
68 | |||
69 | $this->delete($file); |
||
70 | |||
71 | throw new NotWritableException(sprintf('Data could not be written to file on system. Please make sure that file is writeable: %s', $file)); |
||
72 | } |
||
73 | |||
74 | 42 | return $file; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Fetch data from file. |
||
79 | * |
||
80 | * @access public |
||
81 | * @param string $id |
||
82 | * @return mixed|void |
||
83 | * @throws \JonnyW\PhantomJs\Exception\NotExistsException |
||
84 | */ |
||
85 | 23 | public function fetch($id) |
|
86 | { |
||
87 | 23 | $file = $this->getFilename($id); |
|
88 | |||
89 | 23 | if (!$this->exists($id)) { |
|
90 | 1 | throw new NotExistsException(sprintf('Could not fetch data from file as file does not exist: %s', $file)); |
|
91 | } |
||
92 | |||
93 | 22 | return $this->readData($file); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Delete data from storage. |
||
98 | * |
||
99 | * @access public |
||
100 | * @param string $id |
||
101 | * @return void |
||
102 | */ |
||
103 | 38 | public function delete($id) |
|
104 | { |
||
105 | 38 | $files = glob($this->getFilename($id)); |
|
106 | |||
107 | 38 | if (count($files)) { |
|
108 | 38 | array_map('unlink', $files); |
|
109 | } |
||
110 | 38 | } |
|
111 | |||
112 | /** |
||
113 | * Data exists in storage. |
||
114 | * |
||
115 | * @access public |
||
116 | * @param string $id |
||
117 | * @return boolean |
||
118 | */ |
||
119 | 33 | public function exists($id) |
|
123 | |||
124 | /** |
||
125 | * Is data writeable. |
||
126 | * |
||
127 | * @access protected |
||
128 | * @param $file |
||
129 | * @return boolean |
||
130 | */ |
||
131 | 44 | protected function isWritable($file) |
|
135 | |||
136 | /** |
||
137 | * Write data to file. |
||
138 | * |
||
139 | * @access protected |
||
140 | * @param string $file |
||
141 | * @param string $data |
||
142 | * @return boolean |
||
143 | */ |
||
144 | 42 | protected function writeData($file, $data) |
|
148 | |||
149 | /** |
||
150 | * Read data from file. |
||
151 | * |
||
152 | * @access protected |
||
153 | * @param string $file |
||
154 | * @return mixed |
||
155 | */ |
||
156 | 22 | protected function readData($file) |
|
160 | |||
161 | /** |
||
162 | * Get filename |
||
163 | * |
||
164 | * @access protected |
||
165 | * @param string $id |
||
166 | * @return string |
||
167 | * @throws \JonnyW\PhantomJs\Exception\NotWritableException |
||
168 | */ |
||
169 | 48 | protected function getFileName($id) |
|
187 | } |
||
188 |