|
<?php namespace Acme; |
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
|
|
class Updater { |
|
|
|
/** |
|
* The Filesystem instance |
|
* |
|
* @var Illuminate\Filesystem\Filesystem |
|
*/ |
|
protected $file; |
|
|
|
|
|
/** |
|
* Full path to the application folder |
|
* |
|
* @var string |
|
*/ |
|
protected $app; |
|
|
|
/** |
|
* Full path to the backup folder |
|
* |
|
* @var string |
|
*/ |
|
protected $backup; |
|
|
|
/** |
|
* The constructor |
|
* |
|
* @param Illuminate\Filesystem\Filesystem $file |
|
* @param string $app |
|
* @return void |
|
*/ |
|
public function __construct(Filesystem $file, $app) |
|
{ |
|
$this->file = $file; |
|
|
|
$this->app = $app; |
|
} |
|
|
|
/** |
|
* Update the application |
|
* |
|
* @param string $source |
|
* @param string $backup |
|
* @return boolean |
|
*/ |
|
public function update($source, $backup) |
|
{ |
|
$this->backup = $backup; |
|
|
|
// if the process failed, we cannot proceed |
|
if ( ! $this->backup()) return false; |
|
|
|
// get all files from the source directory (recursively) |
|
$files = $this->file->allFiles($source); |
|
|
|
foreach ($files as $file) |
|
{ |
|
$from = $this->path($source, $file); // source path |
|
$to = $this->path($this->app, $file); // and destination path |
|
|
|
// attempt to [over]write a file |
|
// if something goes wrong, we rollback all the changes |
|
if ( ! $this->write($from, $to)) |
|
{ |
|
$this->rollback(); |
|
|
|
return false; |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
/** |
|
* Get the backup directory path |
|
* |
|
* @return string |
|
*/ |
|
protected function getBackupPath() |
|
{ |
|
return $this->backup; |
|
} |
|
|
|
/** |
|
* Backup the whole application |
|
* |
|
* @return boolean |
|
*/ |
|
protected function backup() |
|
{ |
|
return $this->file->copyDirectory($this->app, $this->getBackupPath()); |
|
} |
|
|
|
/** |
|
* Rollback all made changes |
|
* |
|
* @return boolean |
|
*/ |
|
protected function rollback() |
|
{ |
|
return $this->file->copyDirectory($this->getBackupPath(), $this->app); |
|
} |
|
|
|
/** |
|
* Write to a file |
|
* |
|
* @param string $from |
|
* @param string $to |
|
* @return boolean |
|
*/ |
|
protected function write($from, $to) |
|
{ |
|
$directory = dirname($from); |
|
|
|
$file = $this->file; |
|
|
|
return $file->isWritable($directory) and $file->put($from, $to); |
|
} |
|
|
|
/** |
|
* Build a path string |
|
* |
|
* @param dynamic |
|
* @return string |
|
*/ |
|
protected function path() |
|
{ |
|
$path = implode('/', func_get_args()); |
|
|
|
return str_replace('//', '/', $path); |
|
} |
|
|
|
} |