Deprecated/0.10/YourConfigFileRedux
Warning! This page has the following tags:
- This page is deprecated, and is only useful for developers using Agavi 0.10.
- This page exists for reference purposes only.
Creating Your Own Config Files (Redux)
Some days ago, Bob wrote a tutorial that described how to create and use your own config files. In the meantime, I have written a new config file handler that makes things a bit less "messy", because you won't have dozens of constants defined in the global namespace when dealing with larger config files.
About ReturnArrayConfigHandler?
Every config file handler processes and compiles a config file, usually .ini files. Most handlers compile the files into a very efficient format - most system config file handlers like the one for databases.ini will create the code that sets up and registers database drivers and writes it to a file, which is then included during the startup process.
In your own application, however, you often just want the plain contents of an ini file, where you define certain behaviors that affect how E-Mails are sent, or where you specify thumbnail sizes for images stored on the server. Bob showed how to fetch the contents of these ini files and have them defined as constants. With the ReturnArrayConfigHandler, there's a new way of doing it, because this handler returns the parsed ini file as a multi-dimensional array.
Installing the Handler
ReturnArrayConfigHandler has been in SVN since changeset:187. It should be in autoload.ini by default, if not, copy the appropriate line from autoload.ini in the sample project or the buildtools/code_templates/config directory.
The next thing to do is registering the handler, so the system knows which handler to use when processing your custom config file on request. To do that, you have to edit config_handlers.ini. In my example, I have a global thumbnails.ini configuration file which resides in webapp/config
[config/thumbnails.ini]
class = "ReturnArrayConfigHandler"
That's it. We can move on to using it now.
Our thumbnails.ini
[tiny] width = 120 height = 120 preserveAspectRatio = false [small] width = 400 height = 300
Using it in Your Action
$thumbnailIniContents = include(ConfigCache::checkConfig('config/thumbnails.ini'));
foreach($thumbnailIniContents as $key => $section)
{
echo '\\nThumbnail size "' . $key . '" specifies width/height values ' . $section['width'] . '/' . $section['height'] . ' pixels' . $section['preserveAspectRatio'] ? ' and will not preserve the aspect ratio of the original image' : '';
}
This will output:
Thumbnail size "tiny" specifies width/height values 120/120 pixels and will not preserve the aspect ratio of the original image Thumbnail size "small" specifies width/height values 400/300 pixels
More Dimensions
Of course, you do not have to use sections. In that case, you'll get a one-dimensional array. But in case you need more than the default one or two dimensions, you can use any infinite number of depth levels in your ini file (at least until you run out of memory). This works by using a period as a delimiter between the different parts of a key:
param.foo = "value1" param.bar = "value2"
This will result in a multi-dimensional array being created. An array with two values "value1" and "value2" and the associative keys "foo" and "bar" will sit inside the outer array, having the key "param".
You may also create numeric arrays like this:
my.crazy.config.0 = "first item" my.crazy.config.1 = "second item" my.crazy.config.2 = "third item" my.crazy.config.3 = "fourth item"

