Kohana v3.3.0
This commit is contained in:
15
system/classes/Kohana/Config/File.php
Normal file
15
system/classes/Kohana/Config/File.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* File-based configuration reader. Multiple configuration directories can be
|
||||
* used by attaching multiple instances of this class to [Config].
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2009-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Config_File extends Kohana_Config_File_Reader
|
||||
{
|
||||
// @see Kohana_Config_File_Reader
|
||||
}
|
56
system/classes/Kohana/Config/File/Reader.php
Normal file
56
system/classes/Kohana/Config/File/Reader.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* File-based configuration reader. Multiple configuration directories can be
|
||||
* used by attaching multiple instances of this class to [Kohana_Config].
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2009-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Config_File_Reader implements Kohana_Config_Reader {
|
||||
|
||||
/**
|
||||
* The directory where config files are located
|
||||
* @var string
|
||||
*/
|
||||
protected $_directory = '';
|
||||
|
||||
/**
|
||||
* Creates a new file reader using the given directory as a config source
|
||||
*
|
||||
* @param string $directory Configuration directory to search
|
||||
*/
|
||||
public function __construct($directory = 'config')
|
||||
{
|
||||
// Set the configuration directory name
|
||||
$this->_directory = trim($directory, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and merge all of the configuration files in this group.
|
||||
*
|
||||
* $config->load($name);
|
||||
*
|
||||
* @param string $group configuration group name
|
||||
* @return $this current object
|
||||
* @uses Kohana::load
|
||||
*/
|
||||
public function load($group)
|
||||
{
|
||||
$config = array();
|
||||
|
||||
if ($files = Kohana::find_file($this->_directory, $group, NULL, TRUE))
|
||||
{
|
||||
foreach ($files as $file)
|
||||
{
|
||||
// Merge each file to the configuration array
|
||||
$config = Arr::merge($config, Kohana::load($file));
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
} // End Kohana_Config
|
130
system/classes/Kohana/Config/Group.php
Normal file
130
system/classes/Kohana/Config/Group.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* The group wrapper acts as an interface to all the config directives
|
||||
* gathered from across the system.
|
||||
*
|
||||
* This is the object returned from Kohana_Config::load
|
||||
*
|
||||
* Any modifications to configuration items should be done through an instance of this object
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Config_Group extends ArrayObject {
|
||||
|
||||
/**
|
||||
* Reference the config object that created this group
|
||||
* Used when updating config
|
||||
* @var Kohana_Config
|
||||
*/
|
||||
protected $_parent_instance = NULL;
|
||||
|
||||
/**
|
||||
* The group this config is for
|
||||
* Used when updating config items
|
||||
* @var string
|
||||
*/
|
||||
protected $_group_name = '';
|
||||
|
||||
/**
|
||||
* Constructs the group object. Kohana_Config passes the config group
|
||||
* and its config items to the object here.
|
||||
*
|
||||
* @param Kohana_Config $instance "Owning" instance of Kohana_Config
|
||||
* @param string $group The group name
|
||||
* @param array $config Group's config
|
||||
*/
|
||||
public function __construct(Kohana_Config $instance, $group, array $config = array())
|
||||
{
|
||||
$this->_parent_instance = $instance;
|
||||
$this->_group_name = $group;
|
||||
|
||||
parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current group in serialized form.
|
||||
*
|
||||
* echo $config;
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return serialize($this->getArrayCopy());
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for getArrayCopy()
|
||||
*
|
||||
* @return array Array copy of the group's config
|
||||
*/
|
||||
public function as_array()
|
||||
{
|
||||
return $this->getArrayCopy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the config group's name
|
||||
*
|
||||
* @return string The group name
|
||||
*/
|
||||
public function group_name()
|
||||
{
|
||||
return $this->_group_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a variable from the configuration or return the default value.
|
||||
*
|
||||
* $value = $config->get($key);
|
||||
*
|
||||
* @param string $key array key
|
||||
* @param mixed $default default value
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = NULL)
|
||||
{
|
||||
return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value in the configuration array.
|
||||
*
|
||||
* $config->set($key, $new_value);
|
||||
*
|
||||
* @param string $key array key
|
||||
* @param mixed $value array value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->offsetSet($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides ArrayObject::offsetSet()
|
||||
* This method is called when config is changed via
|
||||
*
|
||||
* $config->var = 'asd';
|
||||
*
|
||||
* // OR
|
||||
*
|
||||
* $config['var'] = 'asd';
|
||||
*
|
||||
* @param string $key The key of the config item we're changing
|
||||
* @param mixed $value The new array value
|
||||
*/
|
||||
public function offsetSet($key, $value)
|
||||
{
|
||||
$this->_parent_instance->_write_config($this->_group_name, $key, $value);
|
||||
|
||||
return parent::offsetSet($key, $value);
|
||||
}
|
||||
}
|
25
system/classes/Kohana/Config/Reader.php
Normal file
25
system/classes/Kohana/Config/Reader.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Interface for config readers
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
interface Kohana_Config_Reader extends Kohana_Config_Source
|
||||
{
|
||||
|
||||
/**
|
||||
* Tries to load the specificed configuration group
|
||||
*
|
||||
* Returns FALSE if group does not exist or an array if it does
|
||||
*
|
||||
* @param string $group Configuration group
|
||||
* @return boolean|array
|
||||
*/
|
||||
public function load($group);
|
||||
|
||||
}
|
14
system/classes/Kohana/Config/Source.php
Normal file
14
system/classes/Kohana/Config/Source.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Base Config source Interface
|
||||
*
|
||||
* Used to identify either config readers or writers when calling [Kohana_Config::attach()]
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
|
||||
interface Kohana_Config_Source {}
|
27
system/classes/Kohana/Config/Writer.php
Normal file
27
system/classes/Kohana/Config/Writer.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Interface for config writers
|
||||
*
|
||||
* Specifies the methods that a config writer must implement
|
||||
*
|
||||
* @package Kohana
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2012 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
interface Kohana_Config_Writer extends Kohana_Config_Source
|
||||
{
|
||||
/**
|
||||
* Writes the passed config for $group
|
||||
*
|
||||
* Returns chainable instance on success or throws
|
||||
* Kohana_Config_Exception on failure
|
||||
*
|
||||
* @param string $group The config group
|
||||
* @param string $key The config key to write to
|
||||
* @param array $config The configuration to write
|
||||
* @return boolean
|
||||
*/
|
||||
public function write($group, $key, $config);
|
||||
}
|
Reference in New Issue
Block a user