1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 2.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Core\Configure\Engine;
16:
17: use Cake\Core\Configure\ConfigEngineInterface;
18: use Cake\Core\Configure\FileConfigTrait;
19: use Cake\Core\Exception\Exception;
20:
21: /**
22: * PHP engine allows Configure to load configuration values from
23: * files containing simple PHP arrays.
24: *
25: * Files compatible with PhpConfig should return an array that
26: * contains all of the configuration data contained in the file.
27: *
28: * An example configuration file would look like::
29: *
30: * ```
31: * <?php
32: * return [
33: * 'debug' => false,
34: * 'Security' => [
35: * 'salt' => 'its-secret'
36: * ],
37: * 'App' => [
38: * 'namespace' => 'App'
39: * ]
40: * ];
41: * ```
42: *
43: * @see Cake\Core\Configure::load() for how to load custom configuration files.
44: */
45: class PhpConfig implements ConfigEngineInterface
46: {
47: use FileConfigTrait;
48:
49: /**
50: * File extension.
51: *
52: * @var string
53: */
54: protected $_extension = '.php';
55:
56: /**
57: * Constructor for PHP Config file reading.
58: *
59: * @param string|null $path The path to read config files from. Defaults to CONFIG.
60: */
61: public function __construct($path = null)
62: {
63: if ($path === null) {
64: $path = CONFIG;
65: }
66: $this->_path = $path;
67: }
68:
69: /**
70: * Read a config file and return its contents.
71: *
72: * Files with `.` in the name will be treated as values in plugins. Instead of
73: * reading from the initialized path, plugin keys will be located using Plugin::path().
74: *
75: * Setting a `$config` variable is deprecated. Use `return` instead.
76: *
77: * @param string $key The identifier to read from. If the key has a . it will be treated
78: * as a plugin prefix.
79: * @return array Parsed configuration values.
80: * @throws \Cake\Core\Exception\Exception when files don't exist or they don't contain `$config`.
81: * Or when files contain '..' as this could lead to abusive reads.
82: */
83: public function read($key)
84: {
85: $file = $this->_getFilePath($key, true);
86:
87: $config = null;
88:
89: $return = include $file;
90: if (is_array($return)) {
91: return $return;
92: }
93:
94: if ($config === null) {
95: throw new Exception(sprintf('Config file "%s" did not return an array', $key . '.php'));
96: }
97: deprecationWarning(sprintf(
98: 'PHP configuration files like "%s" should not set `$config`. Instead return an array.',
99: $key . '.php'
100: ));
101:
102: return $config;
103: }
104:
105: /**
106: * Converts the provided $data into a string of PHP code that can
107: * be used saved into a file and loaded later.
108: *
109: * @param string $key The identifier to write to. If the key has a . it will be treated
110: * as a plugin prefix.
111: * @param array $data Data to dump.
112: * @return bool Success
113: */
114: public function dump($key, array $data)
115: {
116: $contents = '<?php' . "\n" . 'return ' . var_export($data, true) . ';';
117:
118: $filename = $this->_getFilePath($key);
119:
120: return file_put_contents($filename, $contents) > 0;
121: }
122: }
123: