Exemplo n.º 1
0
 /**
  * Check for a new version
  *
  * @return int|bool
  *         true: New version is available
  *         false: Error while checking for update
  *         int: Status code (i.e. AutoUpdate::NO_UPDATE_AVAILABLE)
  */
 public function checkUpdate()
 {
     $this->log->debug('Checking for a new update...');
     // Reset previous updates
     $this->_latestVersion = new version('0.0.0');
     $this->_updates = array();
     //$versions = $this->_cache->get('update-versions');
     // Check if cache is empty
     //if ($versions === false) {
     // Create absolute url to update file
     $updateFile = $this->_updateUrl . '/' . $this->_updateFile;
     if (!empty($this->_branch)) {
         $updateFile .= '.' . $this->_branch;
     }
     $this->log->debug(sprintf('Get new updates from %s', $updateFile));
     // Read update file from update server
     $update = @file_get_contents($updateFile);
     if ($update === false) {
         $this->log->info(sprintf('Could not download update file "%s"!', $updateFile));
         return false;
     }
     // Parse update file
     $updateFileExtension = substr(strrchr($this->_updateFile, '.'), 1);
     switch ($updateFileExtension) {
         case 'ini':
             $versions = @parse_ini_string($update, true);
             if (!is_array($versions)) {
                 $this->log->info('Unable to parse ini update file!');
                 return false;
             }
             $versions = array_map(function ($block) {
                 return isset($block['url']) ? $block['url'] : false;
             }, $versions);
             break;
         case 'json':
             $versions = (array) @json_decode($update);
             if (!is_array($versions)) {
                 $this->log->info('Unable to parse json update file!');
                 return false;
             }
             break;
         default:
             $this->log->info(sprintf('Unknown file extension "%s"', $updateFileExtension));
             return false;
     }
     //	$this->_cache->set('update-versions', $versions);
     //} else {
     //	$this->log->debug('Got updates from cache');
     //}
     // Check for latest version
     foreach ($versions as $versionRaw => $updateInfo) {
         //var_dump($updateInfo);exit;
         $version = new version($versionRaw);
         if ($version->valid() === null) {
             $this->log->info(sprintf('Could not parse version "%s" from update server "%s"', $versionRaw, $updateFile));
             continue;
         }
         if (version::gt($version, $this->_currentVersion)) {
             if (version::gt($version, $this->_latestVersion)) {
                 $this->_latestVersion = $version;
             }
             $this->_updates[] = array('version' => $version, 'url' => $updateInfo->url, 'changelog' => $updateInfo->changelog, 'date' => $updateInfo->date);
         }
     }
     // Sort versions to install
     usort($this->_updates, function ($a, $b) {
         return version::compare($a['version'], $b['version']);
     });
     if ($this->newVersionAvailable()) {
         $this->log->debug(sprintf('New version "%s" available', $this->_latestVersion));
         return true;
     } else {
         $this->log->debug('No new version available');
         return self::NO_UPDATE_AVAILABLE;
     }
 }