10
10
11
11
/**
12
12
* Main class for deploying
13
- * Example for use it :
13
+ * Usage example :
14
14
*
15
15
* ```php
16
16
* // Run default commands
@@ -42,13 +42,24 @@ class DeployApplication
42
42
43
43
/** @var boolean */
44
44
private $ hasAccess ;
45
- private $ is_first = true ;
46
45
47
- public function __construct ($ securityKey , $ project_root = '. ' , $ logFileName = 'git-deploy-log.txt ' )
46
+ /** @var boolean */
47
+ private $ isFirstLogging = true ;
48
+
49
+ /** @var boolean */
50
+ private $ logError = false ;
51
+
52
+ /**
53
+ * DeployApplication constructor.
54
+ * @param string $securityKey string key for protect application
55
+ * @param string $workPath set working path for executing all commands
56
+ * @param string $logFileName path to a log file
57
+ */
58
+ public function __construct (string $ securityKey , string $ workPath = '. ' , string $ logFileName = 'git-deploy-log.txt ' )
48
59
{
49
60
$ this ->securityKey = $ securityKey ;
50
61
$ this ->logFileName = getcwd () . '/ ' . $ logFileName ;
51
- chdir ($ project_root );
62
+ chdir ($ workPath );
52
63
putenv ('HOME= ' . getcwd ());
53
64
}
54
65
@@ -63,13 +74,20 @@ public function run(array $customCommands = [])
63
74
$ this ->end ();
64
75
}
65
76
77
+ /**
78
+ * Begin log file
79
+ */
66
80
public function begin ()
67
81
{
68
82
if ($ this ->checkSecurity ()) {
69
83
$ this ->logDated ('SESSION START ' );
70
84
}
71
85
}
72
86
87
+ /**
88
+ * Executing command like a command lines
89
+ * @param array $customCommands you can execute custom commands
90
+ */
73
91
public function execute (array $ customCommands = [])
74
92
{
75
93
if (!$ this ->checkSecurity ()) {
@@ -82,17 +100,23 @@ public function execute(array $customCommands = [])
82
100
}
83
101
}
84
102
103
+ /**
104
+ * Finish logging and output all content from the log file
105
+ */
85
106
public function end ()
86
107
{
87
108
if ($ this ->checkSecurity ()) {
88
109
$ this ->logDated ('SESSION END ' );
89
110
}
90
- if (file_exists ($ this ->logFileName )) {
111
+
112
+ if ($ this ->logError ) {
113
+ echo 'Write log failed ' ;
114
+ } else if (file_exists ($ this ->logFileName )) {
91
115
echo '<h1>LOG </h1><pre> ' ;
92
116
echo file_get_contents ($ this ->logFileName );
93
117
echo '</pre> ' ;
94
118
} else {
95
- echo 'log not found ' ;
119
+ echo 'A log file not found ' ;
96
120
}
97
121
}
98
122
@@ -155,9 +179,9 @@ private function exec(array $commands)
155
179
156
180
private function logDated (string $ message )
157
181
{
158
- if ($ this ->is_first ) {
182
+ if ($ this ->isFirstLogging ) {
159
183
$ this ->log ("\n============================== \n" );
160
- $ this ->is_first = false ;
184
+ $ this ->isFirstLogging = false ;
161
185
}
162
186
163
187
$ this ->log (date ('Y.m.d H:i:s ' ) . "\t" . $ message . "\n" );
@@ -168,8 +192,10 @@ private function log(string $message)
168
192
if (empty ($ this ->logFileName )) {
169
193
return ;
170
194
}
171
-
172
- file_put_contents ($ this ->logFileName , $ message , FILE_APPEND | LOCK_EX );
173
- flush ();
195
+ if (file_put_contents ($ this ->logFileName , $ message , FILE_APPEND | LOCK_EX ) === false ) {
196
+ $ this ->logError = true ;
197
+ } else {
198
+ flush ();
199
+ }
174
200
}
175
201
}
0 commit comments