php - YAML and symfony2 -


i have noticed in config.yml file of symfony2, import feature used below

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

i using yaml file in own bundle init read entities. however, jam-packed in 1 single yaml file. using symfony\component\yaml\parser; component read file.

however, if try copy nice feature of import, parser reads , doesn't interpret import nor resource keyword.

imports: - { resource: test.yml } 

that var_dump node tree without interpretation. test not loaded.

how can use same feature in config.yml file ?

well suggested anthon, created own implementation using symfony components, here class people interested ( it's basic implementation, whatever want it)

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 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"] $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 quite simple:

//follow symfony syntax imports is:  imports:   - { resource: test.yml }   - { resource: nested/dir/test2.yml }  $myymlparser = new myymlparser(); $parseddata = $myymlparser->parse($path); //the path yml file //thats it, got array data form other files , original file.   //dont forget add services rootdir  amcebundle.ymlparser:     class: op\acmebundle\services\myymlparser     arguments: ["%kernel.root_dir%"] 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -