Skip to content

Commit 8b4cf16

Browse files
committed
fix logging
1 parent 848da37 commit 8b4cf16

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

DeployApplication.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ class DeployApplication
3838
* Path to log file
3939
* @var string
4040
*/
41-
private $log_file;
41+
private $logFileName;
4242

4343
/** @var boolean */
4444
private $hasAccess;
45+
private $is_first = true;
4546

46-
public function __construct($securityKey, $project_root = '.', $log_file = 'git-deploy-log.txt')
47+
public function __construct($securityKey, $project_root = '.', $logFileName = 'git-deploy-log.txt')
4748
{
4849
$this->securityKey = $securityKey;
49-
$this->log_file = getcwd() . '/' . $log_file;
50+
$this->logFileName = getcwd() . '/' . $logFileName;
5051
chdir($project_root);
5152
putenv('HOME=' . getcwd());
5253
}
@@ -75,12 +76,12 @@ public function execute(array $customCommands = [])
7576
return;
7677
}
7778
if (empty($customCommands)) {
78-
$this->log($this->exec([
79+
$this->exec([
7980
'git branch',
8081
'git pull',
81-
]));
82+
]);
8283
} else {
83-
$this->log($this->exec($customCommands));
84+
$this->exec($customCommands);
8485
}
8586
}
8687

@@ -89,16 +90,15 @@ public function end()
8990
if ($this->checkSecurity()) {
9091
$this->log('SESSION END');
9192
}
92-
if (file_exists($this->log_file)) {
93+
if (file_exists($this->logFileName)) {
9394
echo '<h1>LOG </h1><pre>';
94-
echo file_get_contents($this->log_file);
95+
echo file_get_contents($this->logFileName);
9596
echo '</pre>';
9697
} else {
9798
echo 'log not found';
9899
}
99100
}
100101

101-
102102
/**
103103
* Returning an absolute path to "php". It is useful, cause just "php" not working!
104104
* @return string
@@ -123,10 +123,10 @@ private function checkSecurity(): bool
123123
$this->hasAccess = false;
124124
if (isset($_GET['key']) && !empty($_GET['key'])) {
125125
if ($this->securityKey === $_GET['key']) {
126-
$this->log('ACCESS IS OBTAINED');
126+
$this->logDated('ACCESS IS OBTAINED');
127127
$this->hasAccess = true;
128128
} else {
129-
$this->log(
129+
$this->logDated(
130130
'DENY << ://' . ($_SERVER['HTTP_HOST'] ?? 'unknown-domain') . ($_SERVER['REQUEST_URI'] ?? '')
131131
);
132132
}
@@ -135,38 +135,40 @@ private function checkSecurity(): bool
135135
return $this->hasAccess;
136136
}
137137

138-
private function exec(array $commands): string
138+
private function exec(array $commands)
139139
{
140-
$res = "Executing shell commands:\n";
141140
foreach ($commands as $key => $command) {
142141
$response = [];
143142
if ($key === 'php') {
144143
$command = $this->php() . ' ' . $command;
145144
}
145+
$this->logDated('$ ' . $command);
146146
exec($command . ' 2>&1', $response, $error_code);
147147
if ($error_code > 0 && empty($response)) {
148148
$response = array('Error: ' . $error_code);
149149
}
150150
$response = implode("\n", $response);
151-
$res .= "$ $command \n{$response}\n" . ($response ? "\n" : '');
151+
$this->log($response . "\n" . ($response ? "\n" : ''));
152152
}
153-
return $res;
154153
}
155154

156-
private function log($message)
155+
private function logDated(string $message)
157156
{
158-
if (empty($this->log_file)) {
159-
return;
157+
if ($this->is_first) {
158+
$this->log("\n==============================\n");
159+
$this->is_first = false;
160160
}
161161

162-
static $is_first = true;
163-
if ($is_first) {
164-
file_put_contents($this->log_file, "\n==============================\n", FILE_APPEND | LOCK_EX);
165-
$is_first = false;
162+
$this->log(date('Y.m.d H:i:s') . "\t" . $message . "\n");
163+
}
164+
165+
private function log(string $message)
166+
{
167+
if (empty($this->logFileName)) {
168+
return;
166169
}
167170

168-
$datetime = date('Y.m.d H:i:s');
169-
file_put_contents($this->log_file, $datetime . "\t" . $message . "\n", FILE_APPEND | LOCK_EX);
171+
file_put_contents($this->logFileName, $message, FILE_APPEND | LOCK_EX);
170172
flush();
171173
}
172174
}

tests/DeployApplicationTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testKeyValidDefault()
5656
$app->execute();
5757
$this->assertFileExists($this->fileName);
5858
$log = file_get_contents($this->fileName);
59-
$this->assertRegExp('/.+ACCESS IS OBTAINED.+Executing shell commands.+\$ git branch.+\$ git pull/si', $log);
59+
$this->assertRegExp('/.+ACCESS IS OBTAINED.+\$ git branch.+\$ git pull/si', $log);
6060
}
6161

6262
public function testKeyValidCustom()
@@ -74,7 +74,6 @@ public function testKeyValidCustom()
7474
$log = file_get_contents($this->fileName);
7575
$this->assertRegExp(
7676
'/.+ACCESS IS OBTAINED.+' .
77-
'Executing shell commands.+' .
7877
'\$ echo testing_echo.+' .
7978
'testing_echo.+' .
8079
'\$ .*php -v.+' .
@@ -92,17 +91,17 @@ public function testKeyValidPhp()
9291
$this->assertFileNotExists($this->fileName);
9392
$app->execute([
9493
'echo testing_echo',
95-
'php' => '-v'
94+
'php' => '-v && echo 123412'
9695
]);
9796
$this->assertFileExists($this->fileName);
9897
$log = file_get_contents($this->fileName);
9998
$this->assertRegExp(
10099
'/.+ACCESS IS OBTAINED.+' .
101-
'Executing shell commands.+' .
102100
'\$ echo testing_echo.+' .
103101
'testing_echo.+' .
104102
'\$ .*php -v.+' .
105-
'php ' . PHP_VERSION . '.+/si',
103+
'php ' . PHP_VERSION . '.+' .
104+
'123412.+/si',
106105
$log
107106
);
108107
}

0 commit comments

Comments
 (0)