cubicmushroom-symfony/wildcard-config-loader-bundle

Symfony 插件,用于通过通配符模式包含配置文件

dev-develop 2016-03-12 18:28 UTC

This package is not auto-updated.

Last update: 2024-09-26 00:20:00 UTC


README

此插件提供了一个简单的方法,通过全局模式匹配文件来加载多个配置文件。

用法

要使用配置加载器,您需要更新您的 AppKernel 以添加一个新的 \CubicMushroom\Symfony\WildcardConfigLoaderBundle\ConfigLoader 实例作为配置加载器,通过重写 AppKernel::getContainerLoader() 方法,如下所示...

use CubicMushroom\Symfony\WildcardConfigLoaderBundle\ConfigLoader;
use CubicMushroom\Symfony\WildcardConfigLoaderBundle\GlobFileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;

class AppKernel
{
    // ...

    /**
     * Add loader to load client config files (app/config/clients/*)
     *
     * @param ContainerInterface $container
     *
     * @return DelegatingLoader
     *
     * @throws RuntimeException
     */
    protected function getContainerLoader(ContainerInterface $container)
    {
        // Container must be an instance of ContainerBuilder
        if (!$container instanceof ContainerBuilder) {
            throw new \RuntimeException(
                sprintf(
                    'Expected loader to be instance of %s, but got %s',
                    ContainerBuilder::class,
                    get_class($container)
                )
            );
        }

        $cl = parent::getContainerLoader($container);

        // Add additional loader to the resolver
        $resolver = $cl->getResolver();

        if (!$resolver instanceof LoaderResolver) {
            throw new \RuntimeException(
                sprintf(
                    'Expected container to be instance of %s, but got %s',
                    LoaderResolver::class,
                    get_class($resolver)
                )
            );
        }

        $resolver->addLoader(new ConfigLoader($container, new GlobFileLocator([])));

        return $cl;
    }
}

要处理的文件

默认情况下,此配置加载器将处理任何包含星号 (*) 的文件字符串。如果您想更改类处理的文件,只需扩展该类并重写 supports() 方法。

配置值

在文件中找到的配置值将存储在数组参数中。例如,如下所示的 yml 配置文件内容...

client:
    here:
        this: 123
        that: 456
    now:
        something: else

... 将转换为以下参数...

client = [
    'here' => [
        'this' => 123,
        'that' => 456,
    ],
    'something' => 'else',
]

这是使用 ConfigLoader::PARAMETER_TYPE_ARRAY 选项的细节。如果您更喜欢平面参数,可以通过调用 setParameterOutput() 方法并以 ConfigLoader::FLAT 作为唯一参数来更改此设置。

client.here.this = 123
client.here.that = 456
client.now.something = else

重写默认参数存储

如果您想覆盖默认的配置变量存储,可以扩展 ConfigLoader 类并重写 setParameters() 方法。