1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * This file is part of CacheTool. |
||||
5 | * |
||||
6 | * (c) Samuel Gordalina <[email protected]> |
||||
7 | * |
||||
8 | * For the full copyright and license information, please view the LICENSE |
||||
9 | * file that was distributed with this source code. |
||||
10 | */ |
||||
11 | |||||
12 | namespace CacheTool; |
||||
13 | |||||
14 | use CacheTool\Adapter\AbstractAdapter; |
||||
15 | use CacheTool\Proxy\ProxyInterface; |
||||
16 | use Psr\Log\LoggerInterface; |
||||
17 | use Monolog\Logger; |
||||
18 | |||||
19 | class CacheTool |
||||
20 | { |
||||
21 | /** |
||||
22 | * @var AbstractAdapter |
||||
23 | */ |
||||
24 | protected $adapter; |
||||
25 | |||||
26 | /** |
||||
27 | * @var array |
||||
28 | */ |
||||
29 | protected $proxies = []; |
||||
30 | |||||
31 | /** |
||||
32 | * @var array |
||||
33 | */ |
||||
34 | protected $functions = []; |
||||
35 | |||||
36 | /** |
||||
37 | * @var string |
||||
38 | */ |
||||
39 | protected $tempDir; |
||||
40 | |||||
41 | /** |
||||
42 | * @var LoggerInterface |
||||
43 | */ |
||||
44 | protected $logger; |
||||
45 | |||||
46 | /** |
||||
47 | * @param string $tempDir |
||||
48 | * @param LoggerInterface $logger |
||||
49 | */ |
||||
50 | 29 | public function __construct($tempDir = null, LoggerInterface $logger = null) |
|||
51 | { |
||||
52 | 29 | $this->logger = $logger ?: new Logger('cachetool'); |
|||
53 | 29 | $this->tempDir = $this->getWritableTempDir($tempDir); |
|||
54 | 29 | } |
|||
55 | |||||
56 | /** |
||||
57 | * @param AbstractAdapter $adapter |
||||
58 | * @param string $tempDir |
||||
59 | * @param LoggerInterface $logger |
||||
60 | * @return CacheTool |
||||
61 | */ |
||||
62 | 25 | public static function factory(AbstractAdapter $adapter = null, $tempDir = null, LoggerInterface $logger = null) |
|||
63 | { |
||||
64 | 25 | $cacheTool = new static($tempDir, $logger); |
|||
65 | 25 | $cacheTool->addProxy(new Proxy\ApcuProxy()); |
|||
66 | 25 | $cacheTool->addProxy(new Proxy\PhpProxy()); |
|||
67 | 25 | $cacheTool->addProxy(new Proxy\OpcacheProxy()); |
|||
68 | |||||
69 | 25 | if ($adapter instanceof AbstractAdapter) { |
|||
70 | 24 | $cacheTool->setAdapter($adapter); |
|||
71 | } |
||||
72 | |||||
73 | 25 | return $cacheTool; |
|||
74 | } |
||||
75 | |||||
76 | |||||
77 | /** |
||||
78 | * @param AbstractAdapter $adapter |
||||
79 | * @return CacheTool |
||||
80 | */ |
||||
81 | 24 | public function setAdapter(AbstractAdapter $adapter) |
|||
82 | { |
||||
83 | 24 | $this->logger->info(sprintf('Setting adapter: %s', get_class($adapter))); |
|||
84 | |||||
85 | 24 | $this->adapter = $adapter; |
|||
86 | 24 | $this->adapter->setLogger($this->logger); |
|||
87 | 24 | $this->adapter->setTempDir($this->tempDir); |
|||
88 | |||||
89 | 24 | return $this; |
|||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @return AbstractAdapter |
||||
94 | */ |
||||
95 | 3 | public function getAdapter() |
|||
96 | { |
||||
97 | 3 | return $this->adapter; |
|||
98 | } |
||||
99 | |||||
100 | public function getTempDir() |
||||
101 | { |
||||
102 | return $this->tempDir; |
||||
103 | } |
||||
104 | |||||
105 | /** |
||||
106 | * @param ProxyInterface $proxy |
||||
107 | * @return CacheTool |
||||
108 | */ |
||||
109 | 25 | public function addProxy(ProxyInterface $proxy) |
|||
110 | { |
||||
111 | 25 | $this->logger->info(sprintf('Adding Proxy: %s', get_class($proxy))); |
|||
112 | |||||
113 | 25 | $this->proxies[] = $proxy; |
|||
114 | |||||
115 | // reset functions (to be built when needed) |
||||
116 | 25 | $this->functions = []; |
|||
117 | |||||
118 | 25 | return $this; |
|||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * @return array |
||||
123 | */ |
||||
124 | 3 | public function getProxies() |
|||
125 | { |
||||
126 | 3 | return $this->proxies; |
|||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @param LoggerInterface $logger |
||||
131 | * @return CacheTool |
||||
132 | */ |
||||
133 | public function setLogger(LoggerInterface $logger) |
||||
134 | { |
||||
135 | $this->logger = $logger; |
||||
136 | |||||
137 | if ($this->adapter instanceof AbstractAdapter) { |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
138 | $this->adapter->setLogger($logger); |
||||
139 | } |
||||
140 | |||||
141 | return $this; |
||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * @return LoggerInterface |
||||
146 | */ |
||||
147 | 1 | public function getLogger() |
|||
148 | { |
||||
149 | 1 | return $this->logger; |
|||
150 | } |
||||
151 | |||||
152 | /** |
||||
153 | * Calls proxy functions |
||||
154 | * |
||||
155 | * @param string $name |
||||
156 | * @param array $arguments |
||||
157 | * @return mixed |
||||
158 | */ |
||||
159 | 16 | public function __call($name, $arguments) |
|||
160 | { |
||||
161 | 16 | $this->logger->notice(sprintf('Executing: %s(%s)', $name, implode(', ', array_map('json_encode', $arguments)))); |
|||
162 | |||||
163 | 16 | $function = $this->getFunction($name); |
|||
164 | 14 | if ($function) { |
|||
165 | 14 | return $function(...$arguments); |
|||
166 | } |
||||
167 | } |
||||
168 | |||||
169 | /** |
||||
170 | * Initializes functions and return callable |
||||
171 | * |
||||
172 | * @param string $name |
||||
173 | * @return callable |
||||
174 | */ |
||||
175 | 16 | protected function getFunction($name) |
|||
176 | { |
||||
177 | 16 | if (empty($this->functions)) { |
|||
178 | 16 | foreach ($this->proxies as $proxy) { |
|||
179 | 14 | $this->logger->info(sprintf('Loading Proxy: %s', get_class($proxy))); |
|||
180 | |||||
181 | // lazily set adapter |
||||
182 | 14 | $proxy->setAdapter($this->adapter); |
|||
183 | |||||
184 | 14 | foreach ($proxy->getFunctions() as $fn) { |
|||
185 | 14 | $this->logger->debug(sprintf('Loading Function: %s', $fn)); |
|||
186 | 14 | $this->functions[$fn] = [$proxy, $fn]; |
|||
187 | } |
||||
188 | } |
||||
189 | } |
||||
190 | |||||
191 | 16 | if (isset($this->functions[$name])) { |
|||
192 | 14 | return $this->functions[$name]; |
|||
193 | } |
||||
194 | |||||
195 | 2 | throw new \InvalidArgumentException("Function with name: {$name} is not provided by any Proxy."); |
|||
196 | } |
||||
197 | |||||
198 | /** |
||||
199 | * @param string $tempDir |
||||
200 | * @return string |
||||
201 | */ |
||||
202 | 29 | protected function getWritableTempDir($tempDir = null) { |
|||
203 | 29 | if (is_null($tempDir)) { |
|||
204 | 11 | $tempDirs = ['/dev/shm', '/var/run', sys_get_temp_dir()]; |
|||
205 | 11 | foreach ($tempDirs as $dir) { |
|||
206 | 11 | if (is_dir($dir) && is_writable($dir)) { |
|||
207 | 11 | $tempDir = $dir; |
|||
208 | break; |
||||
209 | } |
||||
210 | } |
||||
211 | } |
||||
212 | |||||
213 | 29 | if (!file_exists($tempDir)) { |
|||
0 ignored issues
–
show
It seems like
$tempDir can also be of type null ; however, parameter $filename of file_exists() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
214 | 3 | mkdir($tempDir, 0700, true); |
|||
0 ignored issues
–
show
It seems like
$tempDir can also be of type null ; however, parameter $directory of mkdir() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
215 | } |
||||
216 | |||||
217 | 29 | return $tempDir; |
|||
218 | } |
||||
219 | } |
||||
220 |