|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / vendor / amasty / base / Model / |
Filename | /home/dev2.destoffenstraat.com/vendor/amasty/base/Model/CliPhpResolver.php |
Size | 2.95 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 16-Aug-2022 09:35 |
Last accessed | 22-Aug-2025 22:29 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package Magento 2 Base Package
*/
namespace Amasty\Base\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Shell;
use Symfony\Component\Process\PhpExecutableFinder;
class CliPhpResolver
{
private const ENV_PHP_EXECUTABLE_PATH = 'php_executable_path';
private const CONFIG_PHP_EXECUTABLE_PATH = 'amasty_base/system/cli_php_path';
private const VERSION_CHECK_REGEXP = '/PHP [\d\.]+ \(cli\)/';
/**
* @var DeploymentConfig
*/
private $deploymentConfig;
/**
* @var PhpExecutableFinder
*/
private $executableFinder;
/**
* @var Shell
*/
private $shell;
/**
* @var ScopeConfigInterface
*/
private $configProvider;
/**
* @var string
*/
private $executablePath;
public function __construct(
DeploymentConfig $deploymentConfig,
ScopeConfigInterface $configProvider,
PhpExecutableFinder $executableFinder,
Shell $shell
) {
$this->deploymentConfig = $deploymentConfig;
$this->executableFinder = $executableFinder;
$this->shell = $shell;
$this->configProvider = $configProvider;
}
/**
* Return Cli PHP executable path.
* Assumed that this executable will be executed through `exec` function
*
* @return string
*/
public function getExecutablePath(): string
{
if (!$this->executablePath) {
$this->executablePath = $this->resolvePhpExecutable();
}
return $this->executablePath;
}
private function resolvePhpExecutable()
{
$pathCandidates = [
$this->configProvider->getValue(self::CONFIG_PHP_EXECUTABLE_PATH),
$this->deploymentConfig->get(self::ENV_PHP_EXECUTABLE_PATH),
$this->executableFinder->find()
];
foreach ($pathCandidates as $path) {
if ($path && $this->isExecutable($path)) {
return $path;
}
}
return 'php';
}
private function isExecutable($path): bool
{
$disabledFunctions = $this->getDisabledPhpFunctions();
if (in_array('exec', $disabledFunctions)) {
throw new \RuntimeException(
(string)__(
'The PHP function exec is disabled.'
. ' Please contact your system administrator or your hosting provider.'
)
);
}
try {
$versionResult = (string)$this->shell->execute($path . ' %s', ['--version']);
} catch (\Exception $e) {
return false;
}
return (bool)preg_match(self::VERSION_CHECK_REGEXP, $versionResult);
}
private function getDisabledPhpFunctions(): array
{
return explode(',', str_replace(' ', ',', ini_get('disable_functions')));
}
}
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package Magento 2 Base Package
*/
namespace Amasty\Base\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Shell;
use Symfony\Component\Process\PhpExecutableFinder;
class CliPhpResolver
{
private const ENV_PHP_EXECUTABLE_PATH = 'php_executable_path';
private const CONFIG_PHP_EXECUTABLE_PATH = 'amasty_base/system/cli_php_path';
private const VERSION_CHECK_REGEXP = '/PHP [\d\.]+ \(cli\)/';
/**
* @var DeploymentConfig
*/
private $deploymentConfig;
/**
* @var PhpExecutableFinder
*/
private $executableFinder;
/**
* @var Shell
*/
private $shell;
/**
* @var ScopeConfigInterface
*/
private $configProvider;
/**
* @var string
*/
private $executablePath;
public function __construct(
DeploymentConfig $deploymentConfig,
ScopeConfigInterface $configProvider,
PhpExecutableFinder $executableFinder,
Shell $shell
) {
$this->deploymentConfig = $deploymentConfig;
$this->executableFinder = $executableFinder;
$this->shell = $shell;
$this->configProvider = $configProvider;
}
/**
* Return Cli PHP executable path.
* Assumed that this executable will be executed through `exec` function
*
* @return string
*/
public function getExecutablePath(): string
{
if (!$this->executablePath) {
$this->executablePath = $this->resolvePhpExecutable();
}
return $this->executablePath;
}
private function resolvePhpExecutable()
{
$pathCandidates = [
$this->configProvider->getValue(self::CONFIG_PHP_EXECUTABLE_PATH),
$this->deploymentConfig->get(self::ENV_PHP_EXECUTABLE_PATH),
$this->executableFinder->find()
];
foreach ($pathCandidates as $path) {
if ($path && $this->isExecutable($path)) {
return $path;
}
}
return 'php';
}
private function isExecutable($path): bool
{
$disabledFunctions = $this->getDisabledPhpFunctions();
if (in_array('exec', $disabledFunctions)) {
throw new \RuntimeException(
(string)__(
'The PHP function exec is disabled.'
. ' Please contact your system administrator or your hosting provider.'
)
);
}
try {
$versionResult = (string)$this->shell->execute($path . ' %s', ['--version']);
} catch (\Exception $e) {
return false;
}
return (bool)preg_match(self::VERSION_CHECK_REGEXP, $versionResult);
}
private function getDisabledPhpFunctions(): array
{
return explode(',', str_replace(' ', ',', ini_get('disable_functions')));
}
}