Refactored \ncc\Objects > PackageLock

This commit is contained in:
Netkas 2023-07-11 18:22:28 -04:00
parent 8fca981d85
commit b287ba5457
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
2 changed files with 36 additions and 10 deletions

View file

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.0.3] - Unreleased
### Changed
- Refactored `\ncc\Objects > PackageLock`
## [1.0.2] - 2023-06-29 ## [1.0.2] - 2023-06-29
### Fixed ### Fixed

View file

@ -85,19 +85,24 @@
*/ */
public function getVersion(string $version, bool $throw_exception=false): ?VersionEntry public function getVersion(string $version, bool $throw_exception=false): ?VersionEntry
{ {
if($version == Versions::Latest) if($version === Versions::Latest && $this->LatestVersion !== null)
{
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$version = $this->LatestVersion; $version = $this->LatestVersion;
}
foreach($this->Versions as $versionEntry) foreach($this->Versions as $versionEntry)
{ {
if($versionEntry->Version == $version) if($versionEntry->Version === $version)
{ {
return $versionEntry; return $versionEntry;
} }
} }
if($throw_exception) if($throw_exception)
{
throw new VersionNotFoundException('The version entry is not found'); throw new VersionNotFoundException('The version entry is not found');
}
return null; return null;
} }
@ -113,15 +118,16 @@
{ {
$count = 0; $count = 0;
$found_node = false; $found_node = false;
foreach($this->Versions as $versionEntry) foreach($this->Versions as $versionEntry)
{ {
if($versionEntry->Version == $version) if($versionEntry->Version === $version)
{ {
$found_node = true; $found_node = true;
break; break;
} }
$count += 1; ++$count;
} }
if($found_node) if($found_node)
@ -150,7 +156,11 @@
{ {
if ($this->getVersion($package->Assembly->Version) !== null) if ($this->getVersion($package->Assembly->Version) !== null)
{ {
if (!$overwrite) return false; if(!$overwrite)
{
return false;
}
$this->removeVersion($package->Assembly->Version); $this->removeVersion($package->Assembly->Version);
} }
} }
@ -167,7 +177,9 @@
$version->Location = $install_path; $version->Location = $install_path;
foreach($version->ExecutionUnits as $unit) foreach($version->ExecutionUnits as $unit)
{
$unit->Data = null; $unit->Data = null;
}
foreach($package->Dependencies as $dependency) foreach($package->Dependencies as $dependency)
{ {
@ -187,16 +199,21 @@
private function updateLatestVersion(): void private function updateLatestVersion(): void
{ {
$latest_version = null; $latest_version = null;
foreach($this->Versions as $version) foreach($this->Versions as $version)
{ {
$version = $version->Version; $version = $version->Version;
if($latest_version == null)
if($latest_version === null)
{ {
$latest_version = $version; $latest_version = $version;
continue; continue;
} }
if(VersionComparator::compareVersion($version, $latest_version)) if(VersionComparator::compareVersion($version, $latest_version))
{
$latest_version = $version; $latest_version = $version;
}
} }
$this->LatestVersion = $latest_version; $this->LatestVersion = $latest_version;
@ -236,7 +253,7 @@
{ {
$path = PathFinder::getPackageDataPath($this->Name); $path = PathFinder::getPackageDataPath($this->Name);
if(!file_exists($path) && Resolver::resolveScope() == Scopes::System) if(!file_exists($path) && Resolver::resolveScope() === Scopes::System)
{ {
$filesystem = new Filesystem(); $filesystem = new Filesystem();
$filesystem->mkdir($path); $filesystem->mkdir($path);
@ -254,6 +271,7 @@
public function toArray(bool $bytecode=false): array public function toArray(bool $bytecode=false): array
{ {
$versions = []; $versions = [];
foreach($this->Versions as $version) foreach($this->Versions as $version)
{ {
$versions[] = $version->toArray($bytecode); $versions[] = $version->toArray($bytecode);
@ -263,12 +281,12 @@
($bytecode ? Functions::cbc('name') : 'name') => $this->Name, ($bytecode ? Functions::cbc('name') : 'name') => $this->Name,
($bytecode ? Functions::cbc('latest_version') : 'latest_version') => $this->LatestVersion, ($bytecode ? Functions::cbc('latest_version') : 'latest_version') => $this->LatestVersion,
($bytecode ? Functions::cbc('versions') : 'versions') => $versions, ($bytecode ? Functions::cbc('versions') : 'versions') => $versions,
($bytecode ? Functions::cbc('update_source') : 'update_source') => ($this->UpdateSource?->toArray($bytecode) ?? null), ($bytecode ? Functions::cbc('update_source') : 'update_source') => ($this->UpdateSource?->toArray($bytecode)),
]; ];
} }
/** /**
* Constructs object from an array representation * Constructs an object from an array representation
* *
* @param array $data * @param array $data
* @return PackageEntry * @return PackageEntry
@ -279,11 +297,13 @@
$object->Name = Functions::array_bc($data, 'name'); $object->Name = Functions::array_bc($data, 'name');
$object->LatestVersion = Functions::array_bc($data, 'latest_version'); $object->LatestVersion = Functions::array_bc($data, 'latest_version');
$versions = Functions::array_bc($data, 'versions');
$object->UpdateSource = Functions::array_bc($data, 'update_source'); $object->UpdateSource = Functions::array_bc($data, 'update_source');
$versions = Functions::array_bc($data, 'versions');
if($object->UpdateSource !== null) if($object->UpdateSource !== null)
{
$object->UpdateSource = UpdateSource::fromArray($object->UpdateSource); $object->UpdateSource = UpdateSource::fromArray($object->UpdateSource);
}
if($versions !== null) if($versions !== null)
{ {