Skip to content

Commit

Permalink
Few code optimizations
Browse files Browse the repository at this point in the history
Signed-off-by: Fadion Dashi <[email protected]>
  • Loading branch information
fadion committed Feb 13, 2014
1 parent 7a2cc04 commit 57b2b70
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions phploy
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class PHPloy {

foreach($servers as $name => $options) {

if($name === 'quickmode') {
if($name == 'quickmode') {
foreach($options as $env => $creds) {
$options = parse_url($creds);
$this->servers[$env] = $options;
Expand Down Expand Up @@ -167,8 +167,10 @@ class PHPloy {
*/
protected function compare($local_revision)
{
$remote_revision = '';
$remote_revision = null;
$tmp_file = tmpfile();
$files_to_upload = array();
$files_to_delete = array();

if( $this->is_submodule ) {
$this->dot_revision = $this->is_submodule . '/.revision';
Expand All @@ -182,41 +184,31 @@ class PHPloy {
$this->output('Fresh deployment - grab a ☕');
}

if( ! empty( $remote_revision ) ) {
if( $remote_revision ) ) {
$command = "git --git-dir=\"$this->repo/.git\" --work-tree=\"$this->repo\" diff --name-status {$remote_revision }...{$local_revision}";
exec( $command, $output );

foreach( $output as $line ) {
// Accessing single characters in a string
// can also be achieved using "square brackets"
if( $line[0] == 'A' or $line[0] == 'C' or $line[0] == 'M') {
$files_to_upload[] = trim( substr( $line, 1 ) );
}
elseif( $line[0] == 'D' ) {
$files_to_delete[] = trim( substr( $line, 1 ) );
}
else {
throw new Exception("Unknown git-diff status: {$line[0]}");
}
}
} else {
$command = "git --git-dir=\"$this->repo/.git\" --work-tree=\"$this->repo\" ls-files";
}

$output = array();
exec( $command, $output );

$files_to_upload = array();
$files_to_delete = array();
exec( $command, $output );

if( ! empty( $remote_revision ) ) {
foreach( $output as $line ) {
// Accessing single characters in a string
// can also be achieved using "square brackets"
if( $line[0] == 'A' or $line[0] == 'C' or $line[0] == 'M') {
$files_to_upload[] = trim( substr( $line, 1 ) );
}
elseif( $line[0] == 'D' ) {
$files_to_delete[] = trim( substr( $line, 1 ) );
}
else {
throw new Exception("Unknown git-diff status: {$line[0]}");
}
}
} else {
$files_to_upload = $output;
$files_to_upload = $output;
}

foreach( $files_to_upload as $key => $file ) {
if( in_array( $file, $this->files_to_ignore ) ) {
unset($files_to_upload[$key]);
}
}
$files_to_upload = array_diff($files_to_upload, $this->files_to_ignore);

return array(
'upload' => $files_to_upload,
Expand Down Expand Up @@ -298,20 +290,20 @@ class PHPloy {
// Let's connect to the server.
$connection = @ftp_connect($server['host'], $server['port']);

if(!$connection) {
$this->output("Could not connect to {$server['host']}", true);
} else {
if (!@ftp_login($connection, $server['user'], $server['pass'])) {
throw new Exception("Could not login to {$server['host']} (Tried to login as {$server['user']}).");
}
ftp_pasv($connection, true);
if(ftp_chdir($connection, $server['path'])) {
$this->connection = $connection;
$this->output("\r\n+ ---------- ☻ ---------- +");
} else {
throw new Exception("Could not change the FTP directory to {$server['path']}.");
}
}
if($connection) {
if (!@ftp_login($connection, $server['user'], $server['pass'])) {
throw new Exception("Could not login to {$server['host']} (Tried to login as {$server['user']}).");
}
ftp_pasv($connection, true);
if(ftp_chdir($connection, $server['path'])) {
$this->connection = $connection;
$this->output("\r\n+ ---------- ☻ ---------- +");
} else {
throw new Exception("Could not change the FTP directory to {$server['path']}.");
}
} else {
throw new Exception("Could not connect to {$server['host']}");
}
}

/**
Expand Down

0 comments on commit 57b2b70

Please sign in to comment.