1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package OperatingSystem/OsType/ValueBuilders |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://code.ganbarodigital.com/php-text-tools |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\OperatingSystem\OsType\ValueBuilders; |
45
|
|
|
|
46
|
|
|
use GanbaroDigital\Filesystem\Checks\IsExecutableFile; |
47
|
|
|
use GanbaroDigital\OperatingSystem\OsType\Values\CentOS; |
48
|
|
|
use GanbaroDigital\OperatingSystem\OsType\Values\Debian; |
49
|
|
|
use GanbaroDigital\OperatingSystem\OsType\Values\LinuxMint; |
50
|
|
|
use GanbaroDigital\OperatingSystem\OsType\Values\Ubuntu; |
51
|
|
|
use GanbaroDigital\ProcessRunner\ProcessRunners\PopenProcessRunner; |
52
|
|
|
use GanbaroDigital\TextTools\Editors\TrimWhitespace; |
53
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingString; |
54
|
|
|
use GanbaroDigital\TextTools\Filters\FilterColumns; |
55
|
|
|
|
56
|
|
|
class BuildTypeFromLsbRelease |
57
|
|
|
{ |
58
|
|
|
public function __invoke($path = "/usr/bin/lsb_release") |
59
|
|
|
{ |
60
|
|
|
return self::usingBinary($path); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function usingDefaultPath() |
64
|
|
|
{ |
65
|
|
|
return self::usingBinary("/usr/bin/lsb_release"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function usingBinary($pathToBinary) |
69
|
|
|
{ |
70
|
|
|
$output = self::getOutputFromBinary($pathToBinary); |
71
|
|
|
if ($output === null) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
list($distroName, $distroVersion) = self::extractDistroDetails($output); |
76
|
|
|
if ($distroName === null || $distroVersion === null) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// do we have a match? |
81
|
|
|
if (!isset(self::$osTypes[$distroName])) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$osType = new self::$osTypes[$distroName]($distroVersion); |
86
|
|
|
return $osType; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private static function getOutputFromBinary($pathToBinary) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// make sure we have an executable binary |
92
|
|
|
if (!IsExecutableFile::check($pathToBinary)) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// get the info |
97
|
|
|
$result = PopenProcessRunner::run([$pathToBinary, '-a']); |
98
|
|
|
if ($result->getReturnCode() !== 0) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// at this point, return the output |
103
|
|
|
return $result->getOutput(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function extractDistroDetails($output) |
107
|
|
|
{ |
108
|
|
|
// what do we have? |
109
|
|
|
$lines = explode(PHP_EOL, $output); |
110
|
|
|
$distroName = self::extractField($lines, 'Distributor ID:'); |
111
|
|
|
$distroVersion = self::extractField($lines, 'Release:'); |
112
|
|
|
|
113
|
|
|
if ($distroName === null || $distroVersion === null) { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$distroVersion = FilterColumns::from($distroVersion, '0-1', '.'); |
118
|
|
|
|
119
|
|
|
return [$distroName, $distroVersion]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private static function extractField($lines, $fieldName) |
123
|
|
|
{ |
124
|
|
|
$matches = FilterForMatchingString::against($lines, $fieldName); |
125
|
|
|
if (empty($matches)) { |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
return TrimWhitespace::from(FilterColumns::from($matches[0], '1', ':')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private static $osTypes = [ |
132
|
|
|
'CentOS' => CentOS::class, |
133
|
|
|
'Debian' => Debian::class, |
134
|
|
|
'Ubuntu' => Ubuntu::class, |
135
|
|
|
]; |
136
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.