Skip to content

Commit 848da37

Browse files
committed
now command "php" might used as key. Example: ...->run(['php' => '-v'])
1 parent 94529d6 commit 848da37

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

DeployApplication.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
* Example for use it:
1414
*
1515
* ```php
16+
* // Run default commands
1617
* (new DeployApplication('security_key'))->run();
18+
*
19+
* // Run custom commands
20+
* (new DeployApplication('security_key'))->run([
21+
* 'echo Hello!', // equal: $ echo Hello!
22+
* 'php' => '-v' // equal: $ php -v ()
23+
* ]);
24+
* // for running "php" used key cause just "php" not working!
1725
* ```
1826
*
1927
* @package optimistex\deploy
@@ -95,7 +103,7 @@ public function end()
95103
* Returning an absolute path to "php". It is useful, cause just "php" not working!
96104
* @return string
97105
*/
98-
public function php()
106+
public function php(): string
99107
{
100108
if (defined('PHP_BINDIR') && PHP_BINDIR) {
101109
return PHP_BINDIR . '/php';
@@ -109,7 +117,7 @@ public function php()
109117
return 'php';
110118
}
111119

112-
private function checkSecurity()
120+
private function checkSecurity(): bool
113121
{
114122
if ($this->hasAccess === null) {
115123
$this->hasAccess = false;
@@ -127,11 +135,14 @@ private function checkSecurity()
127135
return $this->hasAccess;
128136
}
129137

130-
private function exec(array $commands)
138+
private function exec(array $commands): string
131139
{
132140
$res = "Executing shell commands:\n";
133-
foreach ($commands as $command) {
141+
foreach ($commands as $key => $command) {
134142
$response = [];
143+
if ($key === 'php') {
144+
$command = $this->php() . ' ' . $command;
145+
}
135146
exec($command . ' 2>&1', $response, $error_code);
136147
if ($error_code > 0 && empty($response)) {
137148
$response = array('Error: ' . $error_code);

tests/DeployApplicationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,28 @@ public function testKeyValidCustom()
8282
$log
8383
);
8484
}
85+
86+
public function testKeyValidPhp()
87+
{
88+
$_GET['key'] = '123';
89+
$_SERVER['HTTP_HOST'] = 'test.domain';
90+
$app = new DeployApplication('123', '.', $this->fileName);
91+
92+
$this->assertFileNotExists($this->fileName);
93+
$app->execute([
94+
'echo testing_echo',
95+
'php' => '-v'
96+
]);
97+
$this->assertFileExists($this->fileName);
98+
$log = file_get_contents($this->fileName);
99+
$this->assertRegExp(
100+
'/.+ACCESS IS OBTAINED.+' .
101+
'Executing shell commands.+' .
102+
'\$ echo testing_echo.+' .
103+
'testing_echo.+' .
104+
'\$ .*php -v.+' .
105+
'php ' . PHP_VERSION . '.+/si',
106+
$log
107+
);
108+
}
85109
}

0 commit comments

Comments
 (0)