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

feat: use typo3 core YamlFileLoader #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tbal
Copy link

@tbal tbal commented Sep 25, 2024

This way imports in sites config yaml files are processed.

Also extra code for parsing and processing of env vars (%env(MY_ENV)%) is removed, while already implemented by YamlFileLoader.

All tests and qa done and succeed for T3 v12 + v13.3.

Background
Usually our config/sites/*/config.yaml import application context dependent configuration:

imports:
  - { resource: 'Context/%env(TYPO3_CONTEXT)%/config.yaml' }

Contexts (e.g. Production, Staging, Development) can have different values for matomoWidgets* settings (mostly matomoWidgetsUrl and matomoWidgetsIdSite).
The previously used Symfony Yaml Parser didn't processed imports.

This way imports in sites config yaml files are processed.
Comment on lines +40 to +46
GeneralUtility::addInstance(
YamlFileLoader::class,
GeneralUtility::makeInstance(
YamlFileLoader::class,
$this->createMock(Logger::class),
),
);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brotkrueml This may could be done more elegant..
I got errors when running the phpunit test because of missing argument (Logger) for YamlFileLoader class (is instantiated in ConfigurationFinder.php L148. I was struggeling with the dependency injection of the LoggerAwareInterface for the YamlFileLoader class here and that was the solution i found which satisfied the test cases :)

@brotkrueml
Copy link
Owner

@tbal Thanks for the PR, will have a look soon. I think, I considered that, but ran into problems, but was long time ago. Maybe TYPO3 v10/v11.

@brotkrueml brotkrueml added documentation Improvements or additions to documentation feature New feature or request labels Sep 25, 2024

private static function getYamlFileLoader(): YamlFileLoader
{
self::$yamlFileLoader ??= GeneralUtility::makeInstance(YamlFileLoader::class);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I shy away from using GeneralUtility::makeInstance() at this point: when the ConfigurationFinder is utilized, there is no dependency injection container available. This might work now (haven't tested it), but won't bet on it for future TYPO3 versions. Better would be to instantiate manually:

new YamlFileLoader(new NullLogger());

But: as TYPO3 uses here dependency injection already for the logger, this might cause problems in future versions, when other dependencies might be injected. Then this may result in removing this feature and using the plain Symfony YamlFileLoader again.

Another possibility will be to copy the import functionality over to this extension (or the whole class without the logging).

What do you think? @tbal

@brotkrueml brotkrueml added the question Further information is requested label Sep 27, 2024
@brotkrueml
Copy link
Owner

Contexts (e.g. Production, Staging, Development) can have different values for matomoWidgets* settings (mostly matomoWidgetsUrl and matomoWidgetsIdSite). The previously used Symfony Yaml Parser didn't processed imports.

As I have concerns about using TYPO3's YamlFileLoader: You can also use environment variables directly for the various configuration options (like auth token and site id). But I assume, you know that?

@brotkrueml
Copy link
Owner

Okay, thought about it again: We can use TYPO3's YamlFileLoader. If something changes there (like more classes injected), the functionality can be moved into the extension itself (like currently with the environment variables handling). But I do not want to use GeneralUtility::makeInstance(), instead instantiate it manually.

I am not sure if we have the correct logging available at compile time, if not, I see two possibilities:

  • Use the NullLogger: with the drawback that if an error occurs, this vanishes
  • Implement a custom logger which collects errors. If errors occur, throw an exception which prohibits the compilation of the DI container.

Currently, I am slightly in favour of the custom logger (base on the PSR-3 AbstractLogger). What do you think, @tbal?

@tbal
Copy link
Author

tbal commented Oct 2, 2024

Basically looks like doing it this way again cb4f841 ?! 😄

@brotkrueml
Copy link
Owner

Basically looks like doing it this way again cb4f841 ?! 😄

That time ago I was young and unknowing.

@brotkrueml
Copy link
Owner

@tbal I implemented a slightly different solution with acc3183. As it works with imports, I get an error when saving the site configuration (TYPO3 v12):

Introducing placeholder %env(EXT_MATOMOWIDGETS_TOKENAUTH)% for matomoWidgetsTokenAuth is not allowed

The matomoWidgetsTokenAuth is defined with an env variable in an import file. Have you experienced a similar behaviour?

@tbal
Copy link
Author

tbal commented Oct 9, 2024

@tbal I implemented a slightly different solution with acc3183.

LGTM. Even though it feels a little "unhealthy" using a NullLogger ;)
I think a small change is missing, see: acc3183#r147730878 (I added a comment in line 30, which is visually hard to find)

As it works with imports, I get an error when saving the site configuration (TYPO3 v12):

Introducing placeholder %env(EXT_MATOMOWIDGETS_TOKENAUTH)% for matomoWidgetsTokenAuth is not allowed

The matomoWidgetsTokenAuth is defined with an env variable in an import file. Have you experienced a similar behaviour?

This error seems to be a security feature :) See: https://github.com/TYPO3/typo3/blob/12.4/typo3/sysext/core/Classes/Configuration/Loader/YamlPlaceholderGuard.php#L89
There is no error if the value was already set (through yaml config) and saved without changed in the backend sites config.
So that should be fine.

@brotkrueml
Copy link
Owner

@tbal Finished now the feature. Would be nice if you can test it for your purpose and give feedback. Use the branch use-typo3-yamlfileloader. Will then merge to main and release a new version. Thanks 👍

@brotkrueml brotkrueml removed the question Further information is requested label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants