Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * PhpReader file
4
 *
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
13
 * @package       Cake.Configure
14
 * @since         CakePHP(tm) v 2.0
15
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
16
 */
17
 
18
/**
19
 * PHP Reader allows Configure to load configuration values from
20
 * files containing simple PHP arrays.
21
 *
22
 * Files compatible with PhpReader should define a `$config` variable, that
23
 * contains all of the configuration data contained in the file.
24
 *
25
 * @package       Cake.Configure
26
 */
27
class PhpReader implements ConfigReaderInterface {
28
 
29
/**
30
 * The path this reader finds files on.
31
 *
32
 * @var string
33
 */
34
	protected $_path = null;
35
 
36
/**
37
 * Constructor for PHP Config file reading.
38
 *
39
 * @param string $path The path to read config files from. Defaults to APP . 'Config' . DS
40
 */
41
	public function __construct($path = null) {
42
		if (!$path) {
43
			$path = APP . 'Config' . DS;
44
		}
45
		$this->_path = $path;
46
	}
47
 
48
/**
49
 * Read a config file and return its contents.
50
 *
51
 * Files with `.` in the name will be treated as values in plugins. Instead of reading from
52
 * the initialized path, plugin keys will be located using App::pluginPath().
53
 *
54
 * @param string $key The identifier to read from. If the key has a . it will be treated
55
 *  as a plugin prefix.
56
 * @return array Parsed configuration values.
57
 * @throws ConfigureException when files don't exist or they don't contain `$config`.
58
 *  Or when files contain '..' as this could lead to abusive reads.
59
 */
60
	public function read($key) {
61
		if (strpos($key, '..') !== false) {
62
			throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
63
		}
64
 
65
		$file = $this->_getFilePath($key);
66
		if (!is_file($file)) {
67
			throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
68
		}
69
 
70
		include $file;
71
		if (!isset($config)) {
72
			throw new ConfigureException(__d('cake_dev', 'No variable %s found in %s', '$config', $file));
73
		}
74
		return $config;
75
	}
76
 
77
/**
78
 * Converts the provided $data into a string of PHP code that can
79
 * be used saved into a file and loaded later.
80
 *
81
 * @param string $key The identifier to write to. If the key has a . it will be treated
82
 *  as a plugin prefix.
83
 * @param array $data Data to dump.
84
 * @return integer Bytes saved.
85
 */
86
	public function dump($key, $data) {
87
		$contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
88
 
89
		$filename = $this->_getFilePath($key);
90
		return file_put_contents($filename, $contents);
91
	}
92
 
93
/**
94
 * Get file path
95
 *
96
 * @param string $key The identifier to write to. If the key has a . it will be treated
97
 *  as a plugin prefix.
98
 * @return string Full file path
99
 */
100
	protected function _getFilePath($key) {
101
		if (substr($key, -4) === '.php') {
102
			$key = substr($key, 0, -4);
103
		}
104
		list($plugin, $key) = pluginSplit($key);
105
		$key .= '.php';
106
 
107
		if ($plugin) {
108
			$file = App::pluginPath($plugin) . 'Config' . DS . $key;
109
		} else {
110
			$file = $this->_path . $key;
111
		}
112
 
113
		return $file;
114
	}
115
 
116
}