Yii 1.1: fileconfig

This extension allows you to store in a file (read and write) configuration entries needed by your application.
5 followers

This is a simple extension that allows you to store (in a file) configuration entries needed by your application. It provides functions for reading and writing this configuration strings.

It is based on config extension developed by Y!!, but place, where configuration data is kept, has been changed from database to file. There is also a number of changes and fixes to base configuration code (see below).

It uses file for storing configuration entries as it's author thinks that using database for this purpose may decrease application performance significantly.

Requirements

  • e.g. Yii 1.1 or above.

Usage

Installation

  1. Extract the release file under protected/extensions.
  2. Modify your application configuration for the use of this extension (for security reasons, you have to specify path to your configuration file relative to protected folder):
return array
(
        ...
    'components'=>array
    (
                ...
                'config'=>array
                (
                        'class'=>'ext.FileConfig',
                        'configFile'=>'configuration/config.values',
                ),
                ...
    ),
    ...
);

Use Yii::app()->config for reading and writing configuration items:

Yii::app()->config->setValue('test', '7');
Yii::app()->config->getValue('test', 'none');

Both values and configuration array are serialized, therefore you can set and get value of any kind - i.e. string, integer, boolean, array, etc.

Configuration

These properties are available and can be set in the configuration:

  • configFile - path to a file where configuration entries are stored (relative to protected folder),
  • cacheID - defaults to false; the ID of cache component,
  • strictMode - defaults to false; see below, what happens, if this is set to true.

Strict mode

If extension is in strict mode that means:

  • you won't be able to read/write value from/to non-existing key,
  • if configuration file is not readable, you will see exception per each attempt to read configuration (in-non strict mode an empty value will be returned instead),
  • if caching component ID is invalid, exception will be raisen (in non-strict mode cache will not be used without displaying any error in this situation).

Additional notes

Various changes in relation to original config extension and other things that you should take care of:

  1. For security reasons, you have to specify path to your configuration file relative to protected folder.
  2. Setter and getter functions changed from set and get to setValue and getValue.
  3. Strict Mode turned off by default.
  4. Support for using caching component to cache configuration file is left as in base extension (with some minor changes), but most people find it unusable as configuration is kept in a file itself, therefore using caching could in this situation even degrade performance.
  5. English comments were added.
  6. Function getValue supports optional default parameter, which is returned every time when reading key value is impossible (i.e. key does not exists).

Version history

This describes in short changes made to this extension.

Version 1.2

On 8 March 2011 following changes were made to this extension:

  • Private _getConfig() function became public getConfig(),
  • Some code tweak-ups, optimizations and comments clean-ups.

Version 1.1

On 8 December 2010 the behaviour of this extension has changed slightly for further performance optimization:

  • Function setValue() now has optional third parameter forceWrite, set to FALSE by default. Actual writing of configuration file occurs now only, if this is set to TRUE (occurred always in previous version). If forceWrite is set to FALSE, this function now always returns TRUE. If set to TRUE, function will return result of calling setConfig() which can tell developer, if writing was successful.
  • By analogy, function getValue() also now has optional third parameter, forceRead (also set to FALSE by default), which (when set to TRUE) forces reading of configuration file with each call to this function. In previous version, reading of configuration file occurred only when configuration array was empty and there was no way to force it in other situations.
  • Private function _setConfig() became public function setConfig() and now is preferred way of writing configuration file - i.e. set as many params as you want with setValue() and only once call setConfig(). In previous version writing occurred each time configuration item was set, so there was no need for forcing write of configuration file.
  • Configuration items (in _config array) are no longer serialized upon setting and unserialized upon getting. Now, only configuration array is serialized / unserialized, which may result in less configuration file and access time.

Version 1.0

On 6 December 2010 this extension was initially released.

Total 5 comments

#3041 report it
Trejder at 2011/03/09 03:49pm
Super! :)

I'm happy to hear that! :] Hope you will find it useful. Good luck! :]

#3040 report it
amb at 2011/03/09 01:21pm
Sorry ,I'm sorry

it working.

#3039 report it
amb at 2011/03/09 01:18pm
$value Where it ?
public function setValue($key, $value, $forceWrite = FALSE)
    {
        if ($this->_config != NULL)
                {
                        if ((is_array($this->_config === FALSE)) || (array_key_exists($key, $this->_config) === FALSE))
                        {
                                if ($this->strictMode === TRUE) throw new CException('Unable to set value! Key "'.$key.'" does not exists and "strictMode" is set to TRUE!');
                        }
                }
                else $this->getConfig();
 
        $this->_config[$key] = $value;
 
        $cache = $this->_getCache();
        if ($cache !== FALSE) $cache->set(self::CACHE_KEY, $this->_config);
 
                return ($forceWrite) ? $this->setConfig() : TRUE;
    }

How it work?

$value Where it

#3026 report it
Trejder at 2011/03/08 05:05pm
More details!

@amb: Can you provide more details? I'm using this extension in two big project, every new release is tested and I assure you that it works.

#3024 report it
amb at 2011/03/08 02:00pm
is very bad

is very bad,no work

Leave a comment

Please to leave your comment.

Create extension