PhpConfig.php 3.4 KB

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