Skip to content

Commit

Permalink
update simulation process
Browse files Browse the repository at this point in the history
- Simulatioin process doesn't anymore copy real files but just simulates
- Add callbacks examples in example/client/update/index.php
- Edit README example code to add $simulate variable
  • Loading branch information
migliori committed Jul 3, 2018
1 parent e5a5315 commit 60d51a0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 30 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ if ($update->checkUpdate() === false)

// Check if new update is available
if ($update->newVersionAvailable()) {
echo 'New Version: ' . $update->getLatestVersion();
// Simulate or install?
$simulate = true;
//Install new update
echo 'New Version: ' . $update->getLatestVersion();
$result = $update->update($simulate);
if ($result === true) {
echo 'Update simulation successful<br>';
} else {
echo 'Update simulation failed: ' . $result . '!<br>';
}
} else {
// No new update
echo 'Your application is up to date';
Expand Down
81 changes: 54 additions & 27 deletions example/client/update/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,63 @@
// $update->setCache(new Desarrolla2\Cache\Adapter\File(__DIR__ . '/cache'), 3600);

//Check for a new update
if ($update->checkUpdate() === false)
die('Could not check for updates! See log file for details.');
if ($update->checkUpdate() === false) {
die('Could not check for updates! See log file for details.');
}

if ($update->newVersionAvailable()) {
//Install new update
echo 'New Version: ' . $update->getLatestVersion() . '<br>';
echo 'Installing Updates: <br>';
echo '<pre>';
var_dump(array_map(function($version) {
return (string) $version;
}, $update->getVersionsToUpdate()));
echo '</pre>';

// This call will only simulate an update.
// Set the first argument (simulate) to "false" to install the update
// i.e. $update->update(false);
$result = $update->update();
if ($result === true) {
echo 'Update simulation successful<br>';
} else {
echo 'Update simulation failed: ' . $result . '!<br>';

if ($result = AutoUpdate::ERROR_SIMULATE) {
echo '<pre>';
var_dump($update->getSimulationResults());
echo '</pre>';
}
}
//Install new update
echo 'New Version: ' . $update->getLatestVersion() . '<br>';
echo 'Installing Updates: <br>';
echo '<pre>';
var_dump(array_map(function ($version) {
return (string) $version;
}, $update->getVersionsToUpdate()));
echo '</pre>';

// Optional - empty log file
$f = @fopen(__DIR__ . '/update.log', 'r+');
if ($f !== false) {
ftruncate($f, 0);
fclose($f);
}

// Optional Callback function - on each version update
function eachUpdateFinishCallback($updatedVersion)
{
echo '<h3>CALLBACK for version ' . $updatedVersion . '</h3>';
}
$update->onEachUpdateFinish('eachUpdateFinishCallback');

// Optional Callback function - on each version update
function onAllUpdateFinishCallbacks($updatedVersions)
{
echo '<h3>CALLBACK for all updated versions:</h3>';
echo '<ul>';
foreach ($updatedVersions as $v) {
echo '<li>' . $v . '</li>';
}
echo '</ul>';
}
$update->setOnAllUpdateFinishCallbacks('onAllUpdateFinishCallbacks');

// This call will only simulate an update.
// Set the first argument (simulate) to "false" to install the update
// i.e. $update->update(false);
$result = $update->update();
if ($result === true) {
echo 'Update simulation successful<br>';
} else {
echo 'Update simulation failed: ' . $result . '!<br>';

if ($result = AutoUpdate::ERROR_SIMULATE) {
echo '<pre>';
var_dump($update->getSimulationResults());
echo '</pre>';
}
}
} else {
echo 'Current Version is up to date<br>';
echo 'Current Version is up to date<br>';
}

echo 'Log:<br>';
Expand Down
12 changes: 10 additions & 2 deletions src/AutoUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,22 @@ protected function _install($updateFile, $simulateInstall, $version)
$this->_log->addNotice(sprintf('Trying to install update "%s"', $updateFile));

// Check if install should be simulated
if ($simulateInstall && !$this->_simulateInstall($updateFile)) {
$this->_log->addCritical('Simulation of update process failed!');
if ($simulateInstall) {
if ($this->_simulateInstall($updateFile)) {
$this->_log->addNotice(sprintf('Simulation of update "%s" process succeeded', $version));

return true;
}

$this->_log->addCritical(sprintf('Simulation of update "%s" process failed!', $version));

return self::ERROR_SIMULATE;
}

clearstatcache();

// Install only if simulateInstall === false

// Check if zip file could be opened
$zip = zip_open($updateFile);
if (!is_resource($zip)) {
Expand Down

0 comments on commit 60d51a0

Please sign in to comment.