|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Amasty\ImportExportCore\Utils; |
| 6 | + |
| 7 | +use Magento\Framework\App\DeploymentConfig; |
| 8 | +use Magento\Framework\Shell; |
| 9 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 10 | + |
| 11 | +class CliPhpResolver |
| 12 | +{ |
| 13 | + private const PHP_EXECUTABLE_PATH = 'php_executable_path'; |
| 14 | + |
| 15 | + private const VERSION_CHECK_REGEXP = '/PHP [\d\.]+ \(cli\)/'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var DeploymentConfig |
| 19 | + */ |
| 20 | + private $deploymentConfig; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var PhpExecutableFinder |
| 24 | + */ |
| 25 | + private $executableFinder; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Shell |
| 29 | + */ |
| 30 | + private $shell; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + private $executablePath; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + DeploymentConfig $deploymentConfig, |
| 39 | + PhpExecutableFinder $executableFinder, |
| 40 | + Shell $shell |
| 41 | + ) { |
| 42 | + $this->deploymentConfig = $deploymentConfig; |
| 43 | + $this->executableFinder = $executableFinder; |
| 44 | + $this->shell = $shell; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Return Cli PHP executable path. |
| 49 | + * Assumed that this executable will be executed through `exec` function |
| 50 | + * |
| 51 | + * @return string |
| 52 | + */ |
| 53 | + public function getExecutablePath(): string |
| 54 | + { |
| 55 | + if (!$this->executablePath) { |
| 56 | + $this->executablePath = $this->resolvePhpExecutable(); |
| 57 | + } |
| 58 | + |
| 59 | + return $this->executablePath; |
| 60 | + } |
| 61 | + |
| 62 | + private function resolvePhpExecutable() |
| 63 | + { |
| 64 | + $pathCandidates = [ |
| 65 | + $this->deploymentConfig->get(self::PHP_EXECUTABLE_PATH), |
| 66 | + $this->executableFinder->find() |
| 67 | + ]; |
| 68 | + |
| 69 | + foreach ($pathCandidates as $path) { |
| 70 | + if ($path && $this->isExecutable($path)) { |
| 71 | + return $path; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return 'php'; |
| 76 | + } |
| 77 | + |
| 78 | + private function isExecutable($path): bool |
| 79 | + { |
| 80 | + $disabledFunctions = $this->getDisabledPhpFunctions(); |
| 81 | + if (in_array('exec', $disabledFunctions)) { |
| 82 | + throw new \RuntimeException( |
| 83 | + (string)__( |
| 84 | + 'The PHP function exec is disabled.' |
| 85 | + . ' Please contact your system administrator or your hosting provider.' |
| 86 | + ) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + $versionResult = (string)$this->shell->execute($path . ' %s', ['--version']); |
| 92 | + } catch (\Exception $e) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + return (bool)preg_match(self::VERSION_CHECK_REGEXP, $versionResult); |
| 97 | + } |
| 98 | + |
| 99 | + private function getDisabledPhpFunctions(): array |
| 100 | + { |
| 101 | + return explode(',', str_replace(' ', ',', ini_get('disable_functions'))); |
| 102 | + } |
| 103 | +} |
0 commit comments