PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
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 / webp-convert / src / Helpers / BinaryDiscovery.php

BinaryDiscovery.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at vendor/rosell-dk/webp-convert/src/Helpers/BinaryDiscovery.php

110 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WebPConvert\Helpers;
4
5 use WebPConvert\Helpers\FileExists;
6 use ExecWithFallback\ExecWithFallback;
7
8 /**
9 * Discover multiple paths of a binary
10 *
11 * @package WebPConvert
12 * @author Bjørn Rosell <[email protected]>
13 * @since Class available since Release 2.3.0
14 */
15 class BinaryDiscovery
16 {
17
18 // Common system paths
19 private static $commonSystemPaths = [
20 '/usr/bin',
21 '/usr/local/bin',
22 '/usr/gnu/bin',
23 '/usr/syno/bin',
24 '/bin',
25 ];
26
27 /**
28 * Discover binaries by looking in common system paths.
29 *
30 * We try a small set of common system paths, such as "/usr/bin".
31 *
32 * @param string $binary the binary to look for (ie "cwebp")
33 *
34 * @return array binaries found in common system locations
35 */
36 public static function discoverInCommonSystemPaths($binary)
37 {
38 $binaries = [];
39 foreach (self::$commonSystemPaths as $dir) {
40 // PS: FileExists might throw if exec() or similar is unavailable. We let it.
41 // - this class assumes exec is available
42 if (FileExists::fileExistsTryHarder($dir . '/' . $binary)) {
43 $binaries[] = $dir . '/' . $binary;
44 }
45 }
46 return $binaries;
47 }
48
49 /**
50 * Discover installed binaries using ie "whereis -b cwebp"
51 *
52 * @return array Array of cwebp paths discovered (possibly empty)
53 */
54 private static function discoverBinariesUsingWhereIs($binary)
55 {
56 // This method was added due to #226.
57 ExecWithFallback::exec('whereis -b ' . $binary . ' 2>&1', $output, $returnCode);
58 if (($returnCode == 0) && (isset($output[0]))) {
59 $result = $output[0];
60 // Ie: "cwebp: /usr/bin/cwebp /usr/local/bin/cwebp"
61 if (preg_match('#^' . $binary . ':\s(.*)$#', $result, $matches)) {
62 return explode(' ', $matches[1]);
63 }
64 }
65 return [];
66 }
67
68 /**
69 * Discover installed binaries using "which -a cwebp"
70 *
71 * @param string $binary the binary to look for (ie "cwebp")
72 *
73 * @return array Array of paths discovered (possibly empty)
74 */
75 private static function discoverBinariesUsingWhich($binary)
76 {
77 // As suggested by @cantoute here:
78 // https://wordpress.org/support/topic/sh-1-usr-local-bin-cwebp-not-found/
79 ExecWithFallback::exec('which -a ' . $binary . ' 2>&1', $output, $returnCode);
80 if ($returnCode == 0) {
81 return $output;
82 }
83 return [];
84 }
85
86 /**
87 * Discover binaries using "which -a" or, if that fails "whereis -b"
88 *
89 * These commands only searces within $PATH. So it only finds installed binaries (which is good,
90 * as it would be unsafe to deal with binaries found scattered around)
91 *
92 * @param string $binary the binary to look for (ie "cwebp")
93 *
94 * @return array binaries found in common system locations
95 */
96 public static function discoverInstalledBinaries($binary)
97 {
98 $paths = self::discoverBinariesUsingWhich($binary);
99 if (count($paths) > 0) {
100 return $paths;
101 }
102
103 $paths = self::discoverBinariesUsingWhereIs($binary);
104 if (count($paths) > 0) {
105 return $paths;
106 }
107 return [];
108 }
109 }
110