Skip to content

Commit

Permalink
Merge pull request #1577 from ADmad/feature/configure-engine
Browse files Browse the repository at this point in the history
Feature/configure engine
  • Loading branch information
markstory committed Aug 31, 2013
2 parents 8243f8e + 2ec29e8 commit ab83f42
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 178 deletions.
Expand Up @@ -17,10 +17,8 @@

/**
* An interface for creating objects compatible with Configure::load()
*
* @package Cake.Core
*/
interface ConfigReaderInterface {
interface ConfigEngineInterface {

/**
* Read method is used for reading configuration information from sources.
Expand Down
@@ -1,9 +1,5 @@
<?php
/**
* IniReader
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -13,23 +9,23 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Configure
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Configure;
namespace Cake\Configure\Engine;

use Cake\Configure\ConfigEngineInterface;
use Cake\Core\App;
use Cake\Error;
use Cake\Utility\Hash;

/**
* Ini file configuration engine.
*
* Since IniReader uses parse_ini_file underneath, you should be aware that this
* Since IniConfig 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
* In addition to the native `parse_ini_file` features, IniConfig also allows you
* to create nested array structures through usage of `.` delimited names. This allows
* you to create nested arrays structures in an ini config file. For example:
*
Expand All @@ -50,14 +46,13 @@
* You can combine `.` separated values with sections to create more deeply
* nested structures.
*
* IniReader also manipulates how the special ini values of
* IniConfig also manipulates how the special ini values of
* 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
* converted to their boolean equivalents.
*
* @package Cake.Configure
* @see http://php.net/parse_ini_file
*/
class IniReader implements ConfigReaderInterface {
class IniConfig implements ConfigEngineInterface {

/**
* The path to read ini files from.
Expand Down Expand Up @@ -95,7 +90,7 @@ public function __construct($path = null, $section = null) {
* For backwards compatibility, acl.ini.php will be treated specially until 3.0.
*
* @param string $key The identifier to read from. If the key has a . it will be treated
* as a plugin prefix. The chosen file must be on the reader's path.
* as a plugin prefix. The chosen file must be on the engine's path.
* @return array Parsed configuration values.
* @throws Cake\Error\ConfigureException when files don't exist.
* Or when files contain '..' as this could lead to abusive reads.
Expand Down
@@ -1,39 +1,34 @@
<?php
/**
* PhpReader file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
* @package Cake.Configure
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Configure;
namespace Cake\Configure\Engine;

use Cake\Configure\ConfigEngineInterface;
use Cake\Core\App;
use Cake\Error;

/**
* PHP Reader allows Configure to load configuration values from
* PHP engine allows Configure to load configuration values from
* files containing simple PHP arrays.
*
* Files compatible with PhpReader should define a `$config` variable, that
* Files compatible with PhpConfig should define a `$config` variable, that
* contains all of the configuration data contained in the file.
*
* @package Cake.Configure
*/
class PhpReader implements ConfigReaderInterface {
class PhpConfig implements ConfigEngineInterface {

/**
* The path this reader finds files on.
* The path this engine finds files on.
*
* @var string
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Controller/Component/Acl/IniAcl.php
Expand Up @@ -15,7 +15,7 @@
*/
namespace Cake\Controller\Component\Acl;

use Cake\Configure\IniReader;
use Cake\Configure\Engine\IniConfig;
use Cake\Controller\Component;
use Cake\Core\Object;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -156,7 +156,7 @@ public function check($aro, $aco, $action = null) {
* @return array INI section structure
*/
public function readConfigFile($filename) {
$iniFile = new IniReader(dirname($filename) . DS);
$iniFile = new IniConfig(dirname($filename) . DS);
return $iniFile->read(basename($filename));
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Controller/Component/Acl/PhpAcl.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Controller\Component\Acl;

use Cake\Configure\PhpReader;
use Cake\Configure\Engine\PhpConfig;
use Cake\Controller\Component;
use Cake\Core\Object;
use Cake\Error;
Expand Down Expand Up @@ -85,8 +85,8 @@ public function initialize(Component $Component) {
$this->options = array_merge($this->options, $Component->settings['adapter']);
}

$Reader = new PhpReader(dirname($this->options['config']) . DS);
$config = $Reader->read(basename($this->options['config']));
$engine = new PhpConfig(dirname($this->options['config']) . DS);
$config = $engine->read(basename($this->options['config']));
$this->build($config);
$Component->Aco = $this->Aco;
$Component->Aro = $this->Aro;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Controller/Component/AclComponent.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Controller\Component;

use Cake\Configure\IniReader;
use Cake\Configure\Engine\IniConfig;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Component\Acl\AclInterface;
Expand Down
96 changes: 48 additions & 48 deletions lib/Cake/Core/Configure.php
Expand Up @@ -16,8 +16,8 @@
namespace Cake\Core;

use Cake\Cache\Cache;
use Cake\Configure\ConfigReaderInterface;
use Cake\Configure\PhpReader;
use Cake\Configure\ConfigEngineInterface;
use Cake\Configure\Engine\PhpConfig;
use Cake\Error;
use Cake\Utility\Hash;

Expand All @@ -43,12 +43,12 @@ class Configure {
);

/**
* Configured reader classes, used to load config files from resources
* Configured engine classes, used to load config files from resources
*
* @var array
* @see Configure::load()
*/
protected static $_readers = array();
protected static $_engines = array();

/**
* Used to store a dynamic variable in Configure.
Expand Down Expand Up @@ -145,82 +145,82 @@ public static function delete($var = null) {
}

/**
* Add a new reader to Configure. Readers allow you to read configuration
* files in various formats/storage locations. CakePHP comes with two built-in readers
* PhpReader and IniReader. You can also implement your own reader classes in your application.
* Add a new engine to Configure. Engines allow you to read configuration
* files in various formats/storage locations. CakePHP comes with two built-in engines
* PhpConfig and IniConfig. You can also implement your own engine classes in your application.
*
* To add a new reader to Configure:
* To add a new engine to Configure:
*
* `Configure::config('ini', new IniReader());`
* `Configure::config('ini', new IniConfig());`
*
* @param string $name The name of the reader being configured. This alias is used later to
* read values from a specific reader.
* @param ConfigReaderInterface $reader The reader to append.
* @param string $name The name of the engine being configured. This alias is used later to
* read values from a specific engine.
* @param ConfigEngineInterface $engine The engine to append.
* @return void
*/
public static function config($name, ConfigReaderInterface $reader) {
static::$_readers[$name] = $reader;
public static function config($name, ConfigEngineInterface $engine) {
static::$_engines[$name] = $engine;
}

/**
* Gets the names of the configured reader objects.
* Gets the names of the configured Engine objects.
*
* @param string $name
* @return array Array of the configured reader objects.
* @return array Array of the configured Engine objects.
*/
public static function configured($name = null) {
if ($name) {
return isset(static::$_readers[$name]);
return isset(static::$_engines[$name]);
}
return array_keys(static::$_readers);
return array_keys(static::$_engines);
}

/**
* Remove a configured reader. This will unset the reader
* Remove a configured engine. This will unset the engine
* and make any future attempts to use it cause an Exception.
*
* @param string $name Name of the reader to drop.
* @param string $name Name of the engine to drop.
* @return boolean Success
*/
public static function drop($name) {
if (!isset(static::$_readers[$name])) {
if (!isset(static::$_engines[$name])) {
return false;
}
unset(static::$_readers[$name]);
unset(static::$_engines[$name]);
return true;
}

/**
* Loads stored configuration information from a resource. You can add
* config file resource readers with `Configure::config()`.
* config file resource engines with `Configure::config()`.
*
* Loaded configuration information will be merged with the current
* runtime configuration. You can load configuration files from plugins
* by preceding the filename with the plugin name.
*
* `Configure::load('Users.user', 'default')`
*
* Would load the 'user' config file using the default config reader. You can load
* Would load the 'user' config file using the default config engine. You can load
* app config files by giving the name of the resource you want loaded.
*
* `Configure::load('setup', 'default');`
*
* If using `default` config and no reader has been configured for it yet,
* one will be automatically created using PhpReader
* If using `default` config and no engine has been configured for it yet,
* one will be automatically created using PhpConfig
*
* @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
* @param string $key name of configuration resource to load.
* @param string $config Name of the configured reader to use to read the resource identified by $key.
* @param string $config Name of the configured engine to use to read the resource identified by $key.
* @param boolean $merge if config files should be merged instead of simply overridden
* @return mixed false if file not found, void if load successful.
* @throws Cake\Error\ConfigureException Will throw any exceptions the reader raises.
* @throws Cake\Error\ConfigureException Will throw any exceptions the engine raises.
*/
public static function load($key, $config = 'default', $merge = true) {
$reader = static::_getReader($config);
if (!$reader) {
$engine = static::_getEngine($config);
if (!$engine) {
return false;
}
$values = $reader->read($key);
$values = $engine->read($key);

if ($merge) {
$keys = array_keys($values);
Expand All @@ -236,13 +236,13 @@ public static function load($key, $config = 'default', $merge = true) {

/**
* Dump data currently in Configure into $key. The serialization format
* is decided by the config reader attached as $config. For example, if the
* 'default' adapter is a PhpReader, the generated file will be a PHP
* configuration file loadable by the PhpReader.
* is decided by the config engine attached as $config. For example, if the
* 'default' adapter is a PhpConfig, the generated file will be a PHP
* configuration file loadable by the PhpConfig.
*
* ## Usage
*
* Given that the 'default' reader is an instance of PhpReader.
* Given that the 'default' engine is an instance of PhpConfig.
* Save all data in Configure to the file `my_config.php`:
*
* `Configure::dump('my_config.php', 'default');`
Expand All @@ -260,35 +260,35 @@ public static function load($key, $config = 'default', $merge = true) {
* @throws Cake\Error\ConfigureException if the adapter does not implement a `dump` method.
*/
public static function dump($key, $config = 'default', $keys = array()) {
$reader = static::_getReader($config);
if (!$reader) {
throw new Error\ConfigureException(__d('cake_dev', 'There is no "%s" adapter.', $config));
$engine = static::_getEngine($config);
if (!$engine) {
throw new Error\ConfigureException(__d('cake_dev', 'There is no "%s" config engine.', $config));
}
if (!method_exists($reader, 'dump')) {
throw new Error\ConfigureException(__d('cake_dev', 'The "%s" adapter, does not have a %s method.', $config, 'dump()'));
if (!method_exists($engine, 'dump')) {
throw new Error\ConfigureException(__d('cake_dev', 'The "%s" config engine, does not have a %s method.', $config, 'dump()'));
}
$values = static::$_values;
if (!empty($keys) && is_array($keys)) {
$values = array_intersect_key($values, array_flip($keys));
}
return (bool)$reader->dump($key, $values);
return (bool)$engine->dump($key, $values);
}

/**
* Get the configured reader. Internally used by `Configure::load()` and `Configure::dump()`
* Will create new PhpReader for default if not configured yet.
* Get the configured engine. Internally used by `Configure::load()` and `Configure::dump()`
* Will create new PhpConfig for default if not configured yet.
*
* @param string $config The name of the configured adapter
* @return mixed Reader instance or false
* @return mixed Engine instance or false
*/
protected static function _getReader($config) {
if (!isset(static::$_readers[$config])) {
protected static function _getEngine($config) {
if (!isset(static::$_engines[$config])) {
if ($config !== 'default') {
return false;
}
static::config($config, new PhpReader());
static::config($config, new PhpConfig());
}
return static::$_readers[$config];
return static::$_engines[$config];
}

/**
Expand Down

0 comments on commit ab83f42

Please sign in to comment.