Skip to content

Commit f78cfb7

Browse files
committed
implemented executing nested commands like as:
$app->execute([ 'echo testing_echo', 'php' => '-v && echo 123456', 'echo testing_echo', ['php' => '-v && echo 654123'], 'echo testing_echo', ['php' => '-v && echo 147896'] ]);
1 parent 8b4cf16 commit f78cfb7

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

DeployApplication.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,20 @@ private function exec(array $commands)
139139
{
140140
foreach ($commands as $key => $command) {
141141
$response = [];
142-
if ($key === 'php') {
143-
$command = $this->php() . ' ' . $command;
144-
}
145-
$this->logDated('$ ' . $command);
146-
exec($command . ' 2>&1', $response, $error_code);
147-
if ($error_code > 0 && empty($response)) {
148-
$response = array('Error: ' . $error_code);
142+
if (is_array($command)) {
143+
$this->exec($command);
144+
} else {
145+
if ($key === 'php') {
146+
$command = $this->php() . ' ' . $command;
147+
}
148+
$this->logDated('$ ' . $command);
149+
exec($command . ' 2>&1', $response, $error_code);
150+
if ($error_code > 0 && empty($response)) {
151+
$response = array('Error: ' . $error_code);
152+
}
153+
$response = implode("\n", $response);
154+
$this->log($response . "\n" . ($response ? "\n" : ''));
149155
}
150-
$response = implode("\n", $response);
151-
$this->log($response . "\n" . ($response ? "\n" : ''));
152156
}
153157
}
154158

tests/DeployApplicationTest.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ public function testKeyValidDefault()
5656
$app->execute();
5757
$this->assertFileExists($this->fileName);
5858
$log = file_get_contents($this->fileName);
59-
$this->assertRegExp('/.+ACCESS IS OBTAINED.+\$ git branch.+\$ git pull/si', $log);
59+
$this->assertRegExp('/.+ACCESS IS OBTAINED.+' .
60+
date('Y.m.d H:i:.+') . '\$ git branch.+' .
61+
date('Y.m.d H:i:.+') . '\$ git pull/si',
62+
$log
63+
);
6064
}
6165

6266
public function testKeyValidCustom()
@@ -74,9 +78,9 @@ public function testKeyValidCustom()
7478
$log = file_get_contents($this->fileName);
7579
$this->assertRegExp(
7680
'/.+ACCESS IS OBTAINED.+' .
77-
'\$ echo testing_echo.+' .
81+
date('Y.m.d H:i:.+') . '\$ echo testing_echo.+' .
7882
'testing_echo.+' .
79-
'\$ .*php -v.+' .
83+
date('Y.m.d H:i:.+') . '\$ .*php -v.+' .
8084
'php ' . PHP_VERSION . '.+/si',
8185
$log
8286
);
@@ -97,12 +101,44 @@ public function testKeyValidPhp()
97101
$log = file_get_contents($this->fileName);
98102
$this->assertRegExp(
99103
'/.+ACCESS IS OBTAINED.+' .
100-
'\$ echo testing_echo.+' .
104+
date('Y.m.d H:i:.+') . '\$ echo testing_echo.+' .
101105
'testing_echo.+' .
102-
'\$ .*php -v.+' .
106+
date('Y.m.d H:i:.+') . '\$ .*php -v.+' .
103107
'php ' . PHP_VERSION . '.+' .
104108
'123412.+/si',
105109
$log
106110
);
107111
}
112+
113+
public function testKeyValidNestedCommands()
114+
{
115+
$_GET['key'] = '123';
116+
$_SERVER['HTTP_HOST'] = 'test.domain';
117+
$app = new DeployApplication('123', '.', $this->fileName);
118+
119+
$this->assertFileNotExists($this->fileName);
120+
$app->execute([
121+
'echo testing_echo',
122+
'php' => '-v && echo 123456',
123+
'echo testing_echo',
124+
['php' => '-v && echo 654123'],
125+
'echo testing_echo',
126+
['php' => '-v && echo 147896']
127+
]);
128+
$this->assertFileExists($this->fileName);
129+
$log = file_get_contents($this->fileName);
130+
$this->assertRegExp(
131+
'/.+ACCESS IS OBTAINED.+'
132+
133+
. date('Y.m.d H:i:.+') . '\$ echo testing_echo.+testing_echo.+'
134+
. date('Y.m.d H:i:.+') . '\$ .*php -v.+php ' . PHP_VERSION . '.+123456.+'
135+
136+
. date('Y.m.d H:i:.+') . '\$ echo testing_echo.+testing_echo.+'
137+
. date('Y.m.d H:i:.+') . '\$ .*php -v.+php ' . PHP_VERSION . '.+654123.+'
138+
139+
. date('Y.m.d H:i:.+') . '\$ echo testing_echo.+testing_echo.+'
140+
. date('Y.m.d H:i:.+') . '\$ .*php -v.+php ' . PHP_VERSION . '.+147896.+/si',
141+
$log
142+
);
143+
}
108144
}

0 commit comments

Comments
 (0)