Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added ConfigReaderInterface::dump() and made all readers' dump() method ... #992

Merged
merged 1 commit into from

2 participants

@ADmad
Collaborator

...support 'Plugin.keyname' format for keys

Refs 3363

@markstory markstory commented on the diff
lib/Cake/Configure/ConfigReaderInterface.php
@@ -30,4 +30,13 @@
*/
public function read($key);
+/**
+ * Dumps the configure data into source.
+ *
+ * @param string $key The identifier to write to.
+ * @param array $data The data to dump.
+ * @return boolean True on success or false on failure.
+ */
+ public function dump($key, $data);
@markstory Owner

Should this be added? It will make config adapters that used to work in 2.2 no longer work in 2.3.

@ADmad Collaborator
ADmad added a note

Since both our core readers have dump() and Configure::dump() does no checks before trying to call a reader's dump() method whether the method actually exists, I thought it would be best to add this method to the interface. We can document this in the migration guide. People using custom readers would be a very small minority anyway.

@markstory Owner

I guess there are probably not many custom readers out there, as long as we document it well I'm ok with the interface change.

@ADmad Collaborator
ADmad added a note

I will ensure the docs are properly updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory
Owner

Other than the change in the interface this looks good to me.

@ADmad ADmad merged commit 2bc5988 into cakephp:2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 30, 2012
  1. @ADmad

    Added ConfigReaderInterface::dump() and made all readers' dump() meth…

    ADmad authored
    …od support 'Plugin.keyname' format for keys. Closes #3363
This page is out of date. Refresh to see the latest.
View
9 lib/Cake/Configure/ConfigReaderInterface.php
@@ -30,4 +30,13 @@
*/
public function read($key);
+/**
+ * Dumps the configure data into source.
+ *
+ * @param string $key The identifier to write to.
+ * @param array $data The data to dump.
+ * @return boolean True on success or false on failure.
+ */
+ public function dump($key, $data);
@markstory Owner

Should this be added? It will make config adapters that used to work in 2.2 no longer work in 2.3.

@ADmad Collaborator
ADmad added a note

Since both our core readers have dump() and Configure::dump() does no checks before trying to call a reader's dump() method whether the method actually exists, I thought it would be best to add this method to the interface. We can document this in the migration guide. People using custom readers would be a very small minority anyway.

@markstory Owner

I guess there are probably not many custom readers out there, as long as we document it well I'm ok with the interface change.

@ADmad Collaborator
ADmad added a note

