Skip to content

Commit c1d572b

Browse files
author
Eduard Muradov
committed
Release 1.1.2
1 parent f4d53f7 commit c1d572b

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

Utils/CliPhpResolver.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
"name": "amasty/module-import-export-core",
33
"description": "Import Export Core by Amasty",
44
"require": {
5-
"php": ">=7.3",
6-
"amasty/base": "*"
5+
"php": ">=7.3"
76
},
87
"type": "magento2-module",
9-
"version": "1.1.1",
8+
"version": "1.1.2",
109
"license": [
1110
"Commercial"
1211
],

i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"The file ""%1"" does not exist","The file ""%1"" does not exist"
22
"Invalid XML in file %1:\n%2","Invalid XML in file %1:\n%2"
33
.,.
4+
"The PHP function exec is disabled. Please contact your system administrator or your hosting provider.","The PHP function exec is disabled. Please contact your system administrator or your hosting provider."
45
"Error occurred during ""%field_name"" processing. %details","Error occurred during ""%field_name"" processing. %details"

0 commit comments

Comments
 (0)