|
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\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use CacheTool\Code; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractAdapter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $tempDir; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoggerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $logger; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Code $code |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function doRun(Code $code); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Code $code |
|
37
|
|
|
* @throws \RuntimeException |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function run(Code $code) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->logger->debug(sprintf('Executing code: %s', $code->getCode())); |
|
43
|
|
|
$data = $this->doRun($code); |
|
44
|
|
|
|
|
45
|
|
|
$result = @unserialize($data); |
|
46
|
|
|
|
|
47
|
|
|
if (!is_array($result)) { |
|
48
|
|
|
$this->logger->debug(sprintf('Return serialized: %s', $data)); |
|
49
|
|
|
throw new \RuntimeException('Could not unserialize data from adapter.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->logger->debug(sprintf('Return errors: %s', json_encode($result['errors']))); |
|
53
|
|
|
$this->logger->debug(sprintf('Return result: %s', json_encode($result['result']))); |
|
54
|
|
|
|
|
55
|
|
|
if (empty($result['errors'])) { |
|
56
|
|
|
return $result['result']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$errors = array_reduce($result['errors'], function ($carry, $error) { |
|
60
|
|
|
return $carry .= "{$error['str']} (error code: {$error['no']})\n"; |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
throw new \RuntimeException($errors); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getTempDir() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->tempDir; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $tempDir |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setTempDir($tempDir) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->tempDir = $tempDir; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param LoggerInterface $logger |
|
86
|
|
|
* @return AbstractAdapter |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setLogger(LoggerInterface $logger) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->logger = $logger; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function createTemporaryFile() |
|
99
|
|
|
{ |
|
100
|
|
|
$file = sprintf("%s/cachetool-%s.php", $this->tempDir, uniqid()); |
|
101
|
|
|
|
|
102
|
|
|
touch($file); |
|
103
|
|
|
chmod($file, 0666); |
|
104
|
|
|
|
|
105
|
|
|
return $file; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|