我可以在Symfony2中包含一个可选的配置文件吗?
我想制作一个本地配置文件config_local.yml
,它允许在不破坏其他人的开发环境的情况下正确配置每个开发环境。我希望它是一个单独的文件,以便我可以“gitignore”它,并知道项目中没有任何必要的东西,同时没有git问题不断告诉我config_dev.yml有新的变化(并且运行风险有人实施这些改变)。我可以在Symfony2中包含一个可选的配置文件吗?
现在,我已经做config_dev.yml
imports:
- { resource: config_local.yml }
这是伟大的,除非该文件不存在(即存储库的一个新的克隆)。
我的问题是:有没有什么办法可以使这包括可选的?即,如果文件存在然后导入它,否则忽略它。
编辑:我希望的语法,如:
imports:
- { resource: config.yml }
? { resource: config_local.yml }
一个解决方案是创建一个单独的环境,这是在Symfony2中cookbook解释。如果你不想创建一个扩展,还有另外一种方法可以创建扩展。
// src/Acme/Bundle/AcmeDemo/DepencendyInjection/AcmeDemoExtension.php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class AcmeDemoExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
// All following files will be loaded from the configuration directory
// of your bundle. You may change the location to /app/ of course.
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
try
{
$loader->load('config_local.yml');
}
catch(\InvalidArgumentException $e)
{
// File was not found
}
}
}
在Symfony的代码一些挖掘发现我
YamlFileLoader::load()
FileLocator::locate()
将抛出\InvalidArgumentException
,如果没有找到一个文件。它由YamlFileLoader::load()
调用。
如果使用命名约定,则扩展名将自动执行。有关更详尽的解释,请参阅visit this blog。
还有另一种选择。
上的应用程序/ appKernel.php改变registerContainerConfiguration方法是:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
$extrafiles = array (
__DIR__.'/config/config_local.yml',
);
foreach ($extrafiles as $filename) {
if (file_exists($filename) && is_readable($filename)) {
$loader->load($filename);
}
}
}
这种方式,您有覆盖config_env.yml文件
这是个好主意,谢谢 –
我尝试上述两种答案全球config_local.yml文件但没有人为我工作。
我犯了一个新的环境:“本地”是进口的“开发”,但你可以在这里阅读:There is no extension able to load the configuration for "web_profiler"你还必须破解AppKernel类。
此外,您无法将config_local.yml设置为.gitignore,因为该文件在本地环境中是必需的。因为我有无论如何要破解AppKernel我试图与$ extrafiles的方法,但是,导致“ForbiddenOverwriteException”
所以,现在什么工作对我来说是$ extrafiles办法的修改:
替换app/AppKernel中。PHP $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
与
if ($this->getEnvironment() == 'dev') {
$extrafiles = array(
__DIR__ . '/config/config_local.yml',
);
foreach ($extrafiles as $filename) {
if (file_exists($filename) && is_readable($filename)) {
$loader->load($filename);
}
}
} else {
$loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
}
我知道这是一个非常古老的问题,我认为在认可的解决方案是更好,我想我会给它并没有改变任何代码
的利益而简单的解决方案可以使用ignore_errors选项,这将不显示任何错误,如果该文件不存在
imports:
- { resource: config_local.yml, ignore_errors: true }
警告,如果文件中出现语法错误,它也将被忽略,因此如果您有意外的结果,请检查以确保文件中没有语法错误或其他错误。
你的回答让我想,也许他可以创建自己的'YamlFileLoader',如果无法找到yml文件,将会优雅地失败。 –
非常好的答案,谢谢。我希望我能给+2 –
@arms是的,这也是可能的,但他只需要创建一个自定义的'FileLocator'而不是'YamlFileLoader'。这个异常被引发到'Symfony \ Component \ Config \ FileLocator :: locate'中,所以我们只需要扩展基类来使得加载失败。 – gilden