diff --git a/src/ncc/Classes/NccExtension/NccCompiler.php b/src/ncc/Classes/NccExtension/NccCompiler.php index 8ccdc45..c079ddc 100644 --- a/src/ncc/Classes/NccExtension/NccCompiler.php +++ b/src/ncc/Classes/NccExtension/NccCompiler.php @@ -333,6 +333,8 @@ $metadata->addOptions($this->project_manager->getProjectConfiguration()->getBuild()->getOptions($build_configuration)); $metadata->addOptions($this->project_manager->getProjectConfiguration()->getProject()->getOptions()); + $metadata->addConstants($this->project_manager->getRuntimeConstants($build_configuration)); + $metadata->addConstants($this->project_manager->getRuntimeConstants()); $metadata->setUpdateSource($this->project_manager->getProjectConfiguration()->getProject()->getUpdateSource()); $metadata->setMainExecutionPolicy($this->project_manager->getProjectConfiguration()->getBuild()->getMain()); $metadata->setInstaller($this->project_manager->getProjectConfiguration()->getInstaller()); @@ -350,6 +352,7 @@ $metadata->removeOption(BuildConfigurationOptions::STATIC_DEPENDENCIES->value); } + /** @noinspection UnusedFunctionResultInspection */ $package_writer->setMetadata($metadata); } diff --git a/src/ncc/Classes/Runtime.php b/src/ncc/Classes/Runtime.php index 6675bca..a324a2d 100644 --- a/src/ncc/Classes/Runtime.php +++ b/src/ncc/Classes/Runtime.php @@ -44,9 +44,11 @@ use ncc\Utilities\Console; use ncc\Utilities\IO; use ncc\Utilities\Resolver; + use ncc\Utilities\RuntimeCache; use ncc\Utilities\Validate; use RuntimeException; use Throwable; + use function trigger_error; class Runtime { @@ -209,6 +211,34 @@ } } + $safe_package_name = strtoupper($entry->getAssembly($version)->getName()); + foreach($entry->getMetadata($version)->getConstants() as $constant => $value) + { + $constant_full_name = sprintf("%s_%s", $safe_package_name, $constant); + + // Skip if already defined. + if(defined($constant_full_name)) + { + if(RuntimeCache::get(sprintf("defined_%s", $constant_full_name))) + { + continue; + } + + trigger_error(sprintf('Cannot define constant %s from package %s because the constant is already defined', $constant_full_name, $package), E_USER_WARNING); + continue; + } + + if(!Validate::constantName($constant_full_name)) + { + // trigger warning only + trigger_error(sprintf('Cannot define constant %s from package %s because the constant name is invalid', $constant_full_name, $package), E_USER_WARNING); + continue; + } + + RuntimeCache::set(sprintf("defined_%s", $constant_full_name), true); + define($constant_full_name, $value); + } + if(isset($entry->getMetadata($version)->getOptions()[PackageFlags::STATIC_DEPENDENCIES->value])) { // Fake import the dependencies diff --git a/src/ncc/Managers/ProjectManager.php b/src/ncc/Managers/ProjectManager.php index 5b7dfe8..58c2b82 100644 --- a/src/ncc/Managers/ProjectManager.php +++ b/src/ncc/Managers/ProjectManager.php @@ -203,11 +203,10 @@ * * @param AuthenticationInterface|null $authentication * @return array Array of installed packages - * @throws OperationException - * @throws IOException * @throws ConfigurationException + * @throws IOException + * @throws OperationException * @throws PathNotFoundException - * @throws NotSupportedException */ public function installDependencies(?AuthenticationInterface $authentication=null): array { @@ -295,7 +294,6 @@ * * @param string $build_configuration * @return array - * @throws NotSupportedException */ public function getComponents(string $build_configuration=BuildConfigurationValues::DEFAULT->value): array { @@ -311,7 +309,6 @@ * * @param string $build_configuration * @return array - * @throws NotSupportedException */ public function getResources(string $build_configuration=BuildConfigurationValues::DEFAULT->value): array { @@ -333,11 +330,7 @@ public function getRuntimeConstants(string $build_configuration=BuildConfigurationValues::DEFAULT->value): array { $configuration = $this->project_configuration->getBuild()->getBuildConfiguration($build_configuration); - - return array_merge( - $configuration->getDefineConstants(), - $this->project_configuration->getBuild()->getDefineConstants() - ); + return array_merge($configuration->getDefineConstants(), $this->project_configuration->getBuild()->getDefineConstants()); } /** diff --git a/src/ncc/Objects/Package/Metadata.php b/src/ncc/Objects/Package/Metadata.php index 1d75e34..95265ea 100644 --- a/src/ncc/Objects/Package/Metadata.php +++ b/src/ncc/Objects/Package/Metadata.php @@ -59,6 +59,11 @@ */ private $main_execution_policy; + /** + * @var array + */ + private $constants; + /** * @var Installer|null */ @@ -74,6 +79,7 @@ { $this->compiler_extension = $compiler; $this->compiler_version = NCC_VERSION_NUMBER; + $this->constants = []; $this->options = []; } @@ -117,6 +123,27 @@ $this->compiler_version = $compiler_version; } + /** + * Returns the constants associated with the class + * + * @return array + */ + public function getConstants(): array + { + return $this->constants; + } + + /** + * Sets an array of constants to be used within the package + * + * @param array $constants + * @return void + */ + public function addConstants(array $constants): void + { + $this->constants = array_merge($this->constants, $constants); + } + /** * Returns an array of options associated with the package * @@ -258,6 +285,7 @@ ($bytecode ? Functions::cbc('update_source') : 'update_source') => ($this->update_source?->toArray($bytecode)), ($bytecode ? Functions::cbc('installer') : 'installer') => ($this->installer?->toArray($bytecode)), ($bytecode ? Functions::cbc('main_execution_policy') : 'main_execution_policy') => $this->main_execution_policy, + ($bytecode ? Functions::cbc('constants') : 'constants') => $this->constants, ($bytecode ? Functions::cbc('options') : 'options') => $this->options, ]; } @@ -284,8 +312,14 @@ $object->options = Functions::array_bc($data, 'options'); $object->update_source = Functions::array_bc($data, 'update_source'); $object->main_execution_policy = Functions::array_bc($data, 'main_execution_policy'); + $object->constants = Functions::array_bc($data, 'constants'); $object->installer = Functions::array_bc($data, 'installer'); + if($object->constants === null) + { + $object->constants = []; + } + if($object->update_source !== null) { $object->update_source = UpdateSource::fromArray($object->update_source);