|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Server\Disk; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
|
7
|
|
|
Server\Disk, |
|
8
|
|
|
Server\Disk\Volume, |
|
|
|
|
|
|
9
|
|
|
Server\Disk\Volume\MountPoint, |
|
10
|
|
|
Server\Disk\Volume\Usage, |
|
11
|
|
|
Server\Memory\Bytes, |
|
12
|
|
|
Exception\DiskUsageNotAccessible |
|
13
|
|
|
}; |
|
14
|
|
|
use Innmind\Immutable\{ |
|
15
|
|
|
MapInterface, |
|
16
|
|
|
Str, |
|
17
|
|
|
StreamInterface, |
|
18
|
|
|
Sequence, |
|
19
|
|
|
Map |
|
20
|
|
|
}; |
|
21
|
|
|
use Symfony\Component\Process\Process; |
|
22
|
|
|
|
|
23
|
|
|
final class UnixDisk implements Disk |
|
24
|
|
|
{ |
|
25
|
|
|
private static $columns = [ |
|
26
|
|
|
'Size' => 'size', |
|
27
|
|
|
'Used' => 'used', |
|
28
|
|
|
'Avail' => 'available', |
|
29
|
|
|
'Use%' => 'usage', |
|
30
|
|
|
'Capacity' => 'usage', |
|
31
|
|
|
'Mounted' => 'mountPoint', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function volumes(): MapInterface |
|
38
|
|
|
{ |
|
39
|
2 |
|
return $this->parse( |
|
40
|
2 |
|
$this->run('df -lh') |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function get(MountPoint $point): Volume |
|
45
|
|
|
{ |
|
46
|
|
|
return $this |
|
47
|
1 |
|
->volumes() |
|
48
|
1 |
|
->get((string) $point); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
View Code Duplication |
private function run(string $command): Str |
|
52
|
|
|
{ |
|
53
|
2 |
|
$process = new Process($command); |
|
54
|
2 |
|
$process->run(); |
|
55
|
|
|
|
|
56
|
2 |
|
if (!$process->isSuccessful()) { |
|
57
|
|
|
throw new DiskUsageNotAccessible; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return new Str($process->getOutput()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return MapInterface<string, Volume> |
|
|
|
|
|
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function parse(Str $output): MapInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$lines = $output |
|
69
|
2 |
|
->trim() |
|
70
|
2 |
|
->split("\n"); |
|
71
|
|
|
$columns = $lines |
|
72
|
2 |
|
->first() |
|
73
|
2 |
|
->pregSplit('~ +~') |
|
74
|
2 |
|
->reduce( |
|
75
|
2 |
|
new Sequence, |
|
76
|
|
|
static function(Sequence $columns, Str $column): Sequence { |
|
77
|
2 |
|
$column = (string) $column; |
|
78
|
|
|
|
|
79
|
2 |
|
return $columns->add(self::$columns[$column] ?? $column); |
|
80
|
2 |
|
} |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
return $lines |
|
84
|
2 |
|
->drop(1) |
|
85
|
2 |
|
->reduce( |
|
86
|
2 |
|
new Sequence, |
|
87
|
|
|
static function(Sequence $lines, Str $line) use ($columns): Sequence { |
|
88
|
2 |
|
return $lines->add( |
|
89
|
2 |
|
$line->pregSplit('~ +~', $columns->size()) |
|
90
|
|
|
); |
|
91
|
2 |
|
} |
|
92
|
|
|
) |
|
93
|
|
|
->map(function(StreamInterface $parts) use ($columns): Volume { |
|
94
|
2 |
|
return new Volume( |
|
95
|
2 |
|
new MountPoint( |
|
96
|
2 |
|
(string) $parts->get($columns->indexOf('mountPoint')) |
|
97
|
|
|
), |
|
98
|
2 |
|
Bytes::fromString( |
|
99
|
2 |
|
(string) $parts->get($columns->indexOf('size')) |
|
100
|
|
|
), |
|
101
|
2 |
|
Bytes::fromString( |
|
102
|
2 |
|
(string) $parts->get($columns->indexOf('available')) |
|
103
|
|
|
), |
|
104
|
2 |
|
Bytes::fromString( |
|
105
|
2 |
|
(string) $parts->get($columns->indexOf('used')) |
|
106
|
|
|
), |
|
107
|
2 |
|
new Usage( |
|
108
|
2 |
|
(float) (string) $parts->get($columns->indexOf('usage')) |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
2 |
|
}) |
|
112
|
2 |
|
->reduce( |
|
113
|
2 |
|
new Map('string', Volume::class), |
|
114
|
2 |
|
static function(Map $volumes, Volume $volume): Map { |
|
115
|
2 |
|
return $volumes->put( |
|
116
|
2 |
|
(string) $volume->mountPoint(), |
|
|
|
|
|
|
117
|
2 |
|
$volume |
|
|
|
|
|
|
118
|
|
|
); |
|
119
|
2 |
|
} |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: