Yii Framework Forum: Configuration files in Yaml - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Configuration files in Yaml A more readable way to maintain Yii settings (easy installation) Rate Topic: ***** 2 Votes

#1 User is offline   stefanos 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-February 11

Posted 30 March 2011 - 08:50 PM

The border detection of arrays and subarrays in a php configuration file is a real headache to me. Especially when pretty coding is not in the top priorities, which is the case for the most of the time.

I met Yaml in Ruby on Rails config files and I really appreciated when I was first encountered the Yii's ones (With no offense sir Qiang ;D )

I don't know if it's the optimal way to do this but I crafted a solution with Symfony YAML that produces a double interface consisting:
1. A php config array to Yii
2. A yml structure to user

This is possible just by adding a php tag on the top of configuration file
<?php
    Yii::import('system.vendors.SymfonyComponents.YAML.*');
    list(,$caller) = debug_backtrace(false);
    if($caller['file'] !== __FILE__)
      return sfYaml::load(__FILE__);
?>


All the necessary files to make it happen are included here Attached File  Symfony Yaml for Yii.zip (13.64K)
Number of downloads: 32

Installation process
1.(optional)Install Symfony YAML as described here
2.Go to the folder \PHP_installation_folder\PEAR (Yes I'm on Windows) and copy the folder SymfonyComponents or take it from the attachment
3.Paste it to Yii_path\framework\vendors
4.Create a new Yii web app and copy yml attachment files to your_new_web_app\protected\config folder

For new files to get involved, these should simply be renamed as php (remove the yml extension) or, mainly for code highlight reasons, change the $config assignment adding the ".yml" extension at the end of the reference path in the following files of your_new_web_app folder
1. index.php line 5
2. index-test.php line 9
3. protected\yiic.php line 5
4. protected\tests\bootstrap.php line 5

Please read the The YAML Format, an excellent single page documentation, for further details about the topic.

I'll be happy to know your opinion about this (or my English <_< )

Happy coding!!

[CORRECTION]
In file main.php.yml and line 45 please replace db file name testdrive1.db with testdrive.db (remove the '1')
Giving is not a service to community but to myself.
Because being exposed to people I deeply respect, resets my mind to life
1

#2 User is offline   phtamas 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 445
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 01 April 2011 - 01:15 AM

Nice idea, simple and elegant implementation.
One possible problem:

Parsing a YAML file on every request may have significant impact on application's performance. It's worth caching the result array in a php file by using var_export() and parse the original YAML file again only when it changes.

0

#3 User is offline   stefanos 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-February 11

Posted 01 April 2011 - 07:09 PM

View Postphtamas, on 01 April 2011 - 01:15 AM, said:


Parsing a YAML file on every request may have significant impact on application's performance. It's worth caching the result array in a php file by using var_export() and parse the original YAML file again only when it changes.



Yes indeed, actually in my system this approach is 66 times slower.

So based on your suggestion:
<?php
    @include $tmp = __FILE__ . '.cch';
    if(@filemtime($tmp) < filemtime(__FILE__))
    {
      file_put_contents($tmp, '<?php $config = null;');
      Yii::import('system.vendors.SymfonyComponents.YAML.*');
      file_put_contents($tmp, '<?php $config='. var_export($config=sfYaml::load(__FILE__), true).';');
    }
    if($config !== null) 
      return $config;
?>


This makes importing only 2 times slower than plain php (practically is about 6 additional secs after 10000 requests).

I suppose this should be replaced only in main.php.yml and the cache file 'main.php.yml.cch' should not be deployed on a destination server.

Thank you phtamas :)
Any other suggestions are welcomed
Giving is not a service to community but to myself.
Because being exposed to people I deeply respect, resets my mind to life
0

#4 User is offline   elrond 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 27-March 12

Posted 29 January 2013 - 05:00 AM

How about creating cli command to generate configs in production mode ?? (so it could be run as post-deploy action)

And what about icluded configs? (like we have one general config which is merged with local seettings)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users