PhpConfig.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core\Configure\Engine;
  16. use Cake\Core\Configure\ConfigEngineInterface;
  17. use Cake\Core\Configure\FileConfigTrait;
  18. use Cake\Core\Exception\Exception;
  19. use Cake\Core\Plugin;
  20. /**
  21. * PHP engine allows Configure to load configuration values from
  22. * files containing simple PHP arrays.
  23. *
  24. * Files compatible with PhpConfig should define a `$config` variable, that
  25. * contains all of the configuration data contained in the file.
  26. */
  27. class PhpConfig implements ConfigEngineInterface
  28. {
  29. use FileConfigTrait;
  30. /**
  31. * File extension.
  32. *
  33. * @var string
  34. */
  35. protected $_extension = '.php';
  36. /**
  37. * Constructor for PHP Config file reading.
  38. *
  39. * @param string|null $path The path to read config files from. Defaults to CONFIG.
  40. */
  41. public function __construct($path = null)
  42. {
  43. if ($path === null) {
  44. $path = CONFIG;
  45. }
  46. $this->_path = $path;
  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
  52. * reading from the initialized path, plugin keys will be located using Plugin::path().
  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 \Cake\Core\Exception\Exception 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. {
  62. $file = $this->_getFilePath($key, true);
  63. $return = include $file;
  64. if (is_array($return)) {
  65. return $return;
  66. }
  67. if (!isset($config)) {
  68. throw new Exception(sprintf('Config file "%s" did not return an array', $key . '.php'));
  69. }
  70. return $config;
  71. }
  72. /**
  73. * Converts the provided $data into a string of PHP code that can
  74. * be used saved into a file and loaded later.
  75. *
  76. * @param string $key The identifier to write to. If the key has a . it will be treated
  77. * as a plugin prefix.
  78. * @param array $data Data to dump.
  79. * @return int Bytes saved.
  80. */
  81. public function dump($key, array $data)
  82. {
  83. $contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
  84. $filename = $this->_getFilePath($key);
  85. return file_put_contents($filename, $contents);
  86. }
  87. }