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

One line support for other formats (customFileFormat) #29

Open
xy2z opened this issue Sep 23, 2023 · 0 comments
Open

One line support for other formats (customFileFormat) #29

xy2z opened this issue Sep 23, 2023 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@xy2z
Copy link
Owner

xy2z commented Sep 23, 2023

The Problem

Right now to load yaml or other filetypes other than the standard formats (ini, php, json) the user would have to make a class that extends the LiteConfig class and make a custom_handler method (as shown in the README).

However, that is a lot of "work" and code to just load yaml files.

The Solution

It could be solved using a single line like this: (first arg $extensions can be string|array)

LiteConfig::customFileFormat(['yml', 'yaml'], fn($path) => Yaml::parseFile($path));

A complete example would look like:

require 'path/to/vendor/autoload.php';

use xy2z\LiteConfig\LiteConfig;

LiteConfig::customFileFormat(['yml', 'yaml'], fn($path) => Yaml::parseFile($path));
LiteConfig::customFileFormat('neon', fn($path) => Neon::decodeFile($path));

LiteConfig::loadDir(__DIR__ . '/config', true); // contains settings.yml file.

echo LiteConfig::get('settings.app_name');

Todo

Make a new method in LiteConfig called custom_file_format() that takes an array|string as first argument, and a callback as second argument. It should just be used in getFileContent().

Remember to make a phpunit tests for this, just with the yaml is fine. You can install the symfony/yaml package with dev flag.
And also update the README.md.

The current "custom_handler" functionality should not be touched, it should still work.

Please ask if you have any questions.


🎃 For more PHP hacktoberfest issues, see:
https://github.com/search?q=owner%3Axy2z+label%3Ahacktoberfest+is%3Aopen&type=issues&s=comments&o=asc

@xy2z xy2z changed the title Custom handler method Custom handler method callback v2 Sep 23, 2023
@xy2z xy2z added help wanted Extra attention is needed hacktoberfest labels Sep 26, 2023
@xy2z xy2z changed the title Custom handler method callback v2 One line support for other formats (customFileFormat) Sep 26, 2023
@xy2z xy2z removed the hacktoberfest label Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant