PhpReader.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Configure
  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. * Files compatible with PhpReader should define a `$config` variable, that
  24. * contains all of the configuration data contained in the file.
  25. *
  26. * @package Cake.Configure
  27. */
  28. class PhpReader implements ConfigReaderInterface {
  29. /**
  30. * The path this reader finds files on.
  31. *
  32. * @var string
  33. */
  34. protected $_path = null;
  35. /**
  36. * Constructor for PHP Config file reading.
  37. *
  38. * @param string $path The path to read config files from. Defaults to APP . 'Config' . DS
  39. */
  40. public function __construct($path = null) {
  41. if (!$path) {
  42. $path = APP . 'Config' . DS;
  43. }
  44. $this->_path = $path;
  45. }
  46. /**
  47. * Read a config file and return its contents.
  48. *
  49. * Files with `.` in the name will be treated as values in plugins. Instead of reading from
  50. * the initialized path, plugin keys will be located using App::pluginPath().
  51. *
  52. * @param string $key The identifier to read from. If the key has a . it will be treated
  53. * as a plugin prefix.
  54. * @return array Parsed configuration values.
  55. * @throws ConfigureException when files don't exist or they don't contain `$config`.
  56. * Or when files contain '..' as this could lead to abusive reads.
  57. */
  58. public function read($key) {
  59. if (strpos($key, '..') !== false) {
  60. throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
  61. }
  62. if (substr($key, -4) === '.php') {
  63. $key = substr($key, 0, -4);
  64. }
  65. list($plugin, $key) = pluginSplit($key);
  66. if ($plugin) {
  67. $file = App::pluginPath($plugin) . 'Config' . DS . $key;
  68. } else {
  69. $file = $this->_path . $key;
  70. }
  71. if (!file_exists($file)) {
  72. $file .= '.php';
  73. if (!file_exists($file)) {
  74. throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', substr($file, 0, -4), $file));
  75. }
  76. }
  77. include $file;
  78. if (!isset($config)) {
  79. throw new ConfigureException(
  80. sprintf(__d('cake_dev', 'No variable $config found in %s.php'), $file)
  81. );
  82. }
  83. return $config;
  84. }
  85. }