Welcome Guest, Not a member yet? Register   Sign In
Registrar: Merge Config arrays
#1

(This post was last modified: 09-30-2024, 11:12 AM by ozornick.)

The current behavior looks incorrect.
If I want to add Filters, supportedLocales, for example, then my settings will overwrite the default data. But it would be logical to combine them.

PHP Code:
// before, globals filters
array(2) {
  ["before"]=>
  array(3) {
    [0]=>
    string(8"honeypot"
    [1]=>
    string(4"csrf"
    [2]=>
    string(12"invalidchars"
  }
  ["after"]=>
  array(1) {
    [0]=>
    string(13"secureheaders"
  }
}

// after Registrar
array(2) {
  ["before"]=>
  array(1) {
    [0]=>
    string(23"some_filter"
  }
  ["after"]=>
  array(0) {
  }

// Expected
array(2) {
  ["before"]=>
  array(4) {
    [0]=>
    string(8"honeypot"
    [1]=>
    string(4"csrf"
    [2]=>
    string(12"invalidchars"
    [3]=>
    string(23"some_filter"
  }
  ["after"]=>
  array(1) {
    [0]=>
    string(13"secureheaders"
  }

Am I wrong? Of course, you can manually combine the previous values with the new ones, but it doesn't look. good. It will probably cause recursion.
Should I change it to array_merge_recursive()

Otherwise, you will have to duplicate all the values manually. This is bad for a modular system where each component changes arrays.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#2

(This post was last modified: 10-14-2024, 06:46 AM by ozornick.)

Do you have an opinion on this? I thought that sometimes a merge is not required and a replacement is needed. It may be worth adding a flag to overwrite
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 10-19-2024, 06:28 AM by Chivinsdev.)

Yes i used array_merge_recursive. I discovered the issue to so i extend the base config to child class today fix this but i think the usage of array_merge_recursive is a better option @kenjis help on this to update the core code

`\Codeigniter\Config\BaseConfig`
PHP Code:
    /**
    * Provides external libraries a simple way to register one or more
    * options into a config file.
    *
    * @return void
    *
    * @throws ReflectionException
    */
    protected function registerProperties()
    {
        if (! static::$moduleConfig->shouldDiscover('registrars')) {
            return;
        }

        if (! static::$didDiscovery) {
            $locator        service('locator');
            $registrarsFiles $locator->search('Config/Registrar.php');

            foreach ($registrarsFiles as $file) {
                $className $locator->findQualifiedNameFromPath($file);

                if ($className === false) {
                    continue;
                }

                static::$registrars[] = new $className();
            }

            static::$didDiscovery true;
        }
 
        $shortName = (new ReflectionClass($this))->getShortName();

        // Check the registrar class for a method named after this class' shortName
        foreach (static::$registrars as $callable) {
            // ignore non-applicable registrars
            if (! method_exists($callable$shortName)) {
                continue; // @codeCoverageIgnore
            }

            $properties $callable::$shortName();

            if (! is_array($properties)) {
                throw new RuntimeException('Registrars must return an array of properties and their values.');
            }

            foreach ($properties as $property => $value) {
                if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
                    $this->{$property} = array_merge_recursive($this->{$property}, $value);
                    d($this->{$property});
                } else {
                    $this->{$property} = $value;
                }
            }
        }
    


Reply
#4

This is not a complete solution. For example, for an array of App $supportedLocales, Pager  $templates, adding is a good way. There are no Filters.
It may be worth passing the current values of $this (for reading) to the Registrar
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB