Implemented require files handler & implemented proper exception handling for the import function

This commit is contained in:
Netkas 2023-10-01 14:34:58 -04:00
parent c86b4b7465
commit 107ebfa68c
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
2 changed files with 73 additions and 8 deletions

View file

@ -28,6 +28,7 @@
use InvalidArgumentException; use InvalidArgumentException;
use ncc\Enums\FileDescriptor; use ncc\Enums\FileDescriptor;
use ncc\Enums\Flags\PackageFlags; use ncc\Enums\Flags\PackageFlags;
use ncc\Enums\Options\BuildConfigurationOptions;
use ncc\Enums\Versions; use ncc\Enums\Versions;
use ncc\Exceptions\ConfigurationException; use ncc\Exceptions\ConfigurationException;
use ncc\Exceptions\ImportException; use ncc\Exceptions\ImportException;
@ -107,10 +108,7 @@
* @param string $package * @param string $package
* @param string $version * @param string $version
* @return string * @return string
* @throws ConfigurationException
* @throws IOException
* @throws ImportException * @throws ImportException
* @throws PathNotFoundException
*/ */
public static function import(string $package, string $version=Versions::LATEST): string public static function import(string $package, string $version=Versions::LATEST): string
{ {
@ -121,12 +119,34 @@
if(is_file($package)) if(is_file($package))
{ {
return self::importFromPackage(realpath($package)); try
{
return self::importFromPackage(realpath($package));
}
catch(ImportException $e)
{
throw $e;
}
catch(Exception $e)
{
throw new ImportException(sprintf('Failed to import package from file "%s" due to an exception: %s', $package, $e->getMessage()), $e);
}
} }
if(self::getPackageManager()->getPackageLock()->entryExists($package)) if(self::getPackageManager()->getPackageLock()->entryExists($package))
{ {
return self::importFromSystem($package, $version); try
{
return self::importFromSystem($package, $version);
}
catch(ImportException $e)
{
throw $e;
}
catch(Exception $e)
{
throw new ImportException(sprintf('Failed to import package from system "%s" due to an exception: %s', $package, $e->getMessage()), $e);
}
} }
throw new RuntimeException('Importing from a package name is not supported yet'); throw new RuntimeException('Importing from a package name is not supported yet');
@ -139,6 +159,7 @@
* @throws ConfigurationException * @throws ConfigurationException
* @throws IOException * @throws IOException
* @throws ImportException * @throws ImportException
* @throws NotSupportedException
* @throws PathNotFoundException * @throws PathNotFoundException
*/ */
private static function importFromSystem(string $package, string $version=Versions::LATEST): string private static function importFromSystem(string $package, string $version=Versions::LATEST): string
@ -160,7 +181,25 @@
self::import($dependency->getName(), $dependency->getVersion()); self::import($dependency->getName(), $dependency->getVersion());
} }
// TODO: Import required files if any (see options) if($entry->getMetadata($version)->getOption(BuildConfigurationOptions::REQUIRE_FILES) !== null)
{
foreach($entry->getMetadata($version)->getOption(BuildConfigurationOptions::REQUIRE_FILES) as $item)
{
try
{
// Get the file contents and prepare it
$required_file = IO::fread($entry->getPath($version) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $item);
$required_file = preg_replace('/^<\?php|<\?PHP/', '', $required_file, 1);
eval($required_file);
unset($required_file);
}
catch(ConfigurationException $e)
{
throw new ImportException(sprintf('Failed to import "%s" from %s: %s', $item, $package, $e->getMessage()), $e);
}
}
}
return $package; return $package;
} }
@ -173,6 +212,7 @@
* @throws ConfigurationException * @throws ConfigurationException
* @throws IOException * @throws IOException
* @throws ImportException * @throws ImportException
* @throws OperationException
* @throws PathNotFoundException * @throws PathNotFoundException
*/ */
private static function importFromPackage(string $package_path): string private static function importFromPackage(string $package_path): string
@ -219,7 +259,20 @@
} }
} }
// TODO: Import required files if any (see options) if($package_reader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES) !== null)
{
foreach($package_reader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES) as $item)
{
try
{
eval($package_reader->getComponent($item)->getData());
}
catch(ConfigurationException $e)
{
throw new ImportException(sprintf('Failed to import "%s" from %s: %s', $item, $package_name, $e->getMessage()), $e);
}
}
}
return $package_reader->getAssembly()->getPackage(); return $package_reader->getAssembly()->getPackage();
} }

View file

@ -163,7 +163,14 @@
{ {
if($version === Versions::LATEST) if($version === Versions::LATEST)
{ {
$version = $this->getLatestVersion(); try
{
$version = $this->getLatestVersion();
}
catch(InvalidArgumentException $e)
{
return false;
}
} }
foreach($this->versions as $version_entry) foreach($this->versions as $version_entry)
@ -260,6 +267,11 @@
} }
} }
if($latest_version === null)
{
throw new InvalidArgumentException(sprintf('Package %s does not have any versions', $this->name));
}
return $latest_version; return $latest_version;
} }