/**
  * @param array $tags
  * @param bool  $excludeUnstables
  *
  * @return array
  */
 private function sortTags(array $tags, $excludeUnstables = true)
 {
     $return = array();
     // Don't include invalid tags
     foreach ($tags as $tag) {
         try {
             $fixedName = $this->fixupRawTag($tag['name']);
             $v = new version($fixedName);
             if ($v->valid()) {
                 $version = $v->getVersion();
                 if ($excludeUnstables && $this->isNotStable($v)) {
                     continue;
                 }
                 $tag['parsed_version'] = $v;
                 $return[$version] = $tag;
             }
         } catch (\Exception $ex) {
             // Skip
         }
     }
     uasort($return, function ($a, $b) {
         return version::compare($a['parsed_version'], $b['parsed_version']);
     });
     return $return;
 }
Exemple #2
0
 public function compareTwoVersions($a, $b)
 {
     return version::compare($a, $b);
 }
 /**
  * @param  array $tags
  * @return array
  */
 private function sortTags($tags)
 {
     $return = array();
     // Don't include invalid tags
     foreach ($tags as $tag) {
         try {
             $fixedName = $this->fixupRawTag($tag['name']);
             $v = new version($fixedName, true);
             if ($v->valid()) {
                 $tag['parsed_version'] = $v;
                 $return[$v->getVersion()] = $tag;
             }
         } catch (\Exception $ex) {
             // Skip
         }
     }
     uasort($return, function ($a, $b) {
         return version::compare($a['parsed_version'], $b['parsed_version']);
     });
     return $return;
 }
 /**
  * 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->addNotice('Checking for a new update...');
     // Reset previous updates
     $this->_latestVersion = new version('0.0.0');
     $this->_updates = [];
     $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->addDebug(sprintf('Get new updates from %s', $updateFile));
         // Read update file from update server
         $update = @file_get_contents($updateFile, $this->_useBasicAuth());
         if ($update === false) {
             $this->_log->addInfo(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->addInfo('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->addInfo('Unable to parse json update file!');
                     return false;
                 }
                 break;
             default:
                 $this->_log->addInfo(sprintf('Unknown file extension "%s"', $updateFileExtension));
                 return false;
         }
         $this->_cache->set('update-versions', $versions);
     } else {
         $this->_log->addDebug('Got updates from cache');
     }
     // Check for latest version
     foreach ($versions as $versionRaw => $updateUrl) {
         $version = new version($versionRaw);
         if ($version->valid() === null) {
             $this->_log->addInfo(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[] = ['version' => $version, 'url' => $updateUrl];
         }
     }
     // Sort versions to install
     usort($this->_updates, function ($a, $b) {
         return version::compare($a['version'], $b['version']);
     });
     if ($this->newVersionAvailable()) {
         $this->_log->addDebug(sprintf('New version "%s" available', $this->_latestVersion));
         return true;
     } else {
         $this->_log->addDebug('No new version available');
         return self::NO_UPDATE_AVAILABLE;
     }
 }
 /**
  * Sorts an array of packages by version.
  * Descending order based on Semantic Versioning.
  *
  * @param array $array Array of objects
  */
 protected function sortPackagesByVersion(array $array = array())
 {
     usort($array, function ($first, $second) {
         if ($first instanceof UpdatePackage && $second instanceof UpdatePackage) {
             return version::compare($first->getVersion(), $second->getVersion());
         }
     });
     return $array;
 }