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\OSX; |
48
|
|
|
use GanbaroDigital\ProcessRunner\ProcessRunners\PopenProcessRunner; |
49
|
|
|
use GanbaroDigital\TextTools\Editors\TrimWhitespace; |
50
|
|
|
use GanbaroDigital\TextTools\Filters\FilterForMatchingString; |
51
|
|
|
use GanbaroDigital\TextTools\Filters\FilterColumns; |
52
|
|
|
|
53
|
|
|
class BuildTypeFromSwVers implements BuildTypeFromFile |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* use the output of /usr/bin/sw_vers (if present) to determine which |
57
|
|
|
* operating system we are using |
58
|
|
|
* |
59
|
|
|
* @param string $pathToBinary |
60
|
|
|
* path to the binary to run |
61
|
|
|
* @return null|OsType |
|
|
|
|
62
|
|
|
* OsType if we know which operating system we are using |
63
|
|
|
* null otherwise |
64
|
|
|
*/ |
65
|
|
|
public function __invoke($pathToBinary = "/usr/bin/sw_vers") |
66
|
|
|
{ |
67
|
|
|
return self::usingPath($pathToBinary); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* use the output of /usr/bin/sw_vers (if present) to determine which |
72
|
|
|
* operating system we are using |
73
|
|
|
* |
74
|
|
|
* @return null|OsType |
|
|
|
|
75
|
|
|
* OsType if we know which operating system we are using |
76
|
|
|
* null otherwise |
77
|
|
|
*/ |
78
|
|
|
public static function usingDefaultPath() |
79
|
|
|
{ |
80
|
|
|
return self::usingPath("/usr/bin/sw_vers"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* use the output of /usr/bin/sw_vers (if present) to determine which |
85
|
|
|
* operating system we are using |
86
|
|
|
* |
87
|
|
|
* @param string $pathToBinary |
88
|
|
|
* path to the binary to run |
89
|
|
|
* @return null|OsType |
|
|
|
|
90
|
|
|
* OsType if we know which operating system we are using |
91
|
|
|
* null otherwise |
92
|
|
|
*/ |
93
|
|
|
public static function usingPath($pathToBinary) |
94
|
|
|
{ |
95
|
|
|
list($productName, $productVersion) = self::getOsDetails($pathToBinary); |
96
|
|
|
if ($productName === null || $productVersion === null) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// do we have a match? |
101
|
|
|
if (!isset(self::$osTypes[$productName])) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$osType = new self::$osTypes[$productName]($productVersion); |
106
|
|
|
return $osType; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* call /usr/bin/sw_vers to get details about this operating system |
111
|
|
|
* |
112
|
|
|
* @param string $pathToBinary |
113
|
|
|
* path to the binary to run |
114
|
|
|
* @return array |
|
|
|
|
115
|
|
|
* [0] is the operating system name |
116
|
|
|
* [1] is the operating system version |
117
|
|
|
*/ |
118
|
|
|
private static function getOsDetails($pathToBinary) |
119
|
|
|
{ |
120
|
|
|
$output = self::getOutputFromBinary($pathToBinary); |
121
|
|
|
if ($output === null) { |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return self::extractOsDetails($output); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* call /usr/bin/sw_vers and return the output |
130
|
|
|
* |
131
|
|
|
* @param string $pathToBinary |
132
|
|
|
* path to the binary to call |
133
|
|
|
* @return string |
|
|
|
|
134
|
|
|
* output from the binary |
135
|
|
|
*/ |
136
|
|
|
private static function getOutputFromBinary($pathToBinary) |
137
|
|
|
{ |
138
|
|
|
// make sure we have an executable binary |
139
|
|
|
if (!IsExecutableFile::check($pathToBinary)) { |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// get the info |
144
|
|
|
$result = PopenProcessRunner::run([$pathToBinary]); |
145
|
|
|
if ($result->getReturnCode() !== 0) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// at this point, return the output |
150
|
|
|
return $result->getOutput(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* parse the output of /usr/bin/sw_vers to get details about |
155
|
|
|
* this operating system |
156
|
|
|
* |
157
|
|
|
* @param string $output |
158
|
|
|
* output from /usr/bin/sw_vers |
159
|
|
|
* @return array |
160
|
|
|
* [0] is the operating system name |
161
|
|
|
* [1] is the operating system version |
162
|
|
|
*/ |
163
|
|
|
private static function extractOsDetails($output) |
164
|
|
|
{ |
165
|
|
|
// what do we have? |
166
|
|
|
$lines = explode(PHP_EOL, $output); |
167
|
|
|
$productName = self::extractField($lines, 'ProductName:'); |
168
|
|
|
$productVersion = self::extractField($lines, 'ProductVersion:'); |
169
|
|
|
|
170
|
|
|
return [$productName, $productVersion]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* extract a named field from the output of /usr/bin/sw_vers |
175
|
|
|
* |
176
|
|
|
* @param array $lines |
177
|
|
|
* the output of /usr/bin/sw_vers |
178
|
|
|
* @param string $fieldName |
179
|
|
|
* the field that we are looking for |
180
|
|
|
* @return string|null |
181
|
|
|
* the value of the field (if found) |
182
|
|
|
*/ |
183
|
|
|
private static function extractField($lines, $fieldName) |
184
|
|
|
{ |
185
|
|
|
$matches = FilterForMatchingString::against($lines, $fieldName); |
186
|
|
|
if (empty($matches)) { |
187
|
|
|
return null; |
188
|
|
|
} |
189
|
|
|
return TrimWhitespace::from(FilterColumns::from($matches[0], '1', ':')); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* map of operating system names to OsTypes |
194
|
|
|
* @var array |
195
|
|
|
*/ |
196
|
|
|
private static $osTypes = [ |
197
|
|
|
'Mac OS X' => OSX::class, |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.