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