- \ncc\Objects\ProjectConfiguration > Compiler: Added Public Constructor to automatically determine the minimum and

maximum supported compiler version for the selected extension
 - `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an ConfigurationException if the property `extension` is null
 - `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an NotSupportedException if the `extension` uses an
   unsupported compiler extension
 - `\ncc\Objects\ProjectConfiguration > Compiler > validate()` No longer accepts `$throw_exception` and throws an
   `ConfigurationException` or `NotSupportedException` if the validation fails, otherwise it returns `True`.
 - `\ncc\Objects\ProjectConfiguration > Project > fromArray()` Throws an `ConfigurationException` if the property `compiler`
   is missing in the project configuration
 - `\ncc\Objects > ProjectConfiguration > fromArray()` Throws an `ConfigurationException` if the property 'project' is
   missing in the root configuration
 - `\ncc\Objects\ProjectConfiguration > Project > __construct()` now requires the parameter `$compiler`
 - Removed parameter `$throw_exception` from `\ncc\Objects\ProjectConfiguration > Project > validate()`
This commit is contained in:
Netkas 2023-08-30 20:38:52 -04:00
parent 99bdd933cd
commit 230675c586
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
6 changed files with 119 additions and 72 deletions

View file

@ -199,6 +199,18 @@ features and reduced the number of exceptions down to 15 exceptions.
- Updated class `\ncc\Objects > RemotePackageInput` to use method calls rather than direct property access - Updated class `\ncc\Objects > RemotePackageInput` to use method calls rather than direct property access
- Updated class `\ncc\Objects > RepositoryQueryResults` to use method calls rather than direct property access - Updated class `\ncc\Objects > RepositoryQueryResults` to use method calls rather than direct property access
- Updated class `\ncc\Objects > Vault` to use method calls rather than direct property access - Updated class `\ncc\Objects > Vault` to use method calls rather than direct property access
- `\ncc\Objects\ProjectConfiguration > Compiler`: Added Public Constructor to automatically determine the minimum and
maximum supported compiler version for the selected extension
- `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an ConfigurationException if the property `extension` is null
- `\ncc\Objects\ProjectConfiguration > Compiler > fromArray()` throws an NotSupportedException if the `extension` uses an
unsupported compiler extension
- `\ncc\Objects\ProjectConfiguration > Compiler > validate()` No longer accepts `$throw_exception` and throws an
`ConfigurationException` or `NotSupportedException` if the validation fails, otherwise it returns `True`.
- `\ncc\Objects\ProjectConfiguration > Project > fromArray()` Throws an `ConfigurationException` if the property `compiler`
is missing in the project configuration
- `\ncc\Objects > ProjectConfiguration > fromArray()` Throws an `ConfigurationException` if the property 'project' is
missing in the root configuration
- `\ncc\Objects\ProjectConfiguration > Project > __construct()` now requires the parameter `$compiler`
### Removed ### Removed
@ -261,6 +273,7 @@ features and reduced the number of exceptions down to 15 exceptions.
- Removed unused `scope` property from `\ncc\Objects\ProjectConfiguration > Build` - Removed unused `scope` property from `\ncc\Objects\ProjectConfiguration > Build`
- Removed unused `\ncc\Objects > NccUpdateInformation` - Removed unused `\ncc\Objects > NccUpdateInformation`
- Removed unused `\ncc\Objects > PhpConfiguration` - Removed unused `\ncc\Objects > PhpConfiguration`
- Removed parameter `$throw_exception` from `\ncc\Objects\ProjectConfiguration > Project > validate()`

View file

@ -208,15 +208,14 @@
* Attempts to load the project configuration * Attempts to load the project configuration
* *
* @return void * @return void
* @throws ConfigurationException
* @throws IOException * @throws IOException
* @throws PathNotFoundException * @throws PathNotFoundException
*/ */
public function load(): void public function load(): void
{ {
if(!file_exists($this->project_file_path) && !is_file($this->project_file_path)) if(!is_file($this->project_file_path))
{ {
throw new ConfigurationException('The project configuration file \'' . $this->project_file_path . '\' was not found'); throw new PathNotFoundException($this->project_file_path);
} }
$this->project_configuration = ProjectConfiguration::fromFile($this->project_file_path); $this->project_configuration = ProjectConfiguration::fromFile($this->project_file_path);

View file

@ -551,6 +551,10 @@
/** /**
* @inheritDoc * @inheritDoc
* @param array $data
* @return ProjectConfiguration
* @throws ConfigurationException
* @throws NotSupportedException
*/ */
public static function fromArray(array $data): ProjectConfiguration public static function fromArray(array $data): ProjectConfiguration
{ {
@ -561,6 +565,10 @@
{ {
$object->project = Project::fromArray($object->project); $object->project = Project::fromArray($object->project);
} }
else
{
throw new ConfigurationException('The project configuration is missing the required property "project" in the root of the configuration');
}
$object->assembly = Functions::array_bc($data, 'assembly'); $object->assembly = Functions::array_bc($data, 'assembly');
if($object->assembly !== null) if($object->assembly !== null)

View file

@ -1,4 +1,5 @@
<?php <?php
/* /*
* Copyright (c) Nosial 2022-2023, all rights reserved. * Copyright (c) Nosial 2022-2023, all rights reserved.
* *
@ -20,6 +21,7 @@
* *
*/ */
/** @noinspection PhpUnused */
/** @noinspection PhpMissingFieldTypeInspection */ /** @noinspection PhpMissingFieldTypeInspection */
namespace ncc\Objects\ProjectConfiguration; namespace ncc\Objects\ProjectConfiguration;
@ -60,6 +62,44 @@
*/ */
private $maximum_version; private $maximum_version;
/**
* Compiler constructor.
*
* @param string $extension
* @param string|null $minimum_version
* @param string|null $maximum_version
* @throws NotSupportedException
*/
public function __construct(string $extension, ?string $minimum_version=null, ?string $maximum_version=null)
{
$extension = strtolower($extension);
/** @noinspection DegradedSwitchInspection */
switch($extension)
{
case CompilerExtensions::PHP:
if($minimum_version === null)
{
$minimum_version = CompilerExtensionSupportedVersions::PHP[0];
}
if($maximum_version === null)
{
$maximum_version = CompilerExtensionSupportedVersions::PHP[count(CompilerExtensionSupportedVersions::PHP) - 1];
}
break;
default:
throw new NotSupportedException(sprintf('The compiler extension \'%s\' is not supported in ncc', $extension));
}
$this->extension = $extension;
$this->minimum_version = $minimum_version;
$this->maximum_version = $maximum_version;
}
/** /**
* @return string * @return string
*/ */
@ -111,53 +151,32 @@
/** /**
* Validates the compiler object * Validates the compiler object
* *
* @param bool $throw_exception
* @return bool * @return bool
* @throws ConfigurationException * @throws ConfigurationException
* @throws NotSupportedException * @throws NotSupportedException
*/ */
public function validate(bool $throw_exception=True): bool public function validate(): bool
{ {
if($this->extension === null) if($this->extension === null)
{ {
if($throw_exception) throw new ConfigurationException('The property \'extension\' must not be null.');
{
throw new ConfigurationException('The property \'extension\' must not be null.');
}
return False;
} }
if($this->minimum_version === null) if($this->minimum_version === null)
{ {
if($throw_exception) throw new ConfigurationException('The property \'minimum_version\' must not be null.');
{
throw new ConfigurationException('The property \'minimum_version\' must not be null.');
}
return False;
} }
if($this->maximum_version === null) if($this->maximum_version === null)
{ {
if($throw_exception) throw new ConfigurationException('The property \'maximum_version\' must not be null.');
{
throw new ConfigurationException('The property \'maximum_version\' must not be null.');
}
return False;
} }
try try
{ {
if(VersionComparator::compareVersion($this->minimum_version, $this->maximum_version) === 1) if(VersionComparator::compareVersion($this->minimum_version, $this->maximum_version) === 1)
{ {
if($throw_exception) throw new ConfigurationException('The minimum version cannot be greater version number than the maximum version');
{
throw new ConfigurationException('The minimum version cannot be greater version number than the maximum version');
}
return False;
} }
} }
catch (Exception $e) catch (Exception $e)
@ -165,40 +184,32 @@
throw new ConfigurationException('Version comparison failed: ' . $e->getMessage()); throw new ConfigurationException('Version comparison failed: ' . $e->getMessage());
} }
if(!in_array($this->extension, CompilerExtensions::ALL)) /** @noinspection InArrayMissUseInspection */
{ if(!in_array($this->extension, CompilerExtensions::ALL, true))
if($throw_exception) {
{ throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported');
throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported'); }
}
return False;
}
/** @noinspection DegradedSwitchInspection */
switch($this->extension) switch($this->extension)
{ {
case CompilerExtensions::PHP: case CompilerExtensions::PHP:
if(!in_array($this->maximum_version, CompilerExtensionSupportedVersions::PHP))
if(!in_array($this->maximum_version, CompilerExtensionSupportedVersions::PHP, true))
{ {
if($throw_exception) throw new NotSupportedException('The property "project.compiler.maximum_version" does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension);
{
throw new NotSupportedException('The MaximumVersion does not support version ' . $this->maximum_version . ' for the extension ' . $this->extension);
}
return False;
} }
if(!in_array($this->minimum_version, CompilerExtensionSupportedVersions::PHP)) if(!in_array($this->minimum_version, CompilerExtensionSupportedVersions::PHP, true))
{ {
if($throw_exception) throw new NotSupportedException('The property "project.compiler.minimum_version" does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension);
{
throw new NotSupportedException('The MinimumVersion does not support version ' . $this->minimum_version . ' for the extension ' . $this->extension);
}
return False;
} }
break; break;
default: default:
throw new NotSupportedException('The compiler extension \'' . $this->extension . '\' is not supported'); throw new NotSupportedException('The compiler extension "' . $this->extension . '" is not supported by ncc');
} }
return True; return True;
@ -230,15 +241,21 @@
/** /**
* @inheritDoc * @inheritDoc
* @param array $data
* @return Compiler
* @throws ConfigurationException
* @throws NotSupportedException
*/ */
public static function fromArray(array $data): Compiler public static function fromArray(array $data): Compiler
{ {
$object = new self(); if(Functions::array_bc($data, 'extension') === null)
{
throw new ConfigurationException('The property \'project.compiler.extension\' must not be null.');
}
$object->maximum_version = Functions::array_bc($data, 'maximum_version'); return new self(Functions::array_bc($data, 'extension'),
$object->extension = Functions::array_bc($data, 'extension'); Functions::array_bc($data, 'maximum_version'),
$object->minimum_version = Functions::array_bc($data, 'minimum_version'); Functions::array_bc($data, 'minimum_version')
);
return $object;
} }
} }

View file

@ -53,9 +53,9 @@
/** /**
* Public Constructor * Public Constructor
*/ */
public function __construct() public function __construct(Compiler $compiler)
{ {
$this->compiler = new Compiler(); $this->compiler = $compiler;
$this->options = []; $this->options = [];
} }
@ -129,17 +129,13 @@
/** /**
* Validates the Project object * Validates the Project object
* *
* @param bool $throw_exception
* @return bool * @return bool
* @throws ConfigurationException * @throws ConfigurationException
* @throws NotSupportedException * @throws NotSupportedException
*/ */
public function validate(bool $throw_exception=True): bool public function validate(): bool
{ {
if(!$this->compiler->validate($throw_exception)) $this->compiler->validate();
{
return False;
}
return True; return True;
} }
@ -170,22 +166,28 @@
* *
* @param array $data * @param array $data
* @return Project * @return Project
* @throws ConfigurationException
* @throws NotSupportedException
*/ */
public static function fromArray(array $data): Project public static function fromArray(array $data): Project
{ {
$object = new self();
if(Functions::array_bc($data, 'compiler') !== null) if(Functions::array_bc($data, 'compiler') !== null)
{ {
$object->compiler = Compiler::fromArray(Functions::array_bc($data, 'compiler')); $object = new self(Compiler::fromArray(Functions::array_bc($data, 'compiler')));
} }
else
if(Functions::array_bc($data, 'options') !== null)
{ {
$object->options = Functions::array_bc($data, 'options'); throw new ConfigurationException('The project configuration is missing the required property "compiler" in the project section.');
} }
if(Functions::array_bc($data, 'update_source') !== null) $object->options = Functions::array_bc($data, 'options');
if($object->options === null)
{
$object->options = [];
}
$object->update_source = Functions::array_bc($data, 'update_source');
if($object->update_source !== null)
{ {
$object->update_source = UpdateSource::fromArray(Functions::array_bc($data, 'update_source')); $object->update_source = UpdateSource::fromArray(Functions::array_bc($data, 'update_source'));
} }

View file

@ -0,0 +1,8 @@
{
"project": {
"compiler": {
"extension": "php",
"minimum_version": "8.0"
}
}
}