/lib/Cake/Configure/PhpReader.php

https://github.com/kunit/cakephp · PHP · 118 lines · 42 code · 12 blank · 64 comment · 7 complexity · 5f009e56edb754ce5ac33982dee5c266 MD5 · raw file

  1. <?php
  2. /**
  3. * PhpReader file
  4. *
  5. * PHP 5
  6. *
  7. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
  15. * @package Cake.Configure
  16. * @since CakePHP(tm) v 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  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. $file = $this->_getFilePath($key);
  63. if (!is_file($file)) {
  64. throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
  65. }
  66. include $file;
  67. if (!isset($config)) {
  68. throw new ConfigureException(__d('cake_dev', 'No variable $config found in %s', $file));
  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, $data) {
  82. $contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
  83. $filename = $this->_getFilePath($key);
  84. return file_put_contents($filename, $contents);
  85. }
  86. /**
  87. * Get file path
  88. *
  89. * @param string $key The identifier to write to. If the key has a . it will be treated
  90. * as a plugin prefix.
  91. * @return string Full file path
  92. */
  93. protected function _getFilePath($key) {
  94. if (substr($key, -4) === '.php') {
  95. $key = substr($key, 0, -4);
  96. }
  97. list($plugin, $key) = pluginSplit($key);
  98. $key .= '.php';
  99. if ($plugin) {
  100. $file = App::pluginPath($plugin) . 'Config' . DS . $key;
  101. } else {
  102. $file = $this->_path . $key;
  103. }
  104. return $file;
  105. }
  106. }