Convert Versions constants to enum cases

This commit is contained in:
netkas 2024-09-13 13:20:12 -04:00
parent f9047dd0d0
commit 1ba50cc7ee
16 changed files with 51 additions and 56 deletions

View file

@ -47,7 +47,7 @@
/**
* @inheritDoc
*/
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
try
{
@ -64,7 +64,7 @@
/**
* @inheritDoc
*/
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return self::getReleasePackage($repository, $vendor, $project, $version, $authentication, $options);
}
@ -162,7 +162,7 @@
*/
private static function getTagArchive(RepositoryConfiguration $repository, string $group, string $project, string $tag, ?AuthenticationInterface $authentication=null): RepositoryResult
{
if($tag === Versions::LATEST)
if($tag === Versions::LATEST->value)
{
$tag = self::getLatestTag($repository, $group, $project, $authentication);
}
@ -307,7 +307,7 @@
private static function getReleasePackage(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication=null, array $options=[]): RepositoryResult
{
/** @noinspection DuplicatedCode */
if($release === Versions::LATEST)
if($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}
@ -409,7 +409,7 @@
private static function getReleaseArchive(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication=null): RepositoryResult
{
/** @noinspection DuplicatedCode */
if ($release === Versions::LATEST)
if ($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}

View file

@ -47,7 +47,7 @@
/**
* @inheritDoc
*/
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
try
{
@ -64,7 +64,7 @@
/**
* @inheritDoc
*/
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST->value, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
{
return self::getReleasePackage($repository, $vendor, $project, $version, $authentication, $options);
}
@ -161,7 +161,7 @@
*/
private static function getTagArchive(RepositoryConfiguration $repository, string $group, string $project, string $tag, ?AuthenticationInterface $authentication = null): RepositoryResult
{
if($tag === Versions::LATEST)
if($tag === Versions::LATEST->value)
{
$tag = self::getLatestTag($repository, $group, $project, $authentication);
}
@ -307,7 +307,7 @@
private static function getReleasePackage(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication=null, array $options=[]): RepositoryResult
{
/** @noinspection DuplicatedCode */
if($release === Versions::LATEST)
if($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}
@ -389,7 +389,7 @@
private static function getReleaseArchive(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication = null): RepositoryResult
{
/** @noinspection DuplicatedCode */
if($release === Versions::LATEST)
if($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}

View file

@ -47,7 +47,7 @@
/**
* @inheritDoc
*/
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
try
{
@ -64,7 +64,7 @@
/**
* @inheritDoc
*/
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return self::getReleasePackage($repository, $vendor, $project, $version, $authentication, $options);
}
@ -164,7 +164,7 @@
*/
private static function getTagArchive(RepositoryConfiguration $repository, string $group, string $project, string $tag, ?AuthenticationInterface $authentication=null): RepositoryResult
{
if($tag === Versions::LATEST)
if($tag === Versions::LATEST->value)
{
$tag = self::getLatestTag($repository, $group, $project, $authentication);
}
@ -308,7 +308,7 @@
*/
private static function getReleasePackage(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication=null, array $options=[]): RepositoryResult
{
if($release === Versions::LATEST)
if($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}
@ -393,7 +393,7 @@
private static function getReleaseArchive(RepositoryConfiguration $repository, string $group, string $project, string $release, ?AuthenticationInterface $authentication=null): RepositoryResult
{
/** @noinspection DuplicatedCode */
if($release === Versions::LATEST)
if($release === Versions::LATEST->value)
{
$release = self::getLatestRelease($repository, $group, $project, $authentication);
}

View file

@ -46,9 +46,9 @@
/**
* @inheritDoc
*/
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST->value, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
{
if($version === Versions::LATEST)
if($version === Versions::LATEST->value)
{
$version = self::getLatestVersion($repository, $vendor, $project);
}
@ -98,7 +98,7 @@
* @inheritDoc
* @throws NotSupportedException
*/
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version = Versions::LATEST->value, ?AuthenticationType $authentication = null, array $options=[]): RepositoryResult
{
throw new NotSupportedException('Fetching ncc packages from Packagist is not supported');
}

View file

@ -121,7 +121,7 @@
* @return string
* @throws ImportException
*/
public static function import(string $package, string $version=Versions::LATEST): string
public static function import(string $package, string $version=Versions::LATEST->value): string
{
if(self::isImported($package))
{
@ -173,7 +173,7 @@
* @throws NotSupportedException
* @throws PathNotFoundException
*/
private static function importFromSystem(string $package, string $version=Versions::LATEST): string
private static function importFromSystem(string $package, string $version=Versions::LATEST->value): string
{
if(!self::getPackageManager()->getPackageLock()->entryExists($package))
{

View file

@ -22,25 +22,20 @@
namespace ncc\Enums;
final class Versions
enum Versions : string
{
/**
* The current version of the credentials store file format
*/
public const CREDENTIALS_STORE_VERSION = '1.0.0';
/**
* The current version of the package structure file format
*/
public const PACKAGE_STRUCTURE_VERSION = '2.0';
case CREDENTIALS_STORE_VERSION = '1.0.0';
/**
* The current version of the package lock structure file format
*/
public const PACKAGE_LOCK_VERSION = '2.0.0';
case PACKAGE_LOCK_VERSION = '2.0.0';
/**
* Generic version of the package structure file format (latest)
*/
public const LATEST = 'latest';
case LATEST = 'latest';
}

View file

@ -45,7 +45,7 @@
* @throws AuthenticationException If the authentication is invalid
* @throws NetworkException If there was an error getting the source
*/
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult;
public static function fetchSourceArchive(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult;
/**
* Returns the archive URL for the ncc package of the specified group and project.
@ -61,5 +61,5 @@
* @throws AuthenticationException If the authentication is invalid
* @throws NetworkException If there was an error getting the package
*/
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult;
public static function fetchPackage(RepositoryConfiguration $repository, string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult;
}

View file

@ -206,7 +206,7 @@
{
Console::outVerbose(sprintf(
'Removing symlink for %s=%s at %s',
$package_name, $this->package_lock->getEntry($package_name)->getVersion(Versions::LATEST)->getVersion(),
$package_name, $this->package_lock->getEntry($package_name)->getVersion(Versions::LATEST->value)->getVersion(),
PathFinder::findBinPath() . DIRECTORY_SEPARATOR . strtolower($package_name)
));
@ -216,7 +216,7 @@
}
catch(Exception $e)
{
throw new IOException(sprintf('Failed to resolve symlink for %s=%s: %s', $package_name, $this->package_lock->getEntry($package_name)->getVersion(Versions::LATEST)->getVersion(), $e->getMessage()), $e);
throw new IOException(sprintf('Failed to resolve symlink for %s=%s: %s', $package_name, $this->package_lock->getEntry($package_name)->getVersion(Versions::LATEST->value)->getVersion(), $e->getMessage()), $e);
}
if(is_file($symlink_path))

View file

@ -59,7 +59,7 @@
/**
* Public Constructor
*/
public function __construct(array $entries=[], string $package_lock_version=Versions::PACKAGE_LOCK_VERSION, ?int $last_updated_timestamp=null)
public function __construct(array $entries=[], string $package_lock_version=Versions::PACKAGE_LOCK_VERSION->value, ?int $last_updated_timestamp=null)
{
$this->entries = $entries;
$this->package_lock_version = $package_lock_version;
@ -280,7 +280,7 @@
}, $entries_array);
$package_lock_version = Functions::array_bc($data, 'package_lock_version') ?? Versions::PACKAGE_LOCK_VERSION;
$package_lock_version = Functions::array_bc($data, 'package_lock_version') ?? Versions::PACKAGE_LOCK_VERSION->value;
$last_updated_timestamp = Functions::array_bc($data, 'last_updated_timestamp') ?? time();
if($package_lock_version === null)

View file

@ -183,7 +183,7 @@
*/
public function versionExists(string $version): bool
{
if($version === Versions::LATEST)
if($version === Versions::LATEST->value)
{
try
{
@ -219,7 +219,7 @@
*/
public function getSatisfyingVersion(string $version): string
{
if($version === Versions::LATEST)
if($version === Versions::LATEST->value)
{
$version = $this->getLatestVersion();
}
@ -248,7 +248,7 @@
*/
public function getVersion(string $version): VersionEntry
{
if($version === Versions::LATEST)
if($version === Versions::LATEST->value)
{
$version = $this->getLatestVersion();
}
@ -354,7 +354,7 @@
* @throws IOException
* @throws PathNotFoundException
*/
public function getAssembly(string $version=Versions::LATEST): Assembly
public function getAssembly(string $version=Versions::LATEST->value): Assembly
{
$assembly_path = $this->getPath($version) . DIRECTORY_SEPARATOR . FileDescriptor::ASSEMBLY;
if(!is_file($assembly_path))
@ -375,7 +375,7 @@
* @throws PathNotFoundException
* @throws NotSupportedException
*/
public function getMetadata(string $version=Versions::LATEST): Metadata
public function getMetadata(string $version=Versions::LATEST->value): Metadata
{
$metadata_path = $this->getPath($version) . DIRECTORY_SEPARATOR . FileDescriptor::METADATA;
if(!is_file($metadata_path))
@ -394,7 +394,7 @@
* @throws IOException
* @throws PathNotFoundException
*/
public function getInstaller(string $version=Versions::LATEST): ?Installer
public function getInstaller(string $version=Versions::LATEST->value): ?Installer
{
$installer_path = $this->getPath($version) . DIRECTORY_SEPARATOR . FileDescriptor::INSTALLER;
if(!is_file($installer_path))
@ -413,7 +413,7 @@
* @throws IOException
* @throws PathNotFoundException
*/
public function getClassMap(string $version=Versions::LATEST): array
public function getClassMap(string $version=Versions::LATEST->value): array
{
$class_map_path = $this->getPath($version) . DIRECTORY_SEPARATOR . FileDescriptor::CLASS_MAP;
if(!is_file($class_map_path))
@ -434,7 +434,7 @@
* @throws IOException
* @throws PathNotFoundException
*/
public function getExecutionPolicy(string $policy_name, string $version=Versions::LATEST): ExecutionPolicy
public function getExecutionPolicy(string $policy_name, string $version=Versions::LATEST->value): ExecutionPolicy
{
$execution_policy_path = $this->getPath($version) . DIRECTORY_SEPARATOR . 'units' . DIRECTORY_SEPARATOR . $policy_name . '.policy';
if(!is_file($execution_policy_path))
@ -453,7 +453,7 @@
* @return string
* @throws IOException
*/
public function getExecutionBinaryPath(string $policy_name, string $version=Versions::LATEST): string
public function getExecutionBinaryPath(string $policy_name, string $version=Versions::LATEST->value): string
{
$execution_binary_path = $this->getPath($version) . DIRECTORY_SEPARATOR . 'units' . DIRECTORY_SEPARATOR . $policy_name;
if(!is_file($execution_binary_path))

View file

@ -63,7 +63,7 @@
{
$this->name = $name;
$this->source = $source;
$this->version = $version ?? Versions::LATEST;
$this->version = $version ?? Versions::LATEST->value;
}
/**
@ -115,7 +115,7 @@
*/
public function getVersion(): string
{
return $this->version ?? Versions::LATEST;
return $this->version ?? Versions::LATEST->value;
}
/**
@ -127,7 +127,7 @@
*/
public function setVersion(?string $version): void
{
$this->version = ($version ?? Versions::LATEST);
$this->version = ($version ?? Versions::LATEST->value);
}
/**
@ -140,7 +140,7 @@
throw new ConfigurationException(sprintf('Invalid dependency name "%s"', $this->name));
}
if($this->version !== Versions::LATEST && !Validate::version($this->version))
if($this->version !== Versions::LATEST->value && !Validate::version($this->version))
{
throw new ConfigurationException(sprintf('Invalid dependency version "%s"', $this->version));
}

View file

@ -66,7 +66,7 @@
{
$this->package = $package;
$this->vendor = $vendor;
$this->version = Versions::LATEST;
$this->version = Versions::LATEST->value;
}
/**
@ -126,7 +126,7 @@
*/
public function setVersion(?string $version): void
{
$this->version = $version ?? Versions::LATEST;
$this->version = $version ?? Versions::LATEST->value;
}
/**
@ -238,7 +238,7 @@
}
$object = new RemotePackageInput($matches['package'], $matches['vendor']);
$object->version = empty($matches['version']) ? Versions::LATEST : $matches['version'];
$object->version = empty($matches['version']) ? Versions::LATEST->value : $matches['version'];
$object->branch = empty($matches['branch']) ? null : $matches['branch'];
$object->repository = empty($matches['source']) ? null : $matches['source'];

View file

@ -178,7 +178,7 @@
* @throws NetworkException If there was an error getting the package
* @throws NotSupportedException If the repository type does not support fetching packages
*/
public function fetchPackage(string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public function fetchPackage(string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return match(strtolower($this->type))
{
@ -202,7 +202,7 @@
* @throws AuthenticationException If the authentication is invalid
* @throws NetworkException If there was an error getting the source
*/
public function fetchSourceArchive(string $vendor, string $project, string $version=Versions::LATEST, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
public function fetchSourceArchive(string $vendor, string $project, string $version=Versions::LATEST->value, ?AuthenticationType $authentication=null, array $options=[]): RepositoryResult
{
return match(strtolower($this->type))
{

View file

@ -53,7 +53,7 @@
*/
public function __construct()
{
$this->version = Versions::CREDENTIALS_STORE_VERSION;
$this->version = Versions::CREDENTIALS_STORE_VERSION->value;
$this->entries = [];
}

View file

@ -641,7 +641,7 @@
* @param string $version
* @return string
*/
public static function createExecutionPointer(string $package_name, string $version=Versions::LATEST): string
public static function createExecutionPointer(string $package_name, string $version=Versions::LATEST->value): string
{
$content = "#!/bin/sh\n";
$content .= sprintf('exec ncc exec --package "%s" --exec-version "%s" --exec-args "$@"', $package_name, $version);

View file

@ -24,7 +24,7 @@
* @param array $options
* @return string
*/
function import(string $package, string $version=Versions::LATEST, array $options=[]): string
function import(string $package, string $version=Versions::LATEST->value, array $options=[]): string
{
try
{