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

Simplify configuration registration #21173

Closed
theofidry opened this issue Jan 6, 2017 · 12 comments
Closed

Simplify configuration registration #21173

theofidry opened this issue Jan 6, 2017 · 12 comments
Labels
Config Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed)

Comments

@theofidry
Copy link
Contributor

theofidry commented Jan 6, 2017

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version master

I guess it's not everyone, but I personally use a lot of configuration files. I like to split them when it makes sense and avoid having config files that are too big. On my last 3 good sized projects (not all from me actually) the average was ~50 files.

I personally find very cumbersome to have to manually include them via config*.yml files, so I've done a small locator which register my config files automatically. It works as follow:

  • scan the config dir to get all the files there
  • filter files: some should not be included at all, e.g. routing and parameters.dist and other are environment specific
  • load each of those files

So you basically have the following:

app/config/
    parameters.yml
    parameters.yml.dist  # ignored (like any `*.dist`)
    routing.yml          # ignored
    security.yml         # included for any environment
    security_prod.yml    # included only for the env `prod`
    framework.yml
    framework_prod.yml
    ...
    services/
        foo.yml
        foo_dev.yml
        foo_prod.yml

I would like to suggest to have that feature directly in the core (I can work on it), WDYT?

/cc DX master @weaverryan

@javiereguiluz
Copy link
Member

In 2.8 we introduced a feature to load recursively all config files in directories. Would that help you?

    # Before Symfony 2.8
    imports:
        - { resource: acme/parameters.yml }
        - { resource: acme/security.yml }
        - { resource: acme/services.yml }

    # In Symfony 2.8
    imports:
        - { resource: acme/ }

See http://symfony.com/blog/new-in-symfony-2-8-dx-improvements#recursive-directory-loading-for-configuration-and-routing

@theofidry
Copy link
Contributor Author

@javiereguiluz interesting feature I didn't know about it! But nope, there is two issues:

  • You would need to recursively include config itself, I've just putted security.yml and framework.yml but I have a lot more at the root
  • Files need to be included only for the right environment if they follow the _<envname>.yml pattern

@javiereguiluz
Copy link
Member

javiereguiluz commented Jan 6, 2017

You would need to recursively include config itself, I've just putted security.yml and framework.yml but I have a lot more at the root

Move the files to load automatically to a dedicated folder inside app/config/ ?

Files need to be included only for the right environment if they follow the _.yml pattern

Change your strategy and use <envname>/filename.yml instead of filename_<envname>.yml ?

@theofidry
Copy link
Contributor Author

@javiereguiluz it's doable, but I'm not really fond of it, it requires too much tweaking with conventions I'm not happy with where I would like it to "just works". If you have:

config/
  infrastructure/
    doctrine/
      repositories.yml
  prod/
    infrastructure/
      doctrine/
        repositories.yml

This looks really annoying to me, I want repositories.yml and repositories_prod.yml next to each other.

But I understand it may be too opinionated to be integrated in the core. I'm just making the suggestion and see what people think of it.

@javiereguiluz
Copy link
Member

@theofidry I totally understand you. It's doable ... but annoying. Let's wait for more opinions!

@sstok
Copy link
Contributor

sstok commented Jan 6, 2017

What if we add Glob support for the directory loader? What if we add a Glob file loader for directory loading?

- { resource: "acme/{framework,security}_%kernel.environment%.yml" }
- { resource: "acme/**_%kernel.environment%.yml" }

You can detect if it's a Glob because there are no filenames with {} (actually still possible as filename? I used this back in 2001 :trollface: ) or *

@linaori
Copy link
Contributor

linaori commented Jan 6, 2017

You could make a convention that if the file name (minus .yml) matches the extension name, it would be loaded as such. Additional check: only actually load it if the first key in yaml also matches.

@theofidry
Copy link
Contributor Author

theofidry commented Jan 6, 2017

@iltar not sure to have understood what you mean, could you elaborate?

@linaori
Copy link
Contributor

linaori commented Jan 6, 2017

  • framework.yml loads the extension named framework
  • iltar_http.yml loads the extension named iltar_http
  • etc.

Requirement: The first key of the file has to match the extension name, so that same as it is now, but just a fail safe for autodetection.

@theofidry
Copy link
Contributor Author

ah yeah that's what I do but not all my files requires an extension (if the default suffice, or if there is no extension, just services – yeah I have extension and service in the same file) and that requires to parse the file content before loading them, which is far from ideal as well.

@linaori
Copy link
Contributor

linaori commented Jan 6, 2017

Load the content, parse the yaml, then merge the arrays or skip it if it failed

@theofidry
Copy link
Contributor Author

Hm actually when I decide if a file is loaded or not, I just choose the appropriate name, having the content matching is just a convention but it's not what I'm basing the loading on. For example if I want to add a file base_parameters.yml I want it to be loaded, not because of it's content (it could be empty), but just because it's name is not specific to an environment (hence included for any).

@javiereguiluz javiereguiluz added Config Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Jan 8, 2017
fabpot added a commit that referenced this issue Feb 14, 2017
…files (pierredup)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DependencyInjection] Use glob pattern to load config files

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21173
| License       | MIT
| Doc PR        | -

This relates to #21173, but I'm not sure if it completely fixes the issue.

This allows to use a glob pattern to load config files, which makes the following possible:

```
# config.yml
imports:
    - { resource: "*.yml" }
    - { resource: "folder/*.yml" }
    - { resource: "/etc/myapp/*.{yml,xml}" }
```

It can also be used in a container extension, if a bundle uses a lot of configs:

```
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('*.yml');
$loader->load('routing/*');
```

Commits
-------

519180e Use glob pattern to load config file
@fabpot fabpot closed this as completed Feb 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Config Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

No branches or pull requests

5 participants