2016-09-23 14:24:04 +00:00
< ? php
namespace Ad5001\Gitable ;
use pocketmine\Server ;
use pocketmine\Player ;
use Ad5001\Gitable\Main ;
2016-09-23 16:30:14 +00:00
class Windows extends GitClient {
2016-09-23 18:52:41 +00:00
2016-11-18 17:32:03 +00:00
/*
Exec ' s a git request with defined args
@ param $args string
*/
public function gitExec ( string $args ) : string {
2016-11-19 09:50:57 +00:00
chdir ( $this -> dir );
2016-11-18 17:32:03 +00:00
$process = proc_open (
2016-11-19 10:01:06 +00:00
" $this->executable " . $args ,
2016-11-18 17:32:03 +00:00
array (
0 => array ( " pipe " , " r " ), //S TDIN
1 => array ( " pipe " , " w " ), //S TDOUT
2 => array ( " pipe " , " w " ), //S TDERR
),
$pipes
);
2016-11-19 09:50:57 +00:00
chdir ( DEFAULT_GIT_DIR );
2016-11-18 17:32:03 +00:00
if ( $process !== false ) {
$stdout = stream_get_contents ( $pipes [ 1 ]);
$stderr = stream_get_contents ( $pipes [ 2 ]);
fclose ( $pipes [ 1 ]);
fclose ( $pipes [ 2 ]);
proc_close ( $process );
2016-11-19 09:50:57 +00:00
return $stdout . $stderr ;
2016-09-23 18:52:41 +00:00
} else {
2016-11-18 17:32:03 +00:00
$stdout = stream_get_contents ( $pipes [ 1 ]);
$stderr = stream_get_contents ( $pipes [ 2 ]);
fclose ( $pipes [ 1 ]);
fclose ( $pipes [ 2 ]);
2016-11-19 09:50:57 +00:00
return " Error while executing command git " . $args . " : $stderr " ;
2016-11-18 17:32:03 +00:00
}
}
/*
Changes current directory
@ param $path string
*/
public function cd ( string $path ) : string {
if ( is_dir ( $this -> dir . $path )) {
2016-11-19 09:50:57 +00:00
$this -> dir = $this -> dir . $path ;
$this -> dir = realpath ( $this -> dir );
2016-11-18 17:32:03 +00:00
if ( substr ( $this -> dir , strlen ( $this -> dir ) - 1 ) !== " / " ) {
$this -> dir .= " / " ;
}
return " §aPath set to $this->dir " ;
} elseif ( is_dir ( $path )) {
$this -> dir = $path ;
2016-11-19 09:50:57 +00:00
$this -> dir = realpath ( $this -> dir );
2016-11-18 17:32:03 +00:00
if ( substr ( $this -> dir , strlen ( $this -> dir ) - 1 ) !== " / " ) {
$this -> dir .= " / " ;
2016-11-19 09:50:57 +00:00
} return " §aPath set to $path " ;
2016-11-18 17:32:03 +00:00
} else {
return " §4Directory $path not found ! " ;
}
}
/*
Return all files and dirs from the current directory
*/
public function ls () : string {
2016-11-19 09:50:57 +00:00
chdir ( $this -> dir );
2016-11-18 17:32:03 +00:00
$whereIsCommand = ( PHP_OS == 'WINNT' ) ? 'dir' : 'ls' ;
$process = proc_open (
2016-11-19 09:50:57 +00:00
" $whereIsCommand " ,
2016-11-18 17:32:03 +00:00
array (
0 => array ( " pipe " , " r " ), //S TDIN
1 => array ( " pipe " , " w " ), //S TDOUT
2 => array ( " pipe " , " w " ), //S TDERR
),
2016-11-19 09:50:57 +00:00
$p
2016-11-18 17:32:03 +00:00
);
2016-11-19 09:50:57 +00:00
chdir ( DEFAULT_GIT_DIR );
2016-11-18 17:32:03 +00:00
if ( $process !== false ) {
2016-11-19 09:50:57 +00:00
$stdout = stream_get_contents ( $p [ 1 ]);
$stderr = stream_get_contents ( $p [ 2 ]);
fclose ( $p [ 1 ]);
fclose ( $p [ 2 ]);
2016-11-18 17:32:03 +00:00
proc_close ( $process );
}
2016-11-19 09:50:57 +00:00
return $stdout ;
2016-11-18 17:32:03 +00:00
}
/*
Checks if the executable exists .
*/
public function initcheck () {
$this -> executable = $this -> main -> getConfig () -> get ( " executable_path " );
$process = proc_open (
2016-11-19 10:01:06 +00:00
" $this->executable --version " ,
2016-11-18 17:32:03 +00:00
array (
0 => array ( " pipe " , " r " ), //S TDIN
1 => array ( " pipe " , " w " ), //S TDOUT
2 => array ( " pipe " , " w " ), //S TDERR
),
$pipes
);
if ( $process !== false ) {
$stdout = stream_get_contents ( $pipes [ 1 ]);
$stderr = stream_get_contents ( $pipes [ 2 ]);
fclose ( $pipes [ 1 ]);
fclose ( $pipes [ 2 ]);
proc_close ( $process );
2016-11-19 09:50:57 +00:00
if ( $stdout == '' ) {
$this -> main -> getLogger () -> info ( " Git error: $stderr " );
2016-11-18 17:32:03 +00:00
$this -> main -> getLogger () -> critical ( " Executable wasn't found at path $this->executable . Be sure that you installed git and that the path in the config is the executable path. " );
$this -> main -> setEnabled ( false );
2016-11-19 09:50:57 +00:00
} else {
$this -> main -> getLogger () -> info ( " Loaded git: $stdout " );
2016-11-18 17:32:03 +00:00
}
}
2016-09-23 18:52:41 +00:00
}
2016-11-18 17:32:03 +00:00
2016-09-23 18:52:41 +00:00
}