I will ensure the docs are properly updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
}
View
70 lib/Cake/Configure/IniReader.php
@@ -100,23 +100,8 @@ public function read($key) {
if (strpos($key, '..') !== false) {
throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
}
- if (substr($key, -8) === '.ini.php') {
- $key = substr($key, 0, -8);
- list($plugin, $key) = pluginSplit($key);
- $key .= '.ini.php';
- } else {
- if (substr($key, -4) === '.ini') {
- $key = substr($key, 0, -4);
- }
- list($plugin, $key) = pluginSplit($key);
- $key .= '.ini';
- }
- if ($plugin) {
- $file = App::pluginPath($plugin) . 'Config' . DS . $key;
- } else {
- $file = $this->_path . $key;
- }
+ $file = $this->_getFilePath($key);
if (!is_file($file)) {
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
}
@@ -165,23 +150,23 @@ protected function _parseNestedValues($values) {
/**
* Dumps the state of Configure data into an ini formatted string.
*
- * @param string $filename The filename on $this->_path to save into.
- * Extension ".ini" will be automatically appended if not included in filename.
+ * @param string $key The identifier to write to. If the key has a . it will be treated
+ * as a plugin prefix.
* @param array $data The data to convert to ini file.
* @return int Bytes saved.
*/
- public function dump($filename, $data) {
+ public function dump($key, $data) {
$result = array();
- foreach ($data as $key => $value) {
+ foreach ($data as $k => $value) {
$isSection = false;
- if ($key[0] != '[') {
- $result[] = "[$key]";
+ if ($k[0] != '[') {
+ $result[] = "[$k]";
$isSection = true;
}
if (is_array($value)) {
- $keyValues = Hash::flatten($value, '.');
- foreach ($keyValues as $k => $v) {
- $result[] = "$k = " . $this->_value($v);
+ $kValues = Hash::flatten($value, '.');
+ foreach ($kValues as $k2 => $v) {
+ $result[] = "$k2 = " . $this->_value($v);
}
}
if ($isSection) {
@@ -190,10 +175,8 @@ public function dump($filename, $data) {
}
$contents = trim(implode("\n", $result));
- if (substr($filename, -4) !== '.ini') {
- $filename .= '.ini';
- }
- return file_put_contents($this->_path . $filename, $contents);
+ $filename = $this->_getFilePath($key);
+ return file_put_contents($filename, $contents);
}
/**
@@ -215,4 +198,33 @@ protected function _value($val) {
return (string)$val;
}
+/**
+ * Get file path
+ *
+ * @param string $key The identifier to write to. If the key has a . it will be treated
+ * as a plugin prefix.
+ * @return string Full file path
+ */
+ protected function _getFilePath($key) {
+ if (substr($key, -8) === '.ini.php') {
+ $key = substr($key, 0, -8);
+ list($plugin, $key) = pluginSplit($key);
+ $key .= '.ini.php';
+ } else {
+ if (substr($key, -4) === '.ini') {
+ $key = substr($key, 0, -4);
+ }
+ list($plugin, $key) = pluginSplit($key);
+ $key .= '.ini';
+ }
+
+ if ($plugin) {
+ $file = App::pluginPath($plugin) . 'Config' . DS . $key;
+ } else {
+ $file = $this->_path . $key;
+ }
+
+ return $file;
+ }
+
}
View
46 lib/Cake/Configure/PhpReader.php
@@ -52,7 +52,7 @@ public function __construct($path = null) {
* Files with `.` in the name will be treated as values in plugins. Instead of reading from
* the initialized path, plugin keys will be located using App::pluginPath().
*
- * @param string $key The identifier to read from. If the key has a . it will be treated
+ * @param string $key The identifier to read from. If the key has a . it will be treated
* as a plugin prefix.
* @return array Parsed configuration values.
* @throws ConfigureException when files don't exist or they don't contain `$config`.
@@ -62,17 +62,8 @@ public function read($key) {
if (strpos($key, '..') !== false) {
throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
}
- if (substr($key, -4) === '.php') {
- $key = substr($key, 0, -4);
- }
- list($plugin, $key) = pluginSplit($key);
- $key .= '.php';
- if ($plugin) {
- $file = App::pluginPath($plugin) . 'Config' . DS . $key;
- } else {
- $file = $this->_path . $key;
- }
+ $file = $this->_getFilePath($key);
if (!is_file($file)) {
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
}
@@ -90,18 +81,39 @@ public function read($key) {
* Converts the provided $data into a string of PHP code that can
* be used saved into a file and loaded later.
*
- * @param string $filename The filename to create on $this->_path.
- * Extension ".php" will be automatically appended if not included in filename.
+ * @param string $key The identifier to write to. If the key has a . it will be treated
+ * as a plugin prefix.
* @param array $data Data to dump.
* @return int Bytes saved.
*/
- public function dump($filename, $data) {
+ public function dump($key, $data) {
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
- if (substr($filename, -4) !== '.php') {
- $filename .= '.php';
+ $filename = $this->_getFilePath($key);
+ return file_put_contents($filename, $contents);
+ }
+
+/**
+ * Get file path
+ *
+ * @param string $key The identifier to write to. If the key has a . it will be treated
+ * as a plugin prefix.
+ * @return string Full file path
+ */
+ protected function _getFilePath($key) {
+ if (substr($key, -4) === '.php') {
+ $key = substr($key, 0, -4);
+ }
+ list($plugin, $key) = pluginSplit($key);
+ $key .= '.php';
+
+ if ($plugin) {
+ $file = App::pluginPath($plugin) . 'Config' . DS . $key;
+ } else {
+ $file = $this->_path . $key;
}
- return file_put_contents($this->_path . $filename, $contents);
+
+ return $file;
}
}
View
2  lib/Cake/Core/Configure.php
@@ -292,7 +292,7 @@ public static function load($key, $config = 'default', $merge = true) {
}
/**
- * Dump data currently in Configure into $filename. The serialization format
+ * 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.
Something went wrong with that request. Please try again.