framework icon indicating copy to clipboard operation
framework copied to clipboard

[Request] Allow YAML configuration

Open ibrasho opened this issue 12 years ago • 18 comments

I was wondering if implementing native YAML support is considered?

I'm almost done implementing it myself, and it looked super easy to do give native support for that, without effecting current installations.

The major thing would be how to convert the current configuration to yaml, and even this is not very hard.

I'll try to finish my implementation and then post a RP for reviews and see where this could go.

ibrasho avatar Jun 16 '14 13:06 ibrasho

Are there any benefit when using YAML syntax for configuration files?

letrunghieu avatar Jun 16 '14 15:06 letrunghieu

I doubt this will be added to the framework, as it would add a bit of complexity with probably not enough benefits. It may be possible to make a package to do this though. I haven't looked at the config code much, so it might be able to be extracted out into a package.

RyanNielson avatar Jun 16 '14 16:06 RyanNielson

Are there any benefit when using YAML syntax for configuration files?

Not really no, clarity maybe but that's highly debatable. I'd go for PHP files 10 times out of 10.

Anahkiasen avatar Jun 16 '14 16:06 Anahkiasen

@ibrasho I'd recommend just implementing this yourself. I think this has already been requested for laravel and rejected.

GrahamCampbell avatar Jun 16 '14 16:06 GrahamCampbell

Didn't find anything related to YAML support here, and that's why I posted the issue.

One problem for me is that the config loader is hard-coded, I can't change it without changing something in Laravel, specifically Illuminate\Foundation\start.php file.

ibrasho avatar Jun 16 '14 17:06 ibrasho

@letrunghieu Yaml files are more readable than PHP arrays. One can argue that PHP files are more secure.

In the end, I just see it as a matter of personal preference.

ibrasho avatar Jun 16 '14 17:06 ibrasho

Maybe a better approach is changing Laravel itself to make the config loader more replaceable so people can choose to use things like YAML, JSON, PHP, etc for their config loading. I'd probably just stick with php because its the way the framework expects, but I can't see making it more pluggable to be a bad thing.

RyanNielson avatar Jun 16 '14 17:06 RyanNielson

@ibrasho You can actually change the config loader without modifying anything in the core. It certainly isn't ideal, but when it was brought up to make it easier, the request was rejected. I got it working with two steps.

First, you need to bind your own implementation to config.loader by doing something along the lines of the below code in the app/start/start.php file:

$app->bind('config.loader', function($app)
{
    // Get the filesystem
    $filesystem = new Illuminate\Filesystem\Filesystem;
    // Get the cache
    $cache = new Illuminate\Cache\FileStore($filesystem, $app['path.storage'].'/cache');
    return new MyConfigLoader($filesystem, $cache, false);
    
}, true);

Second, you'll need to extend the Illuminate Application class and override the getConfigLoader method. In mine, I just have it return $this->make('config.loader') instead of building the loader like the Illuminate Application class does. (Check the class out to see what I'm talking about.)

This whole thing definitely feels pretty hacky, but that's how I've gotten it to work to this point. I'd love for it to be a little more elegant and baked into the core, but until Taylor opens it up a little more, that's how you'll have to do it.

agentphoenix avatar Jun 16 '14 18:06 agentphoenix

Currently, there are 2 ways to alter the ConfigLoader instantiation:

  1. Extends Application and edit the getConfigLoader() method.
  2. Edit the instantiation in: Illuminate\Foundation\start.php.

I started by doing the same thing you proposed, but as you said, it just didn't feel elegant enough. So I started to tweak with the implementation of the framework to see how to make it easier.

I would like to hear Taylor's point of view on this. Does hehe think it's not worth it? If so, why?

ibrasho avatar Jun 16 '14 18:06 ibrasho

Latest commit is how I see this implemented. Now one can edit the instantiation of Illuminate\Foundation\Application in bootstrap/start.php like the following:

$app = new Illuminate\Foundation\Application(null, 'array');

or

$app = new Illuminate\Foundation\Application(null, 'yaml');

It's also backward compatible and shouldn't break anything.

ibrasho avatar Jun 16 '14 18:06 ibrasho

That doesn't feel terribly elegant either. There's no context as to what that parameter does and it refers to a specific part of the application (config loading), not the whole application.

What makes more sense to me is having a setConfigLoader method on the Application class so that it can be called in the start file and done in one step.

agentphoenix avatar Jun 16 '14 18:06 agentphoenix

I agree with phoenix, that would be the ideal solution.

riotCode avatar Jun 16 '14 18:06 riotCode

So the plan is to make it possible to swap the default php/array config with yaml or everything else and add the same time make it unusable with any package that support configuration option.

crynobone avatar Jun 16 '14 18:06 crynobone

@agentphoenix I agree with the fact it's not elegant enough.. But the setConfigLoader() method might not be the right solution. Assume that between instantiating the application and calling the method some configuration value was needed? How would the Application behave? I think it must be in the Application constructer, to be available from the start.

@crynobone You don't expect current packages to support a unimplemented configuration method from the start, do you?

Mainly, this would allow the use of whatever method of configuration the application want without effecting the current packages.

See #4730 for example, If one wants to load INI config files, they can do the following in a service provider:

$this->instance('config.ini', new ConfigManager($this->app, 'ini'));

And app('config.ini') should return this instance.

This is not the best use-case of course, as I don't think mixing file types is a good idea, but it shows what's possible. :smile:

ibrasho avatar Jun 17 '14 05:06 ibrasho

You don't expect current packages to support a unimplemented configuration method from the start, do you?

If you're planning to use $app = new Illuminate\Foundation\Application(null, 'yaml'); instead of creating a different service locator such as config.yaml then this would make it unusable, Laravel config implementation is not similar to blade/view where we can detect based on file name extension and resolve it automatically.

crynobone avatar Jun 17 '14 08:06 crynobone

@crynobone The parameter passed to the constructer is not the file extension, but the name of the loader to user, check ConfigManager to see it in action.

In case you were talking about the ability to use multiple configuration methods at the same time and give Laravel the ability to resolve all config file regardless of the type, I'm not sure that's a totally bad idea but I think it need to executed well.

I'm not sure I got what you meant so correct me if I'm mistaken.

ibrasho avatar Jun 17 '14 22:06 ibrasho

I would prefer to leave this to packages and such if possible. I think I plan to stick with PHP for configuration for now. Thanks for the suggestion.

taylorotwell avatar Jun 18 '14 03:06 taylorotwell

There is a package for this already and works nicely. It is basically extending the Application and then FileLoader to do the trick. https://github.com/Devitek/laravel-yaml-configuration

He also created a package for loading yaml translation files as well https://github.com/Devitek/laravel-yaml-translation

unitedworx avatar Jul 14 '14 08:07 unitedworx