-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(system): strip redundant tabs or replace with 4 spaces
- Loading branch information
Showing
29 changed files
with
950 additions
and
950 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ you need a **Config Writer**. | |
Implementing them is as simple as extending the | ||
[Kohana_Config_Reader] / [Kohana_Config_Writer] interfaces: | ||
|
||
class Kohana_Config_Database_Reader implements Kohana_Config_Reader | ||
class Kohana_Config_Database_Writer extends Kohana_Config_Database_Reader implements Kohana_Config_Writer | ||
class Kohana_Config_Database_Reader implements Kohana_Config_Reader | ||
class Kohana_Config_Database_Writer extends Kohana_Config_Database_Reader implements Kohana_Config_Writer | ||
|
||
You'll notice in the above example that the Database Writer extends the Database Reader. | ||
This is the convention with config sources, the reasoning being that if you can write to a | ||
|
@@ -35,44 +35,44 @@ the database source uses a column to distinguish between groups. | |
|
||
To load a config group simply call `Kohana::$config->load()` with the name of the group you wish to load: | ||
|
||
$config = Kohana::$config->load('my_group'); | ||
$config = Kohana::$config->load('my_group'); | ||
|
||
`load()` will return an instance of [Config_Group] which encapsulates the config values and ensures | ||
that any modifications made will be passed back to the config writers. | ||
|
||
To get a config value from a [Config_Group] object simply call [Config_Group::get]: | ||
|
||
$config = Kohana::$config->load('my_group'); | ||
$value = $config->get('var'); | ||
$config = Kohana::$config->load('my_group'); | ||
$value = $config->get('var'); | ||
|
||
To modify a value call [Config_Group::set]: | ||
|
||
$config = Kohana::$config->load('my_group'); | ||
$config->set('var', 'new_value'); | ||
$config = Kohana::$config->load('my_group'); | ||
$config->set('var', 'new_value'); | ||
|
||
### Alternative methods for getting / setting config | ||
|
||
In addition to the methods described above you can also access config values using dots to outline a path | ||
from the config group to the value you want: | ||
|
||
// Config file: database.php | ||
return array( | ||
'default' => array( | ||
'connection' => array( | ||
'hostname' => 'localhost' | ||
) | ||
) | ||
); | ||
// Config file: database.php | ||
return array( | ||
'default' => array( | ||
'connection' => array( | ||
'hostname' => 'localhost' | ||
) | ||
) | ||
); | ||
|
||
// Code which needs hostname: | ||
$hostname = Kohana::$config->load('database.default.connection.hostname'); | ||
// Code which needs hostname: | ||
$hostname = Kohana::$config->load('database.default.connection.hostname'); | ||
|
||
|
||
Which is equivalent to: | ||
|
||
$config = Kohana::$config->load('database')->get('default'); | ||
$config = Kohana::$config->load('database')->get('default'); | ||
|
||
$hostname = $config['connection']['hostname']; | ||
$hostname = $config['connection']['hostname']; | ||
|
||
Obviously this method is a lot more compact than the original. However, please bear in mind that using | ||
`dot.notation` is a _lot_ slower than calling `get()` and traversing the array yourself. Dot notation | ||
|
@@ -81,13 +81,13 @@ can be useful if you only need one specific variable, but otherwise it's best to | |
As [Config_Group] extends [Array_Object](http://php.net/manual/en/class.arrayobject.php) you can also use array | ||
syntax to get/set config vars: | ||
|
||
$config = Kohana::$config->load('database'); | ||
$config = Kohana::$config->load('database'); | ||
|
||
// Getting the var | ||
$hostname = $config['default']['connection']['hostname']; | ||
// Getting the var | ||
$hostname = $config['default']['connection']['hostname']; | ||
|
||
// Setting the var | ||
$config['default']['connection']['hostname'] = '127.0.0.1'; | ||
// Setting the var | ||
$config['default']['connection']['hostname'] = '127.0.0.1'; | ||
|
||
Again, this syntax is more costly than calling `get()` / `set()`. | ||
|
||
|
@@ -106,66 +106,66 @@ The position of sources in the stack is determined by how they are loaded in you | |
By default when you load a source it is pushed to the top of a stack: | ||
|
||
// Stack: <empty> | ||
Kohana::$config->attach(new Config_File); | ||
// Stack: Config_File | ||
Kohana::$config->attach(new Config_Database); | ||
// Stack: Config_Database, Config_File | ||
Kohana::$config->attach(new Config_File); | ||
// Stack: Config_File | ||
Kohana::$config->attach(new Config_Database); | ||
// Stack: Config_Database, Config_File | ||
|
||
In the example above, any config values found in the database will override those found in the filesystem. | ||
For example, using the setup outlined above: | ||
|
||
// Configuration in the filesystem: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
|
||
// Configuration in the database: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
|
||
// Configuration returned by Kohana::$config->load('email') | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
method: smtp | ||
// Configuration in the filesystem: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
|
||
// Configuration in the database: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
|
||
// Configuration returned by Kohana::$config->load('email') | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
method: smtp | ||
|
||
[!!] **Note:** The above syntax is simply pseudo code to illustrate the concept of config merging. | ||
|
||
On some occasions you may want to append a config source to the bottom of the stack, to do this pass `FALSE` | ||
as the second parameter to `attach()`: | ||
|
||
// Stack: <empty> | ||
Kohana::$config->attach(new Config_File); | ||
// Stack: Config_File | ||
Kohana::$config->attach(new Config_Database, FALSE); | ||
// Stack: Config_File, Config_Database | ||
// Stack: <empty> | ||
Kohana::$config->attach(new Config_File); | ||
// Stack: Config_File | ||
Kohana::$config->attach(new Config_Database, FALSE); | ||
// Stack: Config_File, Config_Database | ||
|
||
In this example, any values found in the filesystem will override those found in the db. For example: | ||
|
||
// Configuration in the filesystem: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
|
||
// Configuration in the database: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
|
||
// Configuration returned by Kohana::$config->load('email') | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
// Configuration in the filesystem: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
|
||
// Configuration in the database: | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Kohana Bot | ||
|
||
// Configuration returned by Kohana::$config->load('email') | ||
email: | ||
sender: | ||
email: [email protected] | ||
name: Unknown | ||
method: smtp | ||
|
||
## Using different config sources based on the environment | ||
|
||
|
@@ -180,14 +180,14 @@ so replacing the default `Config_File` source isn't really an option. | |
To get around this you can attach a separate config file reader which loads its config from a subdir of `config` called | ||
"testing": | ||
|
||
Kohana::$config->attach(new Config_File); | ||
Kohana::$config->attach(new Config_File); | ||
|
||
Kohana::$config->attach(new Config_Database); | ||
Kohana::$config->attach(new Config_Database); | ||
|
||
if (Kohana::$environment === Kohana::TESTING) | ||
{ | ||
Kohana::$config->attach(new Config_File('config/testing')); | ||
} | ||
if (Kohana::$environment === Kohana::TESTING) | ||
{ | ||
Kohana::$config->attach(new Config_File('config/testing')); | ||
} | ||
|
||
During normal development the config source stack looks like `Config_Database, Config_File('config')`. However, | ||
when `Kohana::$environment === Kohana::TESTING` the stack looks like `Config_File('config/testing'), Config_Database, Config_File('config')` |
Oops, something went wrong.