JsonConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core\Configure\Engine;
  16. use Cake\Core\Configure\ConfigEngineInterface;
  17. use Cake\Core\Configure\FileConfigTrait;
  18. use Cake\Core\Exception\Exception;
  19. use Cake\Core\Plugin;
  20. /**
  21. * JSON engine allows Configure to load configuration values from
  22. * files containing JSON strings.
  23. */
  24. class JsonConfig implements ConfigEngineInterface
  25. {
  26. use FileConfigTrait;
  27. /**
  28. * File extension.
  29. *
  30. * @var string
  31. */
  32. protected $_extension = '.json';
  33. /**
  34. * Constructor for JSON Config file reading.
  35. *
  36. * @param string|null $path The path to read config files from. Defaults to CONFIG.
  37. */
  38. public function __construct($path = null)
  39. {
  40. if ($path === null) {
  41. $path = CONFIG;
  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
  49. * reading from the initialized path, plugin keys will be located using Plugin::path().
  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 \Cake\Core\Exception\Exception When files don't exist or when
  55. * files contain '..' (as this could lead to abusive reads) or when there
  56. * is an error parsing the JSON string.
  57. */
  58. public function read($key)
  59. {
  60. $file = $this->_getFilePath($key, true);
  61. $values = json_decode(file_get_contents($file), true);
  62. if (json_last_error() !== JSON_ERROR_NONE) {
  63. throw new Exception(sprintf(
  64. "Error parsing JSON string fetched from config file \"%s.json\": %s",
  65. $key,
  66. json_last_error_msg()
  67. ));
  68. }
  69. if (!is_array($values)) {
  70. throw new Exception(sprintf(
  71. 'Decoding JSON config file "%s.json" did not return any array',
  72. $key,
  73. json_last_error_msg()
  74. ));
  75. }
  76. return $values;
  77. }
  78. /**
  79. * Converts the provided $data into a JSON string that can be used saved
  80. * into a file and loaded later.
  81. *
  82. * @param string $key The identifier to write to. If the key has a . it will
  83. * be treated as a plugin prefix.
  84. * @param array $data Data to dump.
  85. * @return int Bytes saved.
  86. */
  87. public function dump($key, array $data)
  88. {
  89. $filename = $this->_getFilePath($key);
  90. return file_put_contents($filename, json_encode($data));
  91. }
  92. }