diff --git a/README.md b/README.md index 53e51e4..aa5ecd1 100755 --- a/README.md +++ b/README.md @@ -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
'; + } else { + echo 'Update simulation failed: ' . $result . '!
'; + } } else { // No new update echo 'Your application is up to date'; diff --git a/example/client/update/index.php b/example/client/update/index.php index 7634cd1..6463833 100755 --- a/example/client/update/index.php +++ b/example/client/update/index.php @@ -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() . '
'; - echo 'Installing Updates:
'; - echo '
';
-	var_dump(array_map(function($version) {
-		return (string) $version;
-	}, $update->getVersionsToUpdate()));
-	echo '
'; - - // 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
'; - } else { - echo 'Update simulation failed: ' . $result . '!
'; - - if ($result = AutoUpdate::ERROR_SIMULATE) { - echo '
';
-			var_dump($update->getSimulationResults());
-			echo '
'; - } - } + //Install new update + echo 'New Version: ' . $update->getLatestVersion() . '
'; + echo 'Installing Updates:
'; + echo '
';
+    var_dump(array_map(function ($version) {
+        return (string) $version;
+    }, $update->getVersionsToUpdate()));
+    echo '
'; + + // 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 '

CALLBACK for version ' . $updatedVersion . '

'; + } + $update->onEachUpdateFinish('eachUpdateFinishCallback'); + + // Optional Callback function - on each version update + function onAllUpdateFinishCallbacks($updatedVersions) + { + echo '

CALLBACK for all updated versions:

'; + echo ''; + } + $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
'; + } else { + echo 'Update simulation failed: ' . $result . '!
'; + + if ($result = AutoUpdate::ERROR_SIMULATE) { + echo '
';
+            var_dump($update->getSimulationResults());
+            echo '
'; + } + } } else { - echo 'Current Version is up to date
'; + echo 'Current Version is up to date
'; } echo 'Log:
'; diff --git a/src/AutoUpdate.php b/src/AutoUpdate.php index 25e94f6..b690b54 100755 --- a/src/AutoUpdate.php +++ b/src/AutoUpdate.php @@ -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)) {