Skip to content

Commit

Permalink
feat: Search for Node executable additionally in paths of PATH enviro…
Browse files Browse the repository at this point in the history
…nment variable
  • Loading branch information
DevDavido authored Dec 24, 2023
2 parents 43ca08c + 55e5fff commit f705aa6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 3.1.0
- Added: Search for Node executable additionally in paths of `PATH` environment variable
- Improved: Provide directory search path in error message if executable cannot be found

## 3.0.0
- Release stable version for Matomo 5
- Fixed: CORS template issue
Expand Down
4 changes: 4 additions & 0 deletions Exceptions/DependencyMissingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class DependencyMissingException extends RuntimeException
public function __construct($dependency = '', $extraDirs = [])
{
$message = $dependency . " dependency not found.\n";
if (count($extraDirs) > 0) {
$message .= " Searched in the following directories for the dependency: ";
$message .= implode(", ", $extraDirs) . ".\n";
}
if (ini_get('open_basedir')) {
$searchDirs = array_filter(array_merge(explode(PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs));
$message .= " Please disable PHP open_basedir or set your PHP open_basedir option to: \n";
Expand Down
66 changes: 44 additions & 22 deletions ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

use Piwik\Container\StaticContainer;
use Piwik\Log\Logger;
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyMissingException;
use Symfony\Component\Process\ExecutableFinder as BaseExecutableFinder;

Expand All @@ -28,6 +30,9 @@ public static function search($name)
[__DIR__ . DIRECTORY_SEPARATOR . 'node_modules' . DIRECTORY_SEPARATOR . '.bin'],
explode(PATH_SEPARATOR, self::getDefaultPath())
);
StaticContainer::get(Logger::class)->debug('Searching for executable in directories.',
['executable' => $name, 'directories' => $extraDirs]);

$executablePath = (new parent())->find($name, false, $extraDirs);
if (!$executablePath) {
throw new DependencyMissingException($name, $extraDirs);
Expand All @@ -42,28 +47,45 @@ public static function search($name)
* @return string
*/
public static function getDefaultPath() {
return ExecutableFinder::isRunningOnWindows() ?
implode(";", [
'%SystemRoot%\system32',
'%SystemRoot%',
'%SystemRoot%\System32\Wbem'
]) :
implode(":", [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/opt/plesk/node/24/bin',
'/opt/plesk/node/22/bin',
'/opt/plesk/node/20/bin',
'/opt/plesk/node/18/bin',
'/opt/plesk/node/16/bin',
'/opt/plesk/node/14/bin',
'/opt/plesk/node/12/bin',
'/opt/plesk/node/10/bin'
]);
$searchPaths = ExecutableFinder::isRunningOnWindows() ? [
'%SystemRoot%\system32',
'%SystemRoot%',
'%SystemRoot%\System32\Wbem'
] : [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/opt/plesk/node/24/bin',
'/opt/plesk/node/22/bin',
'/opt/plesk/node/20/bin',
'/opt/plesk/node/18/bin',
'/opt/plesk/node/16/bin',
'/opt/plesk/node/14/bin',
'/opt/plesk/node/12/bin',
'/opt/plesk/node/10/bin'
];
$additionalSearchPaths = ExecutableFinder::getPathsFromEnvironmentVariablePath();
$finalSearchPaths = array_unique(array_merge($searchPaths, $additionalSearchPaths));

return implode(PATH_SEPARATOR, $finalSearchPaths);
}

/**
* Return paths as array if `PATH` environment variable is set,
* empty array otherwise.
*
* @return array
*/
private static function getPathsFromEnvironmentVariablePath() {
$envPath = getenv('PATH');
if (!is_string($envPath)) {
return [];
}

return explode(PATH_SEPARATOR, $envPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "PerformanceAudit",
"description": "Daily performance audits of all your sites in Matomo.",
"version": "3.0.0",
"version": "3.1.0",
"theme": false,
"require": {
"php": ">=7.2.5",
Expand Down

0 comments on commit f705aa6

Please sign in to comment.