2012-01-10 62 views
5

我想从我的一个服务中的yaml文件中获取数组,并且我对如何注入要在我的服务中使用的文件有点困惑。阳明海运。如何从Symfony2中的服务中解析yaml文件

# /path/to/app/src/Bundle/Resources/config/services.yml 
parameters: 
    do_something: Bundle\DoSomething 
    yaml.parser.class: Symfony\Component\Yaml\Parser 
    yaml.config_file: "/Resources/config/config.yml" # what do I put here to win! 

services: 
    yaml_parser: 
     class: %yaml.parser.class% 

    do_parsing: 
     class: %do_something% 
     arguments: [ @yaml_parser, %yaml.config_file% ] 

在我的服务,我有

# /path/to/app/src/Bundle/DoSomething.php 

<?php 

namespace Bundle; 

use \Symfony\Component\Yaml\Parser; 

class DoSemething 
{ 
    protected $parser; 
    protected $parsed_yaml_file; 

    public function __construct(Parser $parser, $file_path) 
    { 
     $this->parsed_yaml_file = $parser->parse(file_get_contents(__DIR__ . $file_path)); 
    } 

    public function useParsedFile() 
    { 
     foreach($parsed_yaml_file as $k => $v) 
     { 
      // ... etc etc 
     } 
    } 
} 

这可能是完全错误的做法,我是否应该做别的事情,请让我知道!

回答

8

首先,我将解释为什么我实现我的解决方案为您来决定,如果这种情况下是适合你的。

我需要一种方法来轻松地加载自定义.yml文件在我的包中(对于很多捆绑包),所以为每个文件添加一个单独的行到app/config.yml对于每个安装似乎都很麻烦。

此外,我希望大部分配置已经默认加载,所以最终用户甚至不需要担心配置大部分时间,特别是不检查每个配置文件的设置是否正确。

如果这对你来说似乎是一个类似的情况,请继续阅读。如果没有,只需使用Kris解决方案,也是一个不错的选择!


回来时,我遇到了一个需要此功能,Symfony2的didnt't提供了一种简单的方式来实现这一点,所以在这里我如何解决它:

首先,我创建了一个本地YamlFileLoader类基本上一个简单化的Symfony2一个:

<?php 

namespace Acme\DemoBundle\Loader; 

use Symfony\Component\Yaml\Yaml; 
use Symfony\Component\Config\Loader\FileLoader; 

/** 
* YamlFileLoader loads Yaml routing files. 
*/ 
class YamlFileLoader extends FileLoader 
{ 
    /** 
    * Loads a Yaml file. 
    * 
    * @param string $file A Yaml file path 
    * 
    * @return array 
    * 
    * @throws \InvalidArgumentException When config can't be parsed 
    */ 
    public function load($file, $type = null) 
    { 
     $path = $this->locator->locate($file); 

     $config = Yaml::parse($path); 

     // empty file 
     if (null === $config) { 
      $config = array(); 
     } 

     // not an array 
     if (!is_array($config)) { 
      throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file)); 
     } 

     return $config; 
    } 

    /** 
    * Returns true if this class supports the given resource. 
    * 
    * @param mixed $resource A resource 
    * @param string $type  The resource type 
    * 
    * @return Boolean True if this class supports the given resource, false otherwise 
    * 
    * @api 
    */ 
    public function supports($resource, $type = null) 
    { 
     return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); 
    } 
} 

然后我更新DIC延长我的包(它通常是自动生成的,如果你让Symfony2中创建完整的包结构,如果不只是在与弗洛捆绑软件目录下创建一个DependencyInjection/<Vendor&BundleName>Extension.php文件翼内容:

<?php 

namespace Acme\DemoBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\Loader; 

use Acme\DemoBundle\Loader\YamlFileLoader; 

/** 
* This is the class that loads and manages your bundle configuration 
* 
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} 
*/ 
class AcmeDemoExtension extends Extension 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 

     $loader->load('services.xml'); 

     // until here everything is default config (for your DIC services) 

     $ymlLoader = new YamlFileLoader(new FileLocator(__DIR__.'/../Resources/config')); 
     $container->setParameter('param_name', $ymlLoader->load('yaml_file_name'))); // load yml file contents as an array 
    } 
} 

现在你可以访问/传递你的yaml配置作为简单的服务参数(即, %param_name%为services.yml)

+0

感谢深入的回答,非常有帮助。 – 2012-01-11 10:41:16

3

可以使用kernel.root_dir参数:

parameters: 
    yaml.config_file: "%kernel.root_dir%/../src/Path/To/MyBundle/Resources/config/config.yml" 
6

我解决这样说:

Services.yml

#/path/to/app/src/Bundle/Resources/config/services.yml 

parameters: 
    example.class: Path\To\Bundle\Service\Class 
    example.yaml_config_file: "%kernel.root_dir%/../src/Path/To/Bundle/Resources/config/config.yml" 

services: 
    example_service: 
     class: %example.class% 
     arguments: [%example.yaml_config_file% ] 

服务类

# /path/to/app/src/Bundle/Service/Example.php 

<?php 

namespace Bundle\Service; 

use \Symfony\Component\Yaml\Yaml; 

class Example 
{ 
    private $parsed_yaml_file; 

    public function __construct($yaml_config_file) 
    { 
     $this->parsed_yaml_file = Yaml::parse($yaml_config_file); 
    } 
}