mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Added ConfigReaderInterface::dump() and made all readers' dump() method support 'Plugin.keyname' format for keys. Closes #3363
This commit is contained in:
parent
4a6ebaa07b
commit
2bc5988674
4 changed files with 80 additions and 47 deletions
|
@ -30,4 +30,13 @@ interface ConfigReaderInterface {
|
||||||
*/
|
*/
|
||||||
public function read($key);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,23 +100,8 @@ class IniReader implements ConfigReaderInterface {
|
||||||
if (strpos($key, '..') !== false) {
|
if (strpos($key, '..') !== false) {
|
||||||
throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
|
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 = $this->_getFilePath($key);
|
||||||
$file = App::pluginPath($plugin) . 'Config' . DS . $key;
|
|
||||||
} else {
|
|
||||||
$file = $this->_path . $key;
|
|
||||||
}
|
|
||||||
if (!is_file($file)) {
|
if (!is_file($file)) {
|
||||||
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
|
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
|
||||||
}
|
}
|
||||||
|
@ -165,23 +150,23 @@ class IniReader implements ConfigReaderInterface {
|
||||||
/**
|
/**
|
||||||
* Dumps the state of Configure data into an ini formatted string.
|
* Dumps the state of Configure data into an ini formatted string.
|
||||||
*
|
*
|
||||||
* @param string $filename The filename on $this->_path to save into.
|
* @param string $key The identifier to write to. If the key has a . it will be treated
|
||||||
* Extension ".ini" will be automatically appended if not included in filename.
|
* as a plugin prefix.
|
||||||
* @param array $data The data to convert to ini file.
|
* @param array $data The data to convert to ini file.
|
||||||
* @return int Bytes saved.
|
* @return int Bytes saved.
|
||||||
*/
|
*/
|
||||||
public function dump($filename, $data) {
|
public function dump($key, $data) {
|
||||||
$result = array();
|
$result = array();
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $k => $value) {
|
||||||
$isSection = false;
|
$isSection = false;
|
||||||
if ($key[0] != '[') {
|
if ($k[0] != '[') {
|
||||||
$result[] = "[$key]";
|
$result[] = "[$k]";
|
||||||
$isSection = true;
|
$isSection = true;
|
||||||
}
|
}
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$keyValues = Hash::flatten($value, '.');
|
$kValues = Hash::flatten($value, '.');
|
||||||
foreach ($keyValues as $k => $v) {
|
foreach ($kValues as $k2 => $v) {
|
||||||
$result[] = "$k = " . $this->_value($v);
|
$result[] = "$k2 = " . $this->_value($v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($isSection) {
|
if ($isSection) {
|
||||||
|
@ -190,10 +175,8 @@ class IniReader implements ConfigReaderInterface {
|
||||||
}
|
}
|
||||||
$contents = trim(implode("\n", $result));
|
$contents = trim(implode("\n", $result));
|
||||||
|
|
||||||
if (substr($filename, -4) !== '.ini') {
|
$filename = $this->_getFilePath($key);
|
||||||
$filename .= '.ini';
|
return file_put_contents($filename, $contents);
|
||||||
}
|
|
||||||
return file_put_contents($this->_path . $filename, $contents);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -215,4 +198,33 @@ class IniReader implements ConfigReaderInterface {
|
||||||
return (string)$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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ class PhpReader implements ConfigReaderInterface {
|
||||||
* Files with `.` in the name will be treated as values in plugins. Instead of reading from
|
* 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().
|
* 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.
|
* as a plugin prefix.
|
||||||
* @return array Parsed configuration values.
|
* @return array Parsed configuration values.
|
||||||
* @throws ConfigureException when files don't exist or they don't contain `$config`.
|
* @throws ConfigureException when files don't exist or they don't contain `$config`.
|
||||||
|
@ -62,17 +62,8 @@ class PhpReader implements ConfigReaderInterface {
|
||||||
if (strpos($key, '..') !== false) {
|
if (strpos($key, '..') !== false) {
|
||||||
throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
|
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 = $this->_getFilePath($key);
|
||||||
$file = App::pluginPath($plugin) . 'Config' . DS . $key;
|
|
||||||
} else {
|
|
||||||
$file = $this->_path . $key;
|
|
||||||
}
|
|
||||||
if (!is_file($file)) {
|
if (!is_file($file)) {
|
||||||
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
|
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
|
||||||
}
|
}
|
||||||
|
@ -90,18 +81,39 @@ class PhpReader implements ConfigReaderInterface {
|
||||||
* Converts the provided $data into a string of PHP code that can
|
* Converts the provided $data into a string of PHP code that can
|
||||||
* be used saved into a file and loaded later.
|
* be used saved into a file and loaded later.
|
||||||
*
|
*
|
||||||
* @param string $filename The filename to create on $this->_path.
|
* @param string $key The identifier to write to. If the key has a . it will be treated
|
||||||
* Extension ".php" will be automatically appended if not included in filename.
|
* as a plugin prefix.
|
||||||
* @param array $data Data to dump.
|
* @param array $data Data to dump.
|
||||||
* @return int Bytes saved.
|
* @return int Bytes saved.
|
||||||
*/
|
*/
|
||||||
public function dump($filename, $data) {
|
public function dump($key, $data) {
|
||||||
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
|
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
|
||||||
|
|
||||||
if (substr($filename, -4) !== '.php') {
|
$filename = $this->_getFilePath($key);
|
||||||
$filename .= '.php';
|
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);
|
||||||
}
|
}
|
||||||
return file_put_contents($this->_path . $filename, $contents);
|
list($plugin, $key) = pluginSplit($key);
|
||||||
|
$key .= '.php';
|
||||||
|
|
||||||
|
if ($plugin) {
|
||||||
|
$file = App::pluginPath($plugin) . 'Config' . DS . $key;
|
||||||
|
} else {
|
||||||
|
$file = $this->_path . $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -292,7 +292,7 @@ class Configure {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* 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
|
* 'default' adapter is a PhpReader, the generated file will be a PHP
|
||||||
* configuration file loadable by the PhpReader.
|
* configuration file loadable by the PhpReader.
|
||||||
|
|
Loading…
Reference in a new issue