PhpReader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * PhpReader file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.libs.config
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * PHP Reader allows Configure to load configuration values from
  21. * files containing simple PHP arrays.
  22. *
  23. * @package cake.libs.config
  24. */
  25. class PhpReader implements ConfigReaderInterface {
  26. /**
  27. * The path this reader finds files on.
  28. *
  29. * @var string
  30. */
  31. protected $_path = null;
  32. /**
  33. * Constructor for PHP Config file reading.
  34. *
  35. * @param string $path The path to read config files from. Defaults to APP . 'Config' . DS
  36. */
  37. public function __construct($path = null) {
  38. if (!$path) {
  39. $path = APP . 'Config' . DS;
  40. }
  41. $this->_path = $path;
  42. }
  43. /**
  44. * Read a config file and return its contents.
  45. *
  46. * Keys with `.` will be treated as values in plugins. Instead of reading from
  47. * the initialized path, plugin keys will be located using App::pluginPath().
  48. *
  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 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 ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
  59. }
  60. if (substr($key, -4) === '.php') {
  61. $key = substr($key, 0, -4);
  62. }
  63. list($plugin, $key) = pluginSplit($key);
  64. if ($plugin) {
  65. $file = App::pluginPath($plugin) . 'Config' . DS . $key;
  66. } else {
  67. $file = $this->_path . $key;
  68. }
  69. if (!file_exists($file)) {
  70. $file .= '.php';
  71. if (!file_exists($file)) {
  72. throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', substr($file, 0, -4), $file));
  73. }
  74. }
  75. include $file;
  76. if (!isset($config)) {
  77. throw new ConfigureException(
  78. sprintf(__d('cake_dev', 'No variable $config found in %s.php'), $file)
  79. );
  80. }
  81. return $config;
  82. }
  83. }