Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Fix/environment loader #4617

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

pquerner
Copy link
Contributor

@pquerner pquerner commented Feb 8, 2025

Description (*)

This PR attempts to fix the incomplete attempt for config values to be overwritten from ENV variables.

Related Pull Requests

Fixed Issues (if relevant)

Relates to issue #4257 (comment)

Manual testing scenarios (*)

  1. Variables from the ENV should now be disabled for editing on the backend, but still shown.
  2. Variables from the ENV should be available from Mage::getStoreConfig.

Automated tests:

  1. ddev phpunit --group Mage_Core_EnvLoader

Questions or comments

Please give feedback to this WIP solution.
Maybe in the future we can add a reasoning of "why" the field in the backend is disabled for edits. Such as give a "warning" sign and show that this field is overwritten by a environment variable.
I feel that this is out of scope for now, for I think that the feature should first work the best it can.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

@github-actions github-actions bot added Component: Core Relates to Mage_Core Component: Adminhtml Relates to Mage_Adminhtml labels Feb 8, 2025
@pquerner
Copy link
Contributor Author

pquerner commented Feb 8, 2025

@sreichel Please see this PR.
(From your comment on #4257 (comment))

@sreichel
Copy link
Contributor

sreichel commented Feb 8, 2025

Quick test. 404 one every page.

@sreichel
Copy link
Contributor

sreichel commented Feb 8, 2025

Maybe it come from invalid setting .... ?

web_environment:
    - MAGE_IS_DEVELOPER_MODE=1
    - OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME=ENV default
    - OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO-BAR__NAME=ENV default dashes
    - OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO_BAR__NAME=ENV default underscore
    - OPENMAGE_CONFIG__DEFAULT__GENERAL__ST=ENV default invalid
    - OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__STORE_INFORMATION__NAME=ENV website
    - OPENMAGE_CONFIG__WEBSITES__BASE-AT__GENERAL__STORE_INFORMATION__NAME=ENV website dashes
    - OPENMAGE_CONFIG__WEBSITES__BASE_CH__GENERAL__STORE_INFORMATION__NAME=ENV website underscore
    - OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__ST=ENV website invalid
    - OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME=ENV store
    - OPENMAGE_CONFIG__STORES__GERMAN-AT__GENERAL__STORE_INFORMATION__NAME=ENV store dashes
    - OPENMAGE_CONFIG__STORES__GERMAN_CH__GENERAL__STORE_INFORMATION__NAME=ENV store underscore
    - OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__ST=ENV store invalid

@pquerner
Copy link
Contributor Author

pquerner commented Feb 9, 2025

For reference my old config:

#web_environment: [
#    "OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME=asd_env",
#    "OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__STORE_INFORMATION__NAME=website_env",
#    "OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME=store_german_env"
#]

With yours I am getting infinite loop.
infinteloop.html.zip

@pquerner
Copy link
Contributor Author

pquerner commented Feb 9, 2025

The error was caused by the Mage::logException call - which finds the file via getConfigStore - which creates an infinite loop.
I've added Mage::registry checks, so that these methods dont run more than needed and removed the logException calls to counter this error.

via 1f6b7ed

@sreichel
Copy link
Contributor

sreichel commented Feb 10, 2025

Thanks for working on it.

Cant make a PR to your repo right now ... how about to filter env vars in getEnv()?

    public function getEnv(): array
    {
        if (is_null($this->envStore)) {
            $env = getenv();
            $env = array_filter($env, function ($key) {
                return str_starts_with($key, self::ENV_STARTS_WITH);
            }, ARRAY_FILTER_USE_KEY);

            $this->envStore = $env;
        }
        return $this->envStore;
    }

Maybe add an additional check for env OPENMAGE_CONFIG_OVERRIDE_ALLOWED to enable/disable that feature?

@sreichel
Copy link
Contributor

sreichel commented Feb 10, 2025

  1. Variables from the ENV should now be disabled for editing on the backend, but still shown.

Yes, on default scope, but not on one website/storeview level.

(Scope label should ben changed to "ENV")

@sreichel
Copy link
Contributor

Note ... Tests should be updated.

@pquerner
Copy link
Contributor Author

pquerner commented Feb 17, 2025

  1. Variables from the ENV should now be disabled for editing on the backend, but still shown.

Yes, on default scope, but not on one website/storeview level.

(Scope label should ben changed to "ENV")

What do you mean by that?

image

You want "Store Name" to be "ENV Store Name"?

All your other suggestions have been added.

Thanks. :)

//edit
To enable this, you would need this now:

web_environment:
// ...
    - OPENMAGE_CONFIG_OVERRIDE_ALLOWED=1

@sreichel
Copy link
Contributor

What do you mean by that?

In "Default" scope the attribute is locked. For "German" storeview its not.

You want "Store Name" to be "ENV Store Name"?

No, not that other "label". :) "[STORE VIEW]" could change to "[ENV]" and the checkbox can be removed.

I am busy for the next 2-3 days. I'll test ASAP.

@pquerner
Copy link
Contributor Author

In "Default" scope the attribute is locked. For "German" storeview its not.

Hm. For me its locked.

No, not that other "label". :) "[STORE VIEW]" could change to "[ENV]" and the checkbox can be removed.

Gotcha. Good point.

@sreichel
Copy link
Contributor

Hm. For me its locked.

Not sure about it, sorry. Maybe the disabled checkbox was missleading. (?)

@pquerner
Copy link
Contributor Author

Its now this:

image

@sreichel
Copy link
Contributor

sreichel commented Feb 17, 2025

Great! ❤️

I can make a PR later, but if you have time for it, please check the red SonarCloud issues.

@pquerner
Copy link
Contributor Author

Great! ❤️

I can do it later, but if you have time for it, please check the red SonarCloud issues.

I've removed one case.
The last ones are: unused variable (from the list) and the use of Reflection class.

I'm not sure how else to get there (without makeing the cache array public?)

@sreichel
Copy link
Contributor

To pass the complexity-check it should be enough to move try/catch-parts to a seperate method.

Leave it as it is, i'll add a PR later.

PS: please composer run php-cs-fixer:fix .... :P

@pquerner
Copy link
Contributor Author

ddev php-cs-fixer
Loaded config default from "/var/www/html/.php-cs-fixer.dist.php".

In RuleSets.php line 65:
                                    
  Set "@PER-CS2.0" does not exist.

:/

@sreichel
Copy link
Contributor

sreichel commented Feb 18, 2025

Yes, unless you can think of some more tests maybe?

Hehe, that may be the only thing ... have you tested code-coverage for all new methods?

I think a automated test for getStoreConfig couldnt hurt, although I am not sure how yet.

Idea only ....

  • add ENV vars to phpunit config
  • set OVERRIDE_ALLOWED in test via putenv()
  • assert ENV-value

Will test as soon i am back.

@sreichel sreichel modified the milestone: 20.13.0 Feb 18, 2025
Pascal Querner and others added 22 commits February 22, 2025 18:18
@pquerner pquerner force-pushed the fix/environmentLoader branch from ed1bd01 to de18a5d Compare February 22, 2025 17:18
@pquerner pquerner marked this pull request as ready for review February 22, 2025 17:18
@sreichel sreichel added this to the 20.14.0 milestone Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Adminhtml Relates to Mage_Adminhtml Component: Core Relates to Mage_Core new feature phpunit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants