Skip to content

Commit d78a0ac

Browse files
Allow php-path to be an executable with arguments
You could use an executable to determine the real executable path. For example '/usr/local/bin/find-php 7.4'. The validation method can now handle this kind of scenarios.
1 parent 90b9823 commit d78a0ac

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/Config/Factory.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,24 @@ private function validatePhpPath(Config $config): void
195195
if (empty($config->getPhpPath())) {
196196
return;
197197
}
198-
if (!file_exists($config->getPhpPath())) {
198+
$foundPHP = false;
199+
$pathToCheck = [$config->getPhpPath()];
200+
$parts = explode(' ', $config->getPhpPath());
201+
// if there are spaces in the php-path and they are not escaped
202+
// it looks like an executable is used to find the PHP binary
203+
// so at least check if the executable exists
204+
if (count($parts) > 1 && substr($parts[0], -1) !== '\\') {
205+
$pathToCheck[] = $parts[0];
206+
}
207+
208+
foreach ($pathToCheck as $path) {
209+
if (file_exists($path)) {
210+
$foundPHP = true;
211+
break;
212+
}
213+
}
214+
215+
if (!$foundPHP) {
199216
throw new RuntimeException('The configured php-path is wrong: ' . $config->getPhpPath());
200217
}
201218
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"config": {
3+
"php-path": "tests/files/bin/success foo"
4+
},
5+
"prepare-commit-msg": {
6+
"enabled": true,
7+
"actions": []
8+
},
9+
"commit-msg": {
10+
"enabled": true,
11+
"actions": []
12+
},
13+
"pre-commit": {
14+
"enabled": true,
15+
"actions": [
16+
{
17+
"action": "phpunit --configuration=build/phpunit-hook.xml",
18+
"options": [],
19+
"conditions": [
20+
{
21+
"exec": "\\CaptainHook\\App\\Hook\\Condition\\AnyFileChanged",
22+
"args": [
23+
["foo.php", "bar.php"]
24+
]
25+
}
26+
],
27+
"config": {
28+
"allow-failure": true
29+
}
30+
}
31+
]
32+
},
33+
"pre-push": {
34+
"enabled": false,
35+
"actions": []
36+
}
37+
}

tests/unit/Config/FactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ public function testCreateWithSettings(): void
113113
$this->assertTrue($config->getHookConfig('pre-commit')->getActions()[0]->isFailureAllowed());
114114
}
115115

116+
/**
117+
* Tests Factory::create
118+
*
119+
* @throws \Exception
120+
*/
121+
public function testCreateWithCrazyPHPPath(): void
122+
{
123+
$config = Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-strange-settings.json'));
124+
125+
$this->assertEquals("tests/files/bin/success foo", $config->getPhpPath());
126+
}
127+
116128
/**
117129
* Tests Factory::create
118130
*

0 commit comments

Comments
 (0)