Skip to content

Yaml with Symfony Yaml

Derek Jones edited this page Jul 5, 2012 · 7 revisions

Category:Libraries::Yaml | Category:Yaml

YAML Ain't Markup Language

YAML is a human friendly data serialization standard for all programming languages. YAML is a great format for your configuration files. YAML files are as expressive as XML files and as readable as INI files.

Symfony YAML

Symfony YAML is a PHP library that parses YAML strings and converts them to PHP arrays. It can also converts PHP arrays to YAML strings. sfYaml is licensed by a Attribution-Share Alike 3.0 Unported License. see license

Thread: http://codeigniter.com/forums/viewthread/168491/

Wrapper (codeigniter library) and sfyaml.

-File:ci_sfyaml.zip

Tested with codeigniter versions:

  • 1.7.2

To load this library:

$this->load->library('yaml');

Method Summary

load Loads YAML into a PHP array.

  • @param string $input Path of YAML file or string containing YAML
$input = APPPATH . 'directory/filename.yml';
$this->yaml->load($input); // return array

dump Dumps a PHP array to a YAML string.

  • @param integer $inline - The level where you switch to inline YAML
$this->yaml->dump($array, $inline); // return string
$array = array(
   'one' => array (
         'id' => 1,
         'username' => 'administrator',
         'password' => 'adminpass',
         'email' => '[email protected]',
         'fullname' => 'Jonh Doe',
         'role' => 1
   ),

   'two' => array (
         'id' => 2,
         'username' => 'manager',
         'password' => 'manpass',
         'email' => '[email protected]',
         'fullname' => 'Jane Doe',
         'role' => 2
   )
);
$inline = 2;
$this->yaml->dump($array, $inline);

Returns this YAML string representing the original PHP array:

one:
  id: 1
  username: administrator
  password: adminpass
  email: [email protected]
  fullname: 'John Doe'
  role: 1
two:
  id: 2
  username: manager
  password: manpass
  email: [email protected]
  fullname: 'Jane Doe'
  role: 2

write_yml Save a file with a YAML string Dumped from a PHP array.

  • @param string $file File name
  • @param array $array PHP array
  • @param string $path Optional specific directory path to save the file
$this->yaml->write_yml($file, $array, $path = '');
$path = APPPATH.'fixtures';
$this->yaml->write_yml('temp.yml', $array, $path);
$file = APPPATH.'fixtures/temp.yml';
$this->yaml->write_yml($file, $array);

In both cases the file is saved to: application/fixtures/temp.yml

getSpecVersion Gets the YAML specification version to use.

$this->yaml->getSpecVersion() // return string

setSpecVersion Sets the YAML specification version to use.

$this->yaml->setSpecVersion($version) // return void

Note: Default Yaml specification version is 1.2

Codeigniter Comunity username: OliverHR

Clone this wiki locally