简体   繁体   English

YAML和symfony2

[英]YAML and symfony2

I have noticed that in the config.yml file of symfony2, the import feature is used as below 我已经注意到,在symfony2的config.yml文件中,导入功能的使用方式如下

imports:
- { resource: security.yml }
- { resource: services.yml }

I am using some YAML file in my own bundle to init some read only entities. 我在自己的捆绑软件中使用了一些YAML文件来初始化一些只读实体。 However, they are all jam-packed in this one single YAML file. 但是,它们都被打包在一个YAML文件中。 I am using Symfony\\Component\\Yaml\\Parser; 我正在使用Symfony\\Component\\Yaml\\Parser; component to read this file. 组件来读取此文件。

However, if I try to copy this nice feature of import , the parser only reads it normally and it doesn't interpret the import nor the resource keyword. 但是,如果我尝试复制import这个不错的功能,则解析器只会正常读取它,并且不会解释import或resource关键字。

imports:
- { resource: test.yml }

That is the var_dump is simply the node tree without interpretation. 那就是var_dump只是没有解释的节点树。 Test is not loaded. 测试未加载。

How can I use this same feature as in the config.yml file ? 如何使用与config.yml文件相同的功能?

Well as suggested by Anthon, I created my own implementation using some symfony components, here is the class for people that are interested ( it's a basic implementation, do whatever you want with it) 就像Anthon所建议的那样,我使用了一些symfony组件创建了自己的实现,这是针对感兴趣的人的类(这是一个基本实现,可以用它做任何想做的事情)

use Symfony\Component\Yaml\Parser;
use Symfony\Component\Filesystem\Filesystem;

class MyYmlParser {

protected $parser;
protected $finder;
protected $currentDir;
protected $ymlPath;
protected $data;

public function __construct($rootDir) {
    $this->rootDir = $rootDir;
    $this->parser = new Parser();
    $this->fs = new Filesystem;
}

public function setYmlPath($ymlPath) {
    $this->ymlPath = $ymlPath;
    $this->currentDir = dirname($this->ymlPath) . "/";
    return $this;
}

public function getYmlPath() {
    return $this->ymlPath;
}

private function parseFile($path) {
    if ($this->fs->exists($path)):
        return $this->parser->parse(file_get_contents($path));
    else:
        throw new \Exception("$path Do not exsist");
    endif;
}

private function buildPathFromImport($fileName) {
    return $this->currentDir . $fileName;
}

public function parse($ymlPath) {
    $this->setYmlPath($ymlPath);
    $this->data = $this->parseFile($this->ymlPath);
    if (isset($this->data["imports"])):
        foreach ($this->data["imports"] as $array):
            $importData = $this->parseFile($this->buildPathFromImport($array["resource"]));
            $this->data = array_merge($this->data, $importData);
        endforeach;
        unset($this->data['imports']);
    endif;
    #dump($this->data); exit();
    return $this->data;
}
}

Usage is quite simple: 用法很简单:

//Follow symfony syntax for imports that is:
 imports:
  - { resource: test.yml }
  - { resource: nested/dir/test2.yml }

$myYmlParser = new MyYmlParser();
$parsedData = $myYmlParser->parse($path); //the path to your yml file
//thats it, you got an array with the data form other files and the original file.

 //dont forget to add it to your services for the rootDir
 AmceBundle.ymlParser:
    class: OP\AcmeBundle\Services\MyYmlParser
    arguments: ["%kernel.root_dir%"]

The YAML parser reads it normally in all cases. 在所有情况下,YAML解析器都会正常读取它。 Only for the config.yml the program processing the represented instances "expands' the values for the mapping key import by taking from the list that is the associated value and replacing the nodes with the value associated with the resource . 仅对于config.yml ,处理所表示实例的程序通过从列表中获取关联键值并将节点替换为与resource关联的值来“扩展”映射键import的值。

This is not a feature of YAML , but an interpretation of the data handed to the program by the parser. 这不是YAML的功能,而是对解析器传递给程序的数据的解释。 If your program doesn't apply this feature recursively, you should patch the program to do so. 如果您的程序没有递归地应用此功能,则应对此程序进行修补。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM