From 6371f260467a15f6760f98457af2400a28424e6d Mon Sep 17 00:00:00 2001 From: Netkas Date: Tue, 6 Dec 2022 01:23:51 -0500 Subject: [PATCH] Added method array_replace_recursive() & recurse() to the installer which corrects issue #30 see https://git.n64.cc/nosial/ncc/-/issues/30 Updated some default values for ncc.yaml for better security. --- src/config/ncc.yaml | 7 ++-- src/installer/installer | 73 +++++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/config/ncc.yaml b/src/config/ncc.yaml index 0b713ba..f552647 100644 --- a/src/config/ncc.yaml +++ b/src/config/ncc.yaml @@ -23,9 +23,6 @@ php: # Whether to initialize NCC when running `require('ncc');` initialize_on_require: true - # if NCC should handle fatal exceptions during execution - handle_exceptions: true - git: # if git is enabled or not enabled: true @@ -55,10 +52,10 @@ composer: quiet: false # Disable ANSI output - no_ansi: false + no_ansi: true # Do not ask any interactive question - no_interaction: false + no_interaction: true # Display timing and memory usage information profile: false diff --git a/src/installer/installer b/src/installer/installer index 1cc4a21..6cfb6e5 100644 --- a/src/installer/installer +++ b/src/installer/installer @@ -712,7 +712,6 @@ $config_obj['composer']['enable_internal_composer'] = false; if($config_obj['composer']['executable_path'] == null) { - // TODO: Implement Configuration Tools Console::outWarning('Cannot locate the executable path for \'composer\', run \'ncc config --composer.executable_path="composer.phar"\' as root to update the path'); } } @@ -724,7 +723,7 @@ { if ($NCC_FILESYSTEM->exists(PathFinder::getConfigurationFile())) { - $config_backup = IO::fread(PathFinder::getConfigurationFile()); + $config_backup = Yaml::parseFile(PathFinder::getConfigurationFile()); } } catch (Exception $e) @@ -736,33 +735,73 @@ // Create/Update configuration file $config_obj = Yaml::parseFile(__DIR__ . DIRECTORY_SEPARATOR . 'default_config.yaml'); - // Update the old configuration - if($config_backup !== null) + if(!function_exists('array_replace_recursive')) { - $old_config_obj = Yaml::parse($config_backup); - foreach($old_config_obj as $section => $value) + /** + * @param $array + * @param $array1 + * @return array|mixed + * @author + * @noinspection PhpMissingReturnTypeInspection + */ + function array_replace_recursive($array, $array1) { - if(isset($config_obj[$section])) + // handle the arguments, merge one by one + $args = func_get_args(); + $array = $args[0]; + if (!is_array($array)) { - foreach($value as $section_item => $section_value) + return $array; + } + for ($i = 1; $i < count($args); $i++) + { + if (is_array($args[$i])) { - if(!isset($config_obj[$section][$section_item])) - { - $config_obj[$section][$section_item] = $section_value; - } + $array = recurse($array, $args[$i]); } } - else - { - $config_obj[$section] = $value; - } + return $array; } } + if(!function_exists('recurse')) + { + /** + * @param $array + * @param $array1 + * @return mixed + * @author + * @noinspection PhpMissingReturnTypeInspection + */ + function recurse($array, $array1) + { + foreach ($array1 as $key => $value) + { + // create new key in $array, if it is empty or not an array + /** @noinspection PhpConditionAlreadyCheckedInspection */ + if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) + { + $array[$key] = array(); + } + + // overwrite the value in the base array + if (is_array($value)) + { + $value = recurse($array[$key], $value); + } + $array[$key] = $value; + } + return $array; + } + } + + + if($config_backup !== null) + $config_obj = array_replace_recursive($config_obj, $config_backup); + if($config_backup == null) { Console::out('Generating ncc.yaml'); - } else {