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:
70: if ($plugin) {
71: $file = App::pluginPath($plugin) . 'Config' . DS . $key;
72: } else {
73: $file = $this->_path . $key;
74: }
75: $file .= '.php';
76: if (!is_file($file)) {
77: if (!is_file(substr($file, 0, -4))) {
78: throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', $file, substr($file, 0, -4)));
79: }
80: }
81: include $file;
82: if (!isset($config)) {
83: throw new ConfigureException(
84: sprintf(__d('cake_dev', 'No variable $config found in %s.php'), $file)
85: );
86: }
87: return $config;
88: }
89:
90: }
91: