• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yaml library

#1
[eluser]OliverHR[/eluser]
Hello everybody,

I post in the wiki a wrapper to user sfYaml library with codeigniter.

Wiki and file link:
http://codeigniter.com/wiki/Yaml_with_Symfony_Yaml/

I hope this may be useful to someone, Any comments are welcome.


OliverHR.

#2
[eluser]OliverHR[/eluser]
Wrapper (codeigniter library) and sfyaml.

-ci_sfyaml.zip

Tested with codeigniter versions:
- 1.7.2


To load this library:
Code:
$this->load->library('yaml');


[h3]Method Summary[/h3]

load
Loads YAML into a PHP array.
* @param string $input Path of YAML file or string containing YAML
Code:
$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

Code:
$this->yaml->dump($array, $inline); // return string
Code:
$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:
Code:
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
Code:
$this->yaml->write_yml($file, $array, $path = '');
Code:
$path = APPPATH.'fixtures';
$this->yaml->write_yml('temp.yml', $array, $path);
Code:
$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.
Code:
$this->yaml->getSpecVersion() // return string


setSpecVersion
Sets the YAML specification version to use.
Code:
$this->yaml->setSpecVersion($version) // return void

Note: Default Yaml specification version is 1.2

#3
[eluser]Sbioko[/eluser]
Great contribution! Thanks.

#4
[eluser]jpi[/eluser]
Nice one.

I like the approach, use the already existing excellent libraries from several frameworks, wrap them, and use them in CI.

#5
[eluser]OliverHR[/eluser]
Yes extend codeigniter capabilities is quite easy.

#6
[eluser]ideogroup[/eluser]
Hi
does it implements all YAML specs (http://en.wikipedia.org/wiki/YAML) ?

I've done:

$this->load->library('yaml');
$input = APPPATH . '/application/yaml/admin_menu.yml';
print_r($this->yaml->load($input)); // return array

but it returns me only:

/application/yaml/admin_menu.yml

Why?

Thanks

#7
[eluser]OliverHR[/eluser]
@ideogroup, "APPPATH" refers to your CI application directory, then you must fix this line:

Code:
$input = APPPATH . '/application/yaml/admin_menu.yml';

To:
Code:
$input = APPPATH . 'yaml/admin_menu.yml';

#8
[eluser]belial.seed[/eluser]
Thanks man, gets the job done on CI 2.0


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.