3
votes

I found the following undocumented feature in the documentation chapter Miscellaneous Configuration:

A resource file can be one of many types. PHP, XML, YAML, INI, and closure resources are all supported by the imports directive.

The class in charge of loading these resources seems to be:

Symfony\Component\DependencyInjection\Loader\ClosureLoader ( APIdoc , Code )

In my understanding according to the documentation it should be possible to import services and/or parameters using a PHP closure inside a configuration file.

non-working example:

# app/config/config.yml
imports:
     # closure? something like eval( <multiline-string> ) ?
    - { resource: >  
                    function($containerBuilder) { 
                        return array( 
                            'parameters' => array(
                                'upload_tmp_dir' => ini_get('upload_tmp_dir')
                            )
                         );
                    } 
    }    

I can't find an example showing how to use this loader in a PHP,XML,YAML configuration file with the imports directive.

  • Is there any example available using imports?

  • Is the ClosureLoader compatible with the YAML format?

There is an example available that shows how to use the Symfony\Component\Routing\Loader\ClosureLoader to load a route collection ( Routes as Closures).

But it does not show how to use the closure with the imports directive as the documentation states.


The idea behind my question is that it might be possible to access PHP's configuration values or some class constants in config.yml without creating an extension/compiler-pass or importing a file in a different configuration format. ( i.e. accessing constants is not possible in YAML format ).

This would open up some great possibilities ...

... like adding a closure that imports a parameter for the upload_tmp_dir path in php.ini that can instantly be used in LiipMonitorBundle's liip_monitor.checks.writable_directory array afterwards.

2
There was a feature in the Yaml component that evaluated PHP code, but was soon removed. But your asking could still be possible.Adam Elsodaney

2 Answers

0
votes

I've been able to use that loader in AppKernel:

class AppKernel extends \Symfony\Component\HttpKernel\Kernel
{
    // ...

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir() . '/config/config_' . $this->getEnvironment() . '.yml');

        // Load config from the closure
        $loader->load(function () {
           // ..
        });
    }
}

The problem is that this is not taken into account when recomputing the cache (i.e. if you change your closure, the cache will not change).

-1
votes

to use an ini file you can juste call the file as ressource:

- { resource: parameters.ini }

theres 4 type of closure with the symfony dependcyInjection Component (Symfony 2.3). there are IniFileLoader, PhpFileLoader, XmlFileLoader and YamlFileLoader.

To use a personal closure you should create you personal closure that extends from Symfony\Component\DependencyInjection\Loader\FileLoader

This a good exemple : http://www.adayinthelifeof.nl/2013/01/30/custom-symfony2-config-loader/