These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Checkers; |
||
4 | |||
5 | use DomainException; |
||
6 | use Carbon\CarbonInterval; |
||
7 | use PragmaRX\Health\Support\Result; |
||
8 | use PragmaRX\Health\Support\Traits\Database; |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | View Code Duplication | class ServerUptime extends Base |
|
11 | { |
||
12 | use Database; |
||
13 | |||
14 | /** |
||
15 | * Check resource. |
||
16 | * |
||
17 | * @return Result |
||
18 | */ |
||
19 | public function check() |
||
20 | { |
||
21 | $this->loadDatabase(); |
||
22 | |||
23 | $current = $this->getCurrentUptime(); |
||
24 | |||
25 | $this->persist($current); |
||
26 | |||
27 | $rebooted = |
||
28 | ($cs = $this->uptimeInSeconds($current)) < |
||
29 | ($ss = $this->uptimeInSeconds($this->database)); |
||
30 | |||
31 | return $this->makeResult( |
||
32 | !$rebooted, |
||
33 | $this->makeMessage($current, $this->database) |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Execute command to get an uptime string. |
||
39 | * |
||
40 | * @return mixed|string |
||
41 | * @throws Exception |
||
42 | */ |
||
43 | private function executeUptimeCommand() |
||
44 | { |
||
45 | $error = exec($this->target->command, $system_string, $output); |
||
0 ignored issues
–
show
The property
command does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
46 | |||
47 | if ($output !== 0) { |
||
48 | throw new DomainException((string) $error); |
||
49 | } |
||
50 | |||
51 | return (!is_array($system_string) || empty($system_string)) |
||
52 | ? '' |
||
53 | : $system_string[0]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get current uptime. |
||
58 | * |
||
59 | * @return array |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | protected function getCurrentUptime() |
||
63 | { |
||
64 | return $this->parseUptimeString($this->executeUptimeCommand()); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Normalize uptime matches. |
||
69 | * |
||
70 | * @param $matches |
||
71 | * @return \Illuminate\Support\Collection |
||
72 | */ |
||
73 | protected function normalizeMatches($matches) |
||
74 | { |
||
75 | return collect($matches) |
||
76 | ->filter(function ($item, $key) { |
||
77 | return !is_numeric($key); |
||
78 | }) |
||
79 | ->map(function ($item, $key) { |
||
80 | $return = $item[0]; |
||
81 | |||
82 | if (starts_with($key, 'load')) { |
||
83 | $return = floatval($return); |
||
84 | } elseif (is_numeric($return)) { |
||
85 | $return = (int) $return; |
||
86 | } elseif (empty($return)) { |
||
87 | $return = null; |
||
88 | } |
||
89 | |||
90 | return $return; |
||
91 | }) |
||
92 | ->toArray(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Parse the uptime string. |
||
97 | * |
||
98 | * @param $system_string |
||
99 | * @return array |
||
100 | */ |
||
101 | protected function parseUptimeString($system_string) |
||
102 | { |
||
103 | $matches = []; |
||
104 | |||
105 | preg_match( |
||
106 | $this->target->regex, |
||
0 ignored issues
–
show
The property
regex does not seem to exist in PragmaRX\Health\Support\Target .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
107 | $system_string, |
||
108 | $matches, |
||
109 | PREG_OFFSET_CAPTURE |
||
110 | ); |
||
111 | |||
112 | $matches = $this->normalizeMatches($matches); |
||
113 | |||
114 | $matches['uptime_string'] = $system_string; |
||
115 | |||
116 | return $matches; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Convert uptime to seconds. |
||
121 | * |
||
122 | * @param $date |
||
123 | * @return int |
||
124 | */ |
||
125 | protected function uptimeInSeconds($date) |
||
126 | { |
||
127 | return ( |
||
128 | (isset($date['up_days']) ? $date['up_days'] * 24 * 60 : 0) + |
||
129 | (isset($date['up_hours']) ? $date['up_hours'] * 60 : 0) + |
||
130 | (isset($date['up_minutes']) ? $date['up_minutes'] : 0) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Make uptime message. |
||
136 | * |
||
137 | * @param $current |
||
138 | * @param $saved |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function makeMessage($current, $saved = null) |
||
142 | { |
||
143 | $current = $this->toUptimeString($current); |
||
144 | |||
145 | $saved = $this->toUptimeString($saved); |
||
146 | |||
147 | return sprintf($this->target->getErrorMessage(), $current, $saved); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Convert uptime to human readable string. |
||
152 | * |
||
153 | * @param $uptime |
||
154 | * @return string |
||
155 | */ |
||
156 | public function toUptimeString($uptime) |
||
157 | { |
||
158 | return (string) CarbonInterval::days( |
||
159 | isset($uptime['up_days']) ? $uptime['up_days'] : 0 |
||
160 | ) |
||
161 | ->hours(isset($uptime['up_hours']) ? $uptime['up_hours'] : 0) |
||
162 | ->minutes(isset($uptime['up_minutes']) ? $uptime['up_minutes'] : 0); |
||
163 | } |
||
164 | } |
||
165 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: