@@ -38,15 +38,16 @@ class DeployApplication
38
38
* Path to log file
39
39
* @var string
40
40
*/
41
- private $ log_file ;
41
+ private $ logFileName ;
42
42
43
43
/** @var boolean */
44
44
private $ hasAccess ;
45
+ private $ is_first = true ;
45
46
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 ' )
47
48
{
48
49
$ this ->securityKey = $ securityKey ;
49
- $ this ->log_file = getcwd () . '/ ' . $ log_file ;
50
+ $ this ->logFileName = getcwd () . '/ ' . $ logFileName ;
50
51
chdir ($ project_root );
51
52
putenv ('HOME= ' . getcwd ());
52
53
}
@@ -75,12 +76,12 @@ public function execute(array $customCommands = [])
75
76
return ;
76
77
}
77
78
if (empty ($ customCommands )) {
78
- $ this ->log ( $ this -> exec ([
79
+ $ this ->exec ([
79
80
'git branch ' ,
80
81
'git pull ' ,
81
- ])) ;
82
+ ]);
82
83
} else {
83
- $ this ->log ( $ this -> exec ($ customCommands) );
84
+ $ this ->exec ($ customCommands );
84
85
}
85
86
}
86
87
@@ -89,16 +90,15 @@ public function end()
89
90
if ($ this ->checkSecurity ()) {
90
91
$ this ->log ('SESSION END ' );
91
92
}
92
- if (file_exists ($ this ->log_file )) {
93
+ if (file_exists ($ this ->logFileName )) {
93
94
echo '<h1>LOG </h1><pre> ' ;
94
- echo file_get_contents ($ this ->log_file );
95
+ echo file_get_contents ($ this ->logFileName );
95
96
echo '</pre> ' ;
96
97
} else {
97
98
echo 'log not found ' ;
98
99
}
99
100
}
100
101
101
-
102
102
/**
103
103
* Returning an absolute path to "php". It is useful, cause just "php" not working!
104
104
* @return string
@@ -123,10 +123,10 @@ private function checkSecurity(): bool
123
123
$ this ->hasAccess = false ;
124
124
if (isset ($ _GET ['key ' ]) && !empty ($ _GET ['key ' ])) {
125
125
if ($ this ->securityKey === $ _GET ['key ' ]) {
126
- $ this ->log ('ACCESS IS OBTAINED ' );
126
+ $ this ->logDated ('ACCESS IS OBTAINED ' );
127
127
$ this ->hasAccess = true ;
128
128
} else {
129
- $ this ->log (
129
+ $ this ->logDated (
130
130
'DENY << :// ' . ($ _SERVER ['HTTP_HOST ' ] ?? 'unknown-domain ' ) . ($ _SERVER ['REQUEST_URI ' ] ?? '' )
131
131
);
132
132
}
@@ -135,38 +135,40 @@ private function checkSecurity(): bool
135
135
return $ this ->hasAccess ;
136
136
}
137
137
138
- private function exec (array $ commands ): string
138
+ private function exec (array $ commands )
139
139
{
140
- $ res = "Executing shell commands: \n" ;
141
140
foreach ($ commands as $ key => $ command ) {
142
141
$ response = [];
143
142
if ($ key === 'php ' ) {
144
143
$ command = $ this ->php () . ' ' . $ command ;
145
144
}
145
+ $ this ->logDated ('$ ' . $ command );
146
146
exec ($ command . ' 2>&1 ' , $ response , $ error_code );
147
147
if ($ error_code > 0 && empty ($ response )) {
148
148
$ response = array ('Error: ' . $ error_code );
149
149
}
150
150
$ response = implode ("\n" , $ response );
151
- $ res .= " $ $ command \n{ $ response }\n " . ($ response ? "\n" : '' );
151
+ $ this -> log ( $ response . "\n " . ($ response ? "\n" : '' ) );
152
152
}
153
- return $ res ;
154
153
}
155
154
156
- private function log ( $ message )
155
+ private function logDated ( string $ message )
157
156
{
158
- if (empty ($ this ->log_file )) {
159
- return ;
157
+ if ($ this ->is_first ) {
158
+ $ this ->log ("\n============================== \n" );
159
+ $ this ->is_first = false ;
160
160
}
161
161
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 ;
166
169
}
167
170
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 );
170
172
flush ();
171
173
}
172
174
}
0 commit comments