/lib/Cake/Configure/PhpReader.php

https://bitbucket.org/00firestar00/ejfirestar.com · PHP · 116 lines · 42 code · 12 blank · 62 comment · 7 complexity · f509b4633439c86abe596ef0830cca0c MD5 · raw file

  1. <?php
  2. /**
  3. * PhpReader file
  4. *
  5. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
  13. * @package Cake.Configure
  14. * @since CakePHP(tm) v 2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. /**
  18. * PHP Reader allows Configure to load configuration values from
  19. * files containing simple PHP arrays.
  20. *
  21. * Files compatible with PhpReader should define a `$config` variable, that
  22. * contains all of the configuration data contained in the file.
  23. *
  24. * @package Cake.Configure
  25. */
  26. class PhpReader implements ConfigReaderInterface {
  27. /**
  28. * The path this reader finds files on.
  29. *
  30. * @var string
  31. */
  32. protected $_path = null;
  33. /**
  34. * Constructor for PHP Config file reading.
  35. *
  36. * @param string $path The path to read config files from. Defaults to APP . 'Config' . DS
  37. */
  38. public function __construct($path = null) {
  39. if (!$path) {
  40. $path = APP . 'Config' . DS;
  41. }
  42. $this->_path = $path;
  43. }
  44. /**
  45. * Read a config file and return its contents.
  46. *
  47. * Files with `.` in the name will be treated as values in plugins. Instead of reading from
  48. * the initialized path, plugin keys will be located using App::pluginPath().
  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. $file = $this->_getFilePath($key);
  61. if (!is_file($file)) {
  62. throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
  63. }
  64. include $file;
  65. if (!isset($config)) {
  66. throw new ConfigureException(__d('cake_dev', 'No variable %s found in %s', '$config', $file));
  67. }
  68. return $config;
  69. }
  70. /**
  71. * Converts the provided $data into a string of PHP code that can
  72. * be used saved into a file and loaded later.
  73. *
  74. * @param string $key The identifier to write to. If the key has a . it will be treated
  75. * as a plugin prefix.
  76. * @param array $data Data to dump.
  77. * @return integer Bytes saved.
  78. */
  79. public function dump($key, $data) {
  80. $contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';
  81. $filename = $this->_getFilePath($key);
  82. return file_put_contents($filename, $contents);
  83. }
  84. /**
  85. * Get file path
  86. *
  87. * @param string $key The identifier to write to. If the key has a . it will be treated
  88. * as a plugin prefix.
  89. * @return string Full file path
  90. */
  91. protected function _getFilePath($key) {
  92. if (substr($key, -4) === '.php') {
  93. $key = substr($key, 0, -4);
  94. }
  95. list($plugin, $key) = pluginSplit($key);
  96. $key .= '.php';
  97. if ($plugin) {
  98. $file = App::pluginPath($plugin) . 'Config' . DS . $key;
  99. } else {
  100. $file = $this->_path . $key;
  101. }
  102. return $file;
  103. }
  104. }