Skip to content

Commit f705aa6

Browse files
authored
feat: Search for Node executable additionally in paths of PATH environment variable
2 parents 43ca08c + 55e5fff commit f705aa6

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## 3.1.0
3+
- Added: Search for Node executable additionally in paths of `PATH` environment variable
4+
- Improved: Provide directory search path in error message if executable cannot be found
5+
26
## 3.0.0
37
- Release stable version for Matomo 5
48
- Fixed: CORS template issue

Exceptions/DependencyMissingException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class DependencyMissingException extends RuntimeException
1717
public function __construct($dependency = '', $extraDirs = [])
1818
{
1919
$message = $dependency . " dependency not found.\n";
20+
if (count($extraDirs) > 0) {
21+
$message .= " Searched in the following directories for the dependency: ";
22+
$message .= implode(", ", $extraDirs) . ".\n";
23+
}
2024
if (ini_get('open_basedir')) {
2125
$searchDirs = array_filter(array_merge(explode(PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs));
2226
$message .= " Please disable PHP open_basedir or set your PHP open_basedir option to: \n";

ExecutableFinder.php

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';
66

7+
use Piwik\Container\StaticContainer;
8+
use Piwik\Log\Logger;
79
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyMissingException;
810
use Symfony\Component\Process\ExecutableFinder as BaseExecutableFinder;
911

@@ -28,6 +30,9 @@ public static function search($name)
2830
[__DIR__ . DIRECTORY_SEPARATOR . 'node_modules' . DIRECTORY_SEPARATOR . '.bin'],
2931
explode(PATH_SEPARATOR, self::getDefaultPath())
3032
);
33+
StaticContainer::get(Logger::class)->debug('Searching for executable in directories.',
34+
['executable' => $name, 'directories' => $extraDirs]);
35+
3136
$executablePath = (new parent())->find($name, false, $extraDirs);
3237
if (!$executablePath) {
3338
throw new DependencyMissingException($name, $extraDirs);
@@ -42,28 +47,45 @@ public static function search($name)
4247
* @return string
4348
*/
4449
public static function getDefaultPath() {
45-
return ExecutableFinder::isRunningOnWindows() ?
46-
implode(";", [
47-
'%SystemRoot%\system32',
48-
'%SystemRoot%',
49-
'%SystemRoot%\System32\Wbem'
50-
]) :
51-
implode(":", [
52-
'/usr/local/sbin',
53-
'/usr/local/bin',
54-
'/usr/sbin',
55-
'/usr/bin',
56-
'/sbin',
57-
'/bin',
58-
'/opt/plesk/node/24/bin',
59-
'/opt/plesk/node/22/bin',
60-
'/opt/plesk/node/20/bin',
61-
'/opt/plesk/node/18/bin',
62-
'/opt/plesk/node/16/bin',
63-
'/opt/plesk/node/14/bin',
64-
'/opt/plesk/node/12/bin',
65-
'/opt/plesk/node/10/bin'
66-
]);
50+
$searchPaths = ExecutableFinder::isRunningOnWindows() ? [
51+
'%SystemRoot%\system32',
52+
'%SystemRoot%',
53+
'%SystemRoot%\System32\Wbem'
54+
] : [
55+
'/usr/local/sbin',
56+
'/usr/local/bin',
57+
'/usr/sbin',
58+
'/usr/bin',
59+
'/sbin',
60+
'/bin',
61+
'/opt/plesk/node/24/bin',
62+
'/opt/plesk/node/22/bin',
63+
'/opt/plesk/node/20/bin',
64+
'/opt/plesk/node/18/bin',
65+
'/opt/plesk/node/16/bin',
66+
'/opt/plesk/node/14/bin',
67+
'/opt/plesk/node/12/bin',
68+
'/opt/plesk/node/10/bin'
69+
];
70+
$additionalSearchPaths = ExecutableFinder::getPathsFromEnvironmentVariablePath();
71+
$finalSearchPaths = array_unique(array_merge($searchPaths, $additionalSearchPaths));
72+
73+
return implode(PATH_SEPARATOR, $finalSearchPaths);
74+
}
75+
76+
/**
77+
* Return paths as array if `PATH` environment variable is set,
78+
* empty array otherwise.
79+
*
80+
* @return array
81+
*/
82+
private static function getPathsFromEnvironmentVariablePath() {
83+
$envPath = getenv('PATH');
84+
if (!is_string($envPath)) {
85+
return [];
86+
}
87+
88+
return explode(PATH_SEPARATOR, $envPath);
6789
}
6890

6991
/**

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "PerformanceAudit",
33
"description": "Daily performance audits of all your sites in Matomo.",
4-
"version": "3.0.0",
4+
"version": "3.1.0",
55
"theme": false,
66
"require": {
77
"php": ">=7.2.5",

0 commit comments

Comments
 (0)