2010-12-04 17:15:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* IniReader
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-04 17:15:47 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-04 17:15:47 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Configure
|
2010-12-04 17:15:47 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2012-04-20 01:53:45 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2010-12-04 17:15:47 +00:00
|
|
|
|
|
|
|
/**
|
2012-04-20 01:53:45 +00:00
|
|
|
* Ini file configuration engine.
|
2010-12-04 17:15:47 +00:00
|
|
|
*
|
2012-04-20 01:53:45 +00:00
|
|
|
* Since IniReader uses parse_ini_file underneath, you should be aware that this
|
|
|
|
* class shares the same behavior, especially with regards to boolean and null values.
|
|
|
|
*
|
|
|
|
* In addition to the native `parse_ini_file` features, IniReader also allows you
|
2011-07-26 01:46:23 +00:00
|
|
|
* to create nested array structures through usage of `.` delimited names. This allows
|
2010-12-04 17:56:37 +00:00
|
|
|
* you to create nested arrays structures in an ini config file. For example:
|
|
|
|
*
|
|
|
|
* `db.password = secret` would turn into `array('db' => array('password' => 'secret'))`
|
2011-03-19 17:00:24 +00:00
|
|
|
*
|
2011-07-26 01:46:23 +00:00
|
|
|
* You can nest properties as deeply as needed using `.`'s. In addition to using `.` you
|
|
|
|
* can use standard ini section notation to create nested structures:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* [section]
|
|
|
|
* key = value
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* Once loaded into Configure, the above would be accessed using:
|
|
|
|
*
|
|
|
|
* `Configure::read('section.key');
|
|
|
|
*
|
|
|
|
* You can combine `.` separated values with sections to create more deeply
|
|
|
|
* nested structures.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* IniReader also manipulates how the special ini values of
|
|
|
|
* 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
|
2011-07-26 01:46:23 +00:00
|
|
|
* converted to their boolean equivalents.
|
2010-12-04 17:56:37 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Configure
|
2010-12-04 17:15:47 +00:00
|
|
|
* @see http://php.net/parse_ini_file
|
|
|
|
*/
|
|
|
|
class IniReader implements ConfigReaderInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The path to read ini files from.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The section to read, if null all sections will be read.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_section;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and construct a new ini file parser. The parser can be used to read
|
|
|
|
* ini files that are on the filesystem.
|
|
|
|
*
|
|
|
|
* @param string $path Path to load ini config files from.
|
2011-07-26 01:46:23 +00:00
|
|
|
* @param string $section Only get one section, leave null to parse and fetch
|
|
|
|
* all sections in the ini file.
|
2010-12-04 17:15:47 +00:00
|
|
|
*/
|
|
|
|
public function __construct($path, $section = null) {
|
|
|
|
$this->_path = $path;
|
|
|
|
$this->_section = $section;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an ini file and return the results as an array.
|
|
|
|
*
|
2011-07-26 01:46:23 +00:00
|
|
|
* @param string $file Name of the file to read. The chosen file
|
|
|
|
* must be on the reader's path.
|
2010-12-04 17:15:47 +00:00
|
|
|
* @return array
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws ConfigureException
|
2010-12-04 17:15:47 +00:00
|
|
|
*/
|
|
|
|
public function read($file) {
|
|
|
|
$filename = $this->_path . $file;
|
2011-01-23 22:27:46 +00:00
|
|
|
if (!file_exists($filename)) {
|
|
|
|
$filename .= '.ini';
|
|
|
|
if (!file_exists($filename)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', substr($filename, 0, -4), $filename));
|
2011-01-23 22:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-04 17:15:47 +00:00
|
|
|
$contents = parse_ini_file($filename, true);
|
|
|
|
if (!empty($this->_section) && isset($contents[$this->_section])) {
|
2010-12-04 17:56:37 +00:00
|
|
|
$values = $this->_parseNestedValues($contents[$this->_section]);
|
2010-12-04 17:15:47 +00:00
|
|
|
} else {
|
2010-12-04 17:56:37 +00:00
|
|
|
$values = array();
|
|
|
|
foreach ($contents as $section => $attribs) {
|
2011-01-23 21:26:13 +00:00
|
|
|
if (is_array($attribs)) {
|
|
|
|
$values[$section] = $this->_parseNestedValues($attribs);
|
|
|
|
} else {
|
|
|
|
$parse = $this->_parseNestedValues(array($attribs));
|
|
|
|
$values[$section] = array_shift($parse);
|
|
|
|
}
|
2010-12-04 17:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parses nested values out of keys.
|
|
|
|
*
|
|
|
|
* @param array $values Values to be exploded.
|
|
|
|
* @return array Array of values exploded
|
|
|
|
*/
|
|
|
|
protected function _parseNestedValues($values) {
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
if ($value === '1') {
|
|
|
|
$value = true;
|
|
|
|
}
|
|
|
|
if ($value === '') {
|
|
|
|
$value = false;
|
|
|
|
}
|
|
|
|
if (strpos($key, '.') !== false) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$values = Hash::insert($values, $key, $value);
|
2010-12-04 17:56:37 +00:00
|
|
|
} else {
|
|
|
|
$values[$key] = $value;
|
|
|
|
}
|
2010-12-04 17:15:47 +00:00
|
|
|
}
|
|
|
|
return $values;
|
|
|
|
}
|
2012-03-03 22:36:59 +00:00
|
|
|
|
2012-04-20 01:53:45 +00:00
|
|
|
/**
|
|
|
|
* Dumps the state of Configure data into an ini formatted string.
|
|
|
|
*
|
2012-04-22 00:26:35 +00:00
|
|
|
* @param string $filename The filename on $this->_path to save into.
|
2012-04-20 01:53:45 +00:00
|
|
|
* @param array $data The data to convert to ini file.
|
2012-04-22 00:26:35 +00:00
|
|
|
* @return int Bytes saved.
|
2012-04-20 01:53:45 +00:00
|
|
|
*/
|
2012-04-22 00:26:35 +00:00
|
|
|
public function dump($filename, $data) {
|
2012-04-20 01:53:45 +00:00
|
|
|
$result = array();
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
if ($key[0] != '[') {
|
|
|
|
$result[] = "[$key]";
|
|
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
|
|
$keyValues = Hash::flatten($value, '.');
|
|
|
|
foreach ($keyValues as $k => $v) {
|
|
|
|
$result[] = "$k = " . trim(var_export($v, true), "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 00:26:35 +00:00
|
|
|
$contents = join("\n", $result);
|
|
|
|
return file_put_contents($this->_path . $filename, $contents);
|
2012-04-20 01:53:45 +00:00
|
|
|
}
|
2011-07-26 01:46:23 +00:00
|
|
|
}
|