Refactor ncc extension registration logic, this also allows for debian packages to install the ncc extension automatically.

The logic related to the registration of the ncc extension has been refactored. Previously, the code that registered the ncc extension was contained in the installer. However, this change moves the code that registers the ncc extension from the installer to the Utilities/Functions class. This change was made because the extension registration is not only applicable to the installer, but is also required for several other components. This fact justified the need for the logic to be located in a more generic and accessible class. The Makefile, installer, generate_build_files.php files have been updated to reflect this change. Components using these should now work properly with the changes.
This commit is contained in:
Netkas 2023-10-12 16:24:36 -04:00
parent 89b0c931b9
commit 71214e769e
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
6 changed files with 64 additions and 80 deletions

View file

@ -1,81 +0,0 @@
<?php
use ncc\Classes\Runtime;
use ncc\Enums\Versions;
use ncc\Exceptions\ConfigurationException;
use ncc\ncc;
if(!defined('NCC_INIT'))
{
if(!file_exists('%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php'))
{
throw new RuntimeException('Cannot locate file \'%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php\'');
}
require('%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php');
if(!function_exists('import'))
{
/**
* Attempts to import a package into the current runtime, returns the imported package name
*
* @param string $package
* @param string $version
* @param array $options
* @return string
*/
function import(string $package, string $version=Versions::LATEST, array $options=[]): string
{
try
{
return Runtime::import($package, $version, $options);
}
catch(Exception $e)
{
throw new RuntimeException(sprintf('Unable to import package \'%s\': %s', $package, $e->getMessage()), $e->getCode(), $e);
}
}
}
if(!function_exists('execute'))
{
/**
* Executes the main execution point of an imported package and returns the evaluated result
* This method may exit the program without returning a value
*
* @param string $package
* @return mixed
* @throws ConfigurationException
*/
function execute(string $package): mixed
{
return Runtime::execute($package);
}
}
if(!function_exists('get_imported'))
{
/**
* Returns an array of all imported packages
*
* @return array
*/
function get_imported(): array
{
return Runtime::getImportedPackages();
}
}
if(!function_exists('ncc_constants'))
{
/**
* Returns an array of constants defined by NCC
*
* @return array
*/
function ncc_constants(): array
{
return ncc::getConstants();
}
}
}

View file

@ -41,8 +41,7 @@
'installer',
'checksum.bin',
'build_files',
'ncc.sh',
'extension'
'ncc.sh'
];
ncc\Utilities\Console::out('Creating build_files ...');

View file

@ -345,83 +345,6 @@
}
}
// Register the ncc extension
try
{
Console::out('Registering extension');
$extension_shortcut = str_ireplace('%ncc_install', $NCC_INSTALL_PATH, IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'extension'));
}
catch(Exception $e)
{
Console::outException(sprintf('Failed to read file \'%s\', %s', __DIR__ . DIRECTORY_SEPARATOR . 'extension', $e->getMessage()), $e, 1);
return;
}
// Remove all the old extensions first.
/**
* @param string $path
* @param Filesystem $filesystem
* @param string $extension_shortcut
* @return bool
*/
function install_extension(string $path, Filesystem $filesystem, string $extension_shortcut): bool
{
if ($filesystem->exists($path . DIRECTORY_SEPARATOR . 'ncc'))
{
$filesystem->remove($path . DIRECTORY_SEPARATOR . 'ncc');
}
try
{
IO::fwrite($path . DIRECTORY_SEPARATOR . 'ncc', $extension_shortcut);
}
catch (\ncc\Exceptions\IOException $e)
{
Console::outException($e->getMessage(), $e, 1);
return false;
}
return $filesystem->exists($path . DIRECTORY_SEPARATOR . 'ncc');
}
if(function_exists('get_include_path'))
{
$default_share = DIRECTORY_SEPARATOR . 'usr' . DIRECTORY_SEPARATOR . 'share' . DIRECTORY_SEPARATOR . 'php';
$include_paths = explode(':', get_include_path());
$extension_registered = false;
if(!in_array($default_share, $include_paths))
{
foreach($include_paths as $path)
{
if($extension_registered)
{
break;
}
switch($path)
{
// Ignore local files
case '.':
case '..':
break;
// First real include path is /usr/share/php
default:
// Install the extension
$extension_registered = install_extension($path, $NCC_FILESYSTEM, $extension_shortcut);
break;
}
}
}
else
{
// Remove the old extension
install_extension($default_share, $NCC_FILESYSTEM, $extension_shortcut);
}
}
// Backup the configuration file
$config_backup = null;