PhpReader.php 3.3 KB

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