From 71214e769e053704210bb548c27ddc3c5544688b Mon Sep 17 00:00:00 2001 From: Netkas Date: Thu, 12 Oct 2023 16:24:36 -0400 Subject: [PATCH] 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. --- CHANGELOG.md | 2 + Makefile | 1 - src/installer/generate_build_files.php | 3 +- src/installer/installer | 77 ---------------------- src/ncc/Utilities/Functions.php | 61 +++++++++++++++++ src/{installer => ncc/Utilities}/extension | 0 6 files changed, 64 insertions(+), 80 deletions(-) rename src/{installer => ncc/Utilities}/extension (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd29000..248055a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Updated fetchPackage method to include authentication - Update dependencies import in \ncc\Classes > Runtime > importFromPackage() + - Refactor ncc extension registration logic, this also allows for debian packages to install the ncc extension + automatically. ### Fixed - Fixed issue where some build artifacts are being bundled with package builds such as `output_path` and `static` diff --git a/Makefile b/Makefile index cb2a598..fe96810 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,6 @@ redist: autoload cp -f $(CONFIG_PATH)/ncc.yaml $(GENERIC_BUILD_PATH)/CLI/template_config.yaml cp -f $(CONFIG_PATH)/default_repositories.json $(GENERIC_BUILD_PATH)/default_repositories.json cp -f $(INSTALLER_SRC_PATH)/ncc-package.xml $(GENERIC_BUILD_PATH)/ncc-package.xml - cp -f $(INSTALLER_SRC_PATH)/extension $(GENERIC_BUILD_PATH)/extension chmod +x $(GENERIC_BUILD_PATH)/INSTALL cp -f LICENSE $(GENERIC_BUILD_PATH)/LICENSE cp -f README.md $(GENERIC_BUILD_PATH)/README.md diff --git a/src/installer/generate_build_files.php b/src/installer/generate_build_files.php index 4a64575..e74b496 100644 --- a/src/installer/generate_build_files.php +++ b/src/installer/generate_build_files.php @@ -41,8 +41,7 @@ 'installer', 'checksum.bin', 'build_files', - 'ncc.sh', - 'extension' + 'ncc.sh' ]; ncc\Utilities\Console::out('Creating build_files ...'); diff --git a/src/installer/installer b/src/installer/installer index c40f271..b159761 100644 --- a/src/installer/installer +++ b/src/installer/installer @@ -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; diff --git a/src/ncc/Utilities/Functions.php b/src/ncc/Utilities/Functions.php index 0219a5e..63df275 100644 --- a/src/ncc/Utilities/Functions.php +++ b/src/ncc/Utilities/Functions.php @@ -28,6 +28,7 @@ use ncc\Enums\Scopes; use ncc\Enums\Versions; use ncc\Exceptions\IOException; + use ncc\Exceptions\NotSupportedException; use ncc\Exceptions\OperationException; use ncc\Exceptions\PathNotFoundException; use ncc\Managers\ConfigurationManager; @@ -338,6 +339,66 @@ { throw new OperationException('Failed to initialize repository database, ' . $e->getMessage(), $e); } + + try + { + self::registerExtension($filesystem); + } + catch(Exception $e) + { + throw new OperationException('Failed to register ncc extension, ' . $e->getMessage(), $e); + } + } + + /** + * Register the ncc extension with the given filesystem. + * + * @param Filesystem $filesystem The filesystem object used for file operations. + * @throws IOException If the extension cannot be registered. + * @throws NotSupportedException If `get_include_path()` function is not available. + * @throws PathNotFoundException If the default include path is not available. + */ + private static function registerExtension(Filesystem $filesystem): void + { + if(!function_exists('get_include_path')) + { + throw new NotSupportedException('Cannot register ncc extension, get_include_path() is not available'); + } + + $default_share = DIRECTORY_SEPARATOR . 'usr' . DIRECTORY_SEPARATOR . 'share' . DIRECTORY_SEPARATOR . 'php'; + $include_paths = explode(':', get_include_path()); + $extension = str_ireplace('%ncc_install', NCC_EXEC_LOCATION, IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'extension')); + + if(in_array($default_share, $include_paths)) + { + if($filesystem->exists($default_share . DIRECTORY_SEPARATOR . 'ncc')) + { + $filesystem->remove($default_share . DIRECTORY_SEPARATOR . 'ncc'); + } + + IO::fwrite($default_share . DIRECTORY_SEPARATOR . 'ncc', $extension); + return; + } + + foreach($include_paths as $include_path) + { + try + { + if($filesystem->exists($include_path . DIRECTORY_SEPARATOR . 'ncc')) + { + $filesystem->remove($include_path . DIRECTORY_SEPARATOR . 'ncc'); + } + + IO::fwrite($include_path . DIRECTORY_SEPARATOR . 'ncc', $extension); + return; + } + catch(IOException $e) + { + Console::outWarning(sprintf('Failed to register ncc extension in %s: %s', $include_path, $e->getMessage())); + } + } + + throw new IOException('Cannot register ncc extension, no include path is available'); } /** diff --git a/src/installer/extension b/src/ncc/Utilities/extension similarity index 100% rename from src/installer/extension rename to src/ncc/Utilities/extension