2010-12-03 04:06:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PhpReader file
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-12-03 04:06:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-12-03 04:06:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Configure
|
2010-12-03 04:06:08 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-12-03 04:06:08 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-24 09:52:29 +00:00
|
|
|
App::uses('CakePlugin', 'Core');
|
|
|
|
|
2010-12-03 04:06:08 +00:00
|
|
|
/**
|
2011-03-19 17:00:24 +00:00
|
|
|
* PHP Reader allows Configure to load configuration values from
|
2010-12-03 04:06:08 +00:00
|
|
|
* files containing simple PHP arrays.
|
|
|
|
*
|
2011-07-26 01:46:23 +00:00
|
|
|
* Files compatible with PhpReader should define a `$config` variable, that
|
|
|
|
* contains all of the configuration data contained in the file.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Configure
|
2010-12-03 04:06:08 +00:00
|
|
|
*/
|
2010-12-04 05:09:11 +00:00
|
|
|
class PhpReader implements ConfigReaderInterface {
|
2012-03-03 22:36:59 +00:00
|
|
|
|
2010-12-03 04:06:08 +00:00
|
|
|
/**
|
|
|
|
* The path this reader finds files on.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_path = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for PHP Config file reading.
|
|
|
|
*
|
2017-05-12 06:02:36 +00:00
|
|
|
* @param string $path The path to read config files from. Defaults to CONFIG
|
2010-12-03 04:06:08 +00:00
|
|
|
*/
|
2011-04-17 11:13:02 +00:00
|
|
|
public function __construct($path = null) {
|
|
|
|
if (!$path) {
|
2017-05-12 06:02:36 +00:00
|
|
|
$path = CONFIG;
|
2011-04-17 11:13:02 +00:00
|
|
|
}
|
2010-12-03 04:06:08 +00:00
|
|
|
$this->_path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a config file and return its contents.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Files with `.` in the name will be treated as values in plugins. Instead of reading from
|
2014-06-24 09:52:29 +00:00
|
|
|
* the initialized path, plugin keys will be located using CakePlugin::path().
|
2010-12-03 04:06:08 +00:00
|
|
|
*
|
2012-11-18 14:57:58 +00:00
|
|
|
* @param string $key The identifier to read from. If the key has a . it will be treated
|
2011-07-26 01:46:23 +00:00
|
|
|
* as a plugin prefix.
|
2010-12-03 04:06:08 +00:00
|
|
|
* @return array Parsed configuration values.
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws ConfigureException when files don't exist or they don't contain `$config`.
|
|
|
|
* Or when files contain '..' as this could lead to abusive reads.
|
2010-12-03 04:06:08 +00:00
|
|
|
*/
|
|
|
|
public function read($key) {
|
2010-12-04 05:58:02 +00:00
|
|
|
if (strpos($key, '..') !== false) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
|
2010-12-04 05:58:02 +00:00
|
|
|
}
|
2011-03-19 17:00:24 +00:00
|
|
|
|
2012-11-18 14:57:58 +00:00
|
|
|
$file = $this->_getFilePath($key);
|
2016-02-05 10:11:28 +00:00
|
|
|
if (!is_file(realpath($file))) {
|
2012-08-10 13:46:18 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
|
2010-12-03 04:06:08 +00:00
|
|
|
}
|
2012-08-10 13:46:18 +00:00
|
|
|
|
2010-12-03 04:06:08 +00:00
|
|
|
include $file;
|
|
|
|
if (!isset($config)) {
|
2013-08-20 22:05:53 +00:00
|
|
|
throw new ConfigureException(__d('cake_dev', 'No variable %s found in %s', '$config', $file));
|
2010-12-03 04:06:08 +00:00
|
|
|
}
|
|
|
|
return $config;
|
|
|
|
}
|
2012-03-03 22:36:59 +00:00
|
|
|
|
2012-04-20 02:07:05 +00:00
|
|
|
/**
|
|
|
|
* Converts the provided $data into a string of PHP code that can
|
|
|
|
* be used saved into a file and loaded later.
|
|
|
|
*
|
2012-11-18 14:57:58 +00:00
|
|
|
* @param string $key The identifier to write to. If the key has a . it will be treated
|
|
|
|
* as a plugin prefix.
|
2012-04-20 02:07:05 +00:00
|
|
|
* @param array $data Data to dump.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int Bytes saved.
|
2012-04-20 02:07:05 +00:00
|
|
|
*/
|
2012-11-18 14:57:58 +00:00
|
|
|
public function dump($key, $data) {
|
2012-04-22 00:26:35 +00:00
|
|
|
$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
|
2012-08-15 14:08:58 +00:00
|
|
|
|
2012-11-18 14:57:58 +00:00
|
|
|
$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) {
|
2014-06-24 09:52:29 +00:00
|
|
|
$file = CakePlugin::path($plugin) . 'Config' . DS . $key;
|
2012-11-18 14:57:58 +00:00
|
|
|
} else {
|
|
|
|
$file = $this->_path . $key;
|
2012-08-15 14:08:58 +00:00
|
|
|
}
|
2012-11-18 14:57:58 +00:00
|
|
|
|
|
|
|
return $file;
|
2012-04-20 02:07:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 11:13:02 +00:00
|
|
|
}
|