1010
1111/**
1212 * Main class for deploying
13- * Example for use it :
13+ * Usage example :
1414 *
1515 * ```php
1616 * // Run default commands
@@ -42,13 +42,24 @@ class DeployApplication
4242
4343 /** @var boolean */
4444 private $ hasAccess ;
45- private $ is_first = true ;
4645
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 ' )
4859 {
4960 $ this ->securityKey = $ securityKey ;
5061 $ this ->logFileName = getcwd () . '/ ' . $ logFileName ;
51- chdir ($ project_root );
62+ chdir ($ workPath );
5263 putenv ('HOME= ' . getcwd ());
5364 }
5465
@@ -63,13 +74,20 @@ public function run(array $customCommands = [])
6374 $ this ->end ();
6475 }
6576
77+ /**
78+ * Begin log file
79+ */
6680 public function begin ()
6781 {
6882 if ($ this ->checkSecurity ()) {
6983 $ this ->logDated ('SESSION START ' );
7084 }
7185 }
7286
87+ /**
88+ * Executing command like a command lines
89+ * @param array $customCommands you can execute custom commands
90+ */
7391 public function execute (array $ customCommands = [])
7492 {
7593 if (!$ this ->checkSecurity ()) {
@@ -82,17 +100,23 @@ public function execute(array $customCommands = [])
82100 }
83101 }
84102
103+ /**
104+ * Finish logging and output all content from the log file
105+ */
85106 public function end ()
86107 {
87108 if ($ this ->checkSecurity ()) {
88109 $ this ->logDated ('SESSION END ' );
89110 }
90- if (file_exists ($ this ->logFileName )) {
111+
112+ if ($ this ->logError ) {
113+ echo 'Write log failed ' ;
114+ } else if (file_exists ($ this ->logFileName )) {
91115 echo '<h1>LOG </h1><pre> ' ;
92116 echo file_get_contents ($ this ->logFileName );
93117 echo '</pre> ' ;
94118 } else {
95- echo 'log not found ' ;
119+ echo 'A log file not found ' ;
96120 }
97121 }
98122
@@ -155,9 +179,9 @@ private function exec(array $commands)
155179
156180 private function logDated (string $ message )
157181 {
158- if ($ this ->is_first ) {
182+ if ($ this ->isFirstLogging ) {
159183 $ this ->log ("\n============================== \n" );
160- $ this ->is_first = false ;
184+ $ this ->isFirstLogging = false ;
161185 }
162186
163187 $ this ->log (date ('Y.m.d H:i:s ' ) . "\t" . $ message . "\n" );
@@ -168,8 +192,10 @@ private function log(string $message)
168192 if (empty ($ this ->logFileName )) {
169193 return ;
170194 }
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+ }
174200 }
175201}
0 commit comments