1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegryd\OpcacheResetBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
use GuzzleHttp\Psr7\Response as HttpResponse; |
9
|
|
|
|
10
|
|
|
class OpcacheResetCommandHelper |
11
|
|
|
{ |
12
|
|
|
const FILE_NAME_MODEL = 'opcache-reset-%s.php'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var KernelInterface |
16
|
|
|
*/ |
17
|
|
|
protected $kernel; |
18
|
|
|
|
19
|
|
|
public function __construct(KernelInterface $kernel) |
20
|
|
|
{ |
21
|
|
|
$this->kernel = $kernel; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return generated file name. |
26
|
|
|
* |
27
|
|
|
* @param string $replacement |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getFileName($replacement) |
32
|
|
|
{ |
33
|
|
|
return sprintf(self::FILE_NAME_MODEL, $replacement); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return generated file path. |
38
|
|
|
* |
39
|
|
|
* @param string $dir |
40
|
|
|
* @param string $replacement |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getFilePath($dir, $replacement) |
45
|
|
|
{ |
46
|
|
|
return sprintf('%s/%s', $dir, $this->getFileName($replacement)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generate url with given host and token. |
51
|
|
|
* |
52
|
|
|
* @param string $host |
53
|
|
|
* @param string $token |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function generateUrl($host, $token) |
58
|
|
|
{ |
59
|
|
|
return sprintf('%s/%s', $host, $this->getFileName($token)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create file in given directory. |
64
|
|
|
* |
65
|
|
|
* @param string $dir |
66
|
|
|
* @param string $token |
67
|
|
|
* |
68
|
|
|
* @return OpcacheResetCommandHelper |
69
|
|
|
*/ |
70
|
|
|
public function createFile($dir, $token) |
71
|
|
|
{ |
72
|
|
|
if (!is_dir($dir)) { |
73
|
|
|
throw new \RuntimeException(sprintf('Web directory does not exists : %s', $dir)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$templatePath = $this->kernel->locateResource('@TimegrydOpcacheResetBundle/Resources/template/opcacheReset.php'); |
77
|
|
|
|
78
|
|
|
$fileSystem = new FileSystem(); |
79
|
|
|
|
80
|
|
|
$fileSystem->copy( |
81
|
|
|
$templatePath, |
|
|
|
|
82
|
|
|
$this->getFilePath($dir, $token) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove all opcache reset files. |
90
|
|
|
* |
91
|
|
|
* @param string $dir |
92
|
|
|
* |
93
|
|
|
* @return OpcacheResetCommandHelper |
94
|
|
|
*/ |
95
|
|
|
public function clean($dir) |
96
|
|
|
{ |
97
|
|
|
array_map('unlink', glob($this->getFilePath($dir, '*'))); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Handle given response and return message from it. |
104
|
|
|
* |
105
|
|
|
* @param HttpResponse $response |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function handleResponse(HttpResponse $response) |
110
|
|
|
{ |
111
|
|
|
if (Response::HTTP_OK !== $response->getStatusCode()) { |
112
|
|
|
throw new \RuntimeException('Unable to access url'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$result = json_decode($response->getBody()); |
116
|
|
|
|
117
|
|
|
if (null === $result) { |
118
|
|
|
throw new \RuntimeException('Unable to decode json response.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!property_exists($result, 'success')) { |
122
|
|
|
throw new \InvalidArgumentException('"success" property is missing in returned json.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!property_exists($result, 'message')) { |
126
|
|
|
throw new \InvalidArgumentException('"message" property is missing in returned json.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (false === $result->success) { |
130
|
|
|
throw new \RuntimeException($result->message); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $result->message; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.