PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / locate-binaries / src / LocateBinaries.php

LocateBinaries.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at vendor/rosell-dk/locate-binaries/src/LocateBinaries.php

164 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LocateBinaries;
4
5 use FileUtil\FileExists;
6 use ExecWithFallback\ExecWithFallback;
7
8 /**
9 * Locate path (or multiple paths) of a binary
10 *
11 * @package LocateBinaries
12 * @author Bjørn Rosell <[email protected]>
13 */
14 class LocateBinaries
15 {
16
17 /**
18 * Locate binaries by looking in common system paths.
19 *
20 * We try a small set of common system paths, such as "/usr/bin".
21 * On Windows, we only try C:\Windows\System32
22 * Note that you do not have to add ".exe" file extension on Windows, it is taken care of
23 *
24 * @param string $binary the binary to look for (ie "cwebp")
25 *
26 * @return array binaries found in common system locations
27 */
28 public static function locateInCommonSystemPaths($binary)
29 {
30 $binaries = [];
31
32 $commonSystemPaths = [];
33
34 if (stripos(PHP_OS, 'WIN') === 0) {
35 $commonSystemPaths = [
36 'C:\Windows\System32',
37 ];
38 $binary .= '.exe';
39 } else {
40 $commonSystemPaths = [
41 '/usr/bin',
42 '/usr/local/bin',
43 '/usr/gnu/bin',
44 '/usr/syno/bin',
45 '/bin',
46 ];
47 }
48
49 foreach ($commonSystemPaths as $dir) {
50 // PS: FileExists might throw if exec() or similar is unavailable. We let it.
51 // - this class assumes exec is available
52 if (FileExists::fileExistsTryHarder($dir . DIRECTORY_SEPARATOR . $binary)) {
53 $binaries[] = $dir . DIRECTORY_SEPARATOR . $binary;
54 }
55 }
56 return $binaries;
57 }
58
59 /**
60 * Locate installed binaries using ie "whereis -b cwebp" (for Linux, Mac, etc)
61 *
62 * @return array Array of paths locateed (possibly empty)
63 */
64 private static function locateBinariesUsingWhereIs($binary)
65 {
66 $isMac = (PHP_OS == 'Darwin');
67 $command = 'whereis ' . ($isMac ? '' : '-b ') . $binary . ' 2>&1';
68
69 ExecWithFallback::exec($command, $output, $returnCode);
70 //echo 'command:' . $command;
71 //echo 'output:' . print_r($output, true);
72
73 if (($returnCode == 0) && (isset($output[0]))) {
74 // On linux, result looks like this:
75 // "cwebp: /usr/bin/cwebp /usr/local/bin/cwebp"
76 // or, for empty: "cwebp:"
77
78 if ($output[0] == ($binary . ':')) {
79 return [];
80 }
81
82 // On mac, it is not prepended with name of binary.
83 // I don't know if mac returns one result per line or is space seperated
84 // As I don't know if some systems might return several lines,
85 // I assume that some do and convert to space-separation:
86 $result = implode(' ', $output);
87
88 // Next, lets remove the prepended binary (if exists)
89 $result = preg_replace('#\b' . $binary . ':\s?#', '', $result);
90
91 // And back to array
92 return explode(' ', $result);
93 }
94 return [];
95 }
96
97 /**
98 * locate installed binaries using "which -a cwebp"
99 *
100 * @param string $binary the binary to look for (ie "cwebp")
101 *
102 * @return array Array of paths locateed (possibly empty)
103 */
104 private static function locateBinariesUsingWhich($binary)
105 {
106 // As suggested by @cantoute here:
107 // https://wordpress.org/support/topic/sh-1-usr-local-bin-cwebp-not-found/
108 ExecWithFallback::exec('which -a ' . $binary . ' 2>&1', $output, $returnCode);
109 if ($returnCode == 0) {
110 return $output;
111 }
112 return [];
113 }
114
115 /**
116 * Locate binaries using where.exe (for Windows)
117 *
118 * @param string $binary the binary to look for (ie "cwebp")
119 *
120 * @return array binaries found
121 */
122 private static function locateBinariesUsingWhere($binary)
123 {
124 ExecWithFallback::exec('where.exe ' . $binary . ' 2>&1', $output, $returnCode);
125 if ($returnCode == 0) {
126 return $output;
127 }
128 return [];
129 }
130
131 /**
132 * Locate installed binaries
133 *
134 * For linuk, we use "which -a" or, if that fails "whereis -b"
135 * For Windows, we use "where.exe"
136 * These commands only searces within $PATH. So it only finds installed binaries (which is good,
137 * as it would be unsafe to deal with binaries found scattered around)
138 *
139 * @param string $binary the binary to look for (ie "cwebp")
140 *
141 * @return array binaries found
142 */
143 public static function locateInstalledBinaries($binary)
144 {
145 if (stripos(PHP_OS, 'WIN') === 0) {
146 $paths = self::locateBinariesUsingWhere($binary);
147 if (count($paths) > 0) {
148 return $paths;
149 }
150 } else {
151 $paths = self::locateBinariesUsingWhich($binary);
152 if (count($paths) > 0) {
153 return $paths;
154 }
155
156 $paths = self::locateBinariesUsingWhereIs($binary);
157 if (count($paths) > 0) {
158 return $paths;
159 }
160 }
161 return [];
162 }
163 }
164