1
|
|
|
<?php /** MicroFileDriver */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Cache\Driver; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Base\IContainer; |
7
|
|
|
use Micro\File\FileHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FileDriver |
11
|
|
|
* |
12
|
|
|
* @author Oleg Lunegov <[email protected]> |
13
|
|
|
* @link https://github.com/linpax/microphp-framework |
14
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
15
|
|
|
* @license /LICENSE |
16
|
|
|
* @package Micro |
17
|
|
|
* @subpackage Cache\Driver |
18
|
|
|
* @version 1.0 |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class FileDriver extends BaseCacheDriver |
22
|
|
|
{ |
23
|
|
|
/** @var string $driver directory name */ |
24
|
|
|
protected $driver; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* @access pubic |
31
|
|
|
* |
32
|
|
|
* @param IContainer $container |
33
|
|
|
* @param array $config config array |
34
|
|
|
* |
35
|
|
|
* @result void |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function __construct(IContainer $container, array $config = []) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($container, $config); |
41
|
|
|
|
42
|
|
|
$path = !empty($config['path']) ? $config['path'] : sys_get_temp_dir().'/cache'; |
43
|
|
|
|
44
|
|
View Code Duplication |
if (!@mkdir($path, 0600) && !is_dir($path)) { |
|
|
|
|
45
|
|
|
throw new Exception('Can`not create/check access to directory: '.$path); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->driver = $path; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function check() |
55
|
|
|
{ |
56
|
|
|
return is_writable($this->driver) ? true : false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function delete($name) |
63
|
|
|
{ |
64
|
|
|
unlink($this->driver.'/'.$name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function clean() |
71
|
|
|
{ |
72
|
|
|
FileHelper::removeDir($this->driver); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function info() |
79
|
|
|
{ |
80
|
|
|
return count(scandir($this->driver)) - 2; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function getMeta($id) |
87
|
|
|
{ |
88
|
|
|
return filesize($this->driver.'/'.$id); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function increment($name, $offset = 1) |
95
|
|
|
{ |
96
|
|
|
$this->set($name, (int)$this->get($name) + $offset); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function set($name, $value) |
103
|
|
|
{ |
104
|
|
|
return file_put_contents($this->driver.'/'.$name, $value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function get($name) |
111
|
|
|
{ |
112
|
|
|
return file_get_contents($this->driver.'/'.$name); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function decrement($name, $offset = 1) |
119
|
|
|
{ |
120
|
|
|
$this->set($name, (int)$this->get($name) - $offset); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.