Chapter 4. Accessors and configuration

Table of Contents

Accessors
Using paths in keys
Using accessors with any arrays
The configuration file

In Atomik, the way configuration works is a bit different. Rather than having a dedicated system, it offers a global store where anything can be saved for the time of a request.

This global store act like an associative array with key/value pairs.

Accessors

Accessors are methods provided by the Atomik class that allow you to access the global store. They are four of them: get, set, has and delete.

The get method allow you to retreive the value associated to the key passed as first argument. If a second argument is specified it will be use as a default value in the case where the key is not found.

Example 4.1. Using the get method

				
echo Atomik::get('key');
echo Atomik::get('keyThatDoesntExist', 'defaultValue');
			

The set method allow you to define a key and its associated value. It will overwrite an existing value.

This accessor can also take an array as argument to set multiple key/value pairs at once. This array will be merged with the store.

Example 4.2. Using the set method

Setting one key

				
Atomik::set('key', 'value');
			

Setting multiple keys

				
Atomik::set(array(
	'key1' => 'value1',
	'key2' => 'value2'
));
			

The has and the delete methods only take a key as argument. The first one checks if the key exists and the second delete the key and its value.

Example 4.3. Using the has and delete methods

				
if (Atomik::has('key')) {
	Atomik::delete('key');
}
			

Using paths in keys

Paths can be used to access nested arrays. Each key in the path as to point to a nested array unless it's the last one. Keys are separated by a slash.

Example 4.4. Using paths

				
Atomik::set('users', array(
	'paul' => array(
		'id' => 1,
		'age' => 20
	),
	'peter' => array(
		'id' => 2,
		'age' => 33
	)
));

$paul = Atomik::get('users/paul'); // returns an array
$paulAge = Atomik::get('users/paul/age'); // returns 20
$peterId = Atomik::get('users/peter/id'); // returns 2

Atomik::set('users/sofia', array(
	'id' => 3,
	'age' => 25
));

$sofiaAge = Atomik::get('users/sofia/age');
			

Using accessors with any arrays

Accessors can be used on any array. You need to pass as argument an array (the position depends on the method). See the API guide for more information. Still, here's an example:

Example 4.5. Using accessors with any array

				
$array = array();

Atomik::set('key', 'value', $array);

echo Atomik::get('key', null, $array);
			

The configuration file

Atomik provides a default configuration for everything (to fullfill the convention over configuration principle). However, you can override it and provide plugin's configuration or even your own.

To do so, there is two possibilities:

  • modify Atomik's core file (index.php)
  • create a config.php file in the same directory as Atomik's core file

The first method is greatly discourage. It can however be useful if you want to create your own Atomik distribution (this should be the only reason to use this method).

The second method is of course the prefered one. It allows clearer configuration in a unique place, perfect for maintenance. Just create a config.php file in the same directory as Atomik's core file (index.php). In this file, you can use accessors (the set method of course) to define configuration keys.

Example 4.6. Exemple config.php file

				
<?php

/* configuration */
Atomik::set(array(

	'key1' => 'value1',
	
	'key2' => value2',
	
	'key3' => array(
	
		'key3.1' => 'value3.1'
	
	)

));
			

The configuration filename can be overrided in Atomik's core configuration.