/** * Update a plugin from the API */ public function update() { App::response()->setContentType('json'); try { $plugin = Plugin::get($this->plugin); if (!$plugin) { throw new \Exception('The plugin "' . $this->plugin . '" does not exist'); } $api = new HawkApi(); $updates = $api->getPluginsAvailableUpdates(array($plugin->getName() => $plugin->getDefinition('version'))); if (!empty($updates[$plugin->getName()])) { $file = $api->downloadPlugin($this->plugin); $zip = new \ZipArchive(); if ($zip->open($file) !== true) { throw new \Exception('Impossible to open the zip archive'); } // Copy the actual version of the plugin as backup $backup = TMP_DIR . $plugin->getName() . '.bak'; rename($plugin->getRootDir(), $backup); try { $zip->extractTo(PLUGINS_DIR); unset(Plugin::$instances[$this->plugin]); $plugin = Plugin::get($this->plugin); if (!$plugin) { throw new \Exception('An error occured while downloading the plugin'); } $this->installOrupdateDependencies($plugin); $installer = $plugin->getInstallerInstance(); foreach ($updates[$plugin->getName()] as $version) { $method = str_replace('.', '_', 'updateV' . $version); if (method_exists($installer, $method)) { $installer->{$method}(); } } App::fs()->remove($backup); } catch (\Exception $e) { // An error occured while installing the new version, rollback to the previous version App::fs()->remove($plugin->getRootDir()); rename($backup, $plugin->getRootDir()); App::fs()->remove($file); throw $e; } App::fs()->remove($file); } return array(); } catch (\Exception $e) { throw new InternalErrorException($e->getMessage()); } }