Esempio n. 1
0
 /**
  * Get an addon based on its slug in the following form:
  *  - AddonID[-AddonName]
  *  - AddonType-AddonKey[-Version]
  *
  * @param string|int $Slug The slug to lookup
  * @param bool $GetVersions Whether or not to add an array of versions to the result.
  * @return array
  */
 public function getSlug($slug, $getVersions = false)
 {
     if (is_numeric($slug)) {
         $addon = $this->getID($slug, false, ['GetVersions' => $getVersions]);
     } else {
         // This is a string identifier for the addon.
         $parts = explode('-', $slug, 3);
         $key = val(0, $parts);
         if (is_numeric($key)) {
             $addon = $this->getID($key, false, ['GetVersions' => $getVersions]);
         } else {
             $type = strtolower(val(1, $parts));
             $typeID = val($type, self::$Types, 0);
             $version = val(2, $parts);
             $addon = $this->getID(array($key, $typeID, $version), false, ['GetVersions' => $getVersions]);
         }
     }
     if (!$addon) {
         return false;
     }
     $addon['Releases'] = [];
     $addon['Prereleases'] = [];
     if ($getVersions) {
         $maxVersion = valr('Versions.0', $addon);
         $foundMax = false;
         $viewingVersion = false;
         if (is_array(val('Versions', $addon))) {
             foreach ($addon['Versions'] as $version) {
                 // Find the version we are looking at.
                 $versionSlug = AddonModel::slug($addon, $version);
                 if ($versionSlug == $slug && $viewingVersion === false) {
                     $viewingVersion = $version;
                 }
                 // Separate releases & prereleases.
                 if (AddonModel::isReleaseVersion($version['Version'])) {
                     $addon['Releases'][] = $version;
                     // Find the latest stable version.
                     if (!$foundMax) {
                         $maxVersion = $version;
                         $foundMax = true;
                     }
                 } elseif ($foundMax === false) {
                     // Only list prereleases new than the current stable.
                     $addon['Prereleases'][] = $version;
                 }
             }
         }
         if ($viewingVersion === false) {
             $viewingVersion = $maxVersion;
         }
         $addon['CurrentAddonVersionID'] = $maxVersion['AddonVersionID'];
         $addon = array_merge($addon, (array) $viewingVersion);
         $addon['Slug'] = AddonModel::slug($addon, $viewingVersion);
     }
     return $addon;
 }