Json.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config\Reader;
  10. use Zend\Config\Exception;
  11. use Zend\Json\Exception as JsonException;
  12. use Zend\Json\Json as JsonFormat;
  13. /**
  14. * JSON config reader.
  15. */
  16. class Json implements ReaderInterface
  17. {
  18. /**
  19. * Directory of the JSON file
  20. *
  21. * @var string
  22. */
  23. protected $directory;
  24. /**
  25. * fromFile(): defined by Reader interface.
  26. *
  27. * @see ReaderInterface::fromFile()
  28. * @param string $filename
  29. * @return array
  30. * @throws Exception\RuntimeException
  31. */
  32. public function fromFile($filename)
  33. {
  34. if (!is_file($filename) || !is_readable($filename)) {
  35. throw new Exception\RuntimeException(sprintf(
  36. "File '%s' doesn't exist or not readable",
  37. $filename
  38. ));
  39. }
  40. $this->directory = dirname($filename);
  41. try {
  42. $config = JsonFormat::decode(file_get_contents($filename), JsonFormat::TYPE_ARRAY);
  43. } catch (JsonException\RuntimeException $e) {
  44. throw new Exception\RuntimeException($e->getMessage());
  45. }
  46. return $this->process($config);
  47. }
  48. /**
  49. * fromString(): defined by Reader interface.
  50. *
  51. * @see ReaderInterface::fromString()
  52. * @param string $string
  53. * @return array|bool
  54. * @throws Exception\RuntimeException
  55. */
  56. public function fromString($string)
  57. {
  58. if (empty($string)) {
  59. return array();
  60. }
  61. $this->directory = null;
  62. try {
  63. $config = JsonFormat::decode($string, JsonFormat::TYPE_ARRAY);
  64. } catch (JsonException\RuntimeException $e) {
  65. throw new Exception\RuntimeException($e->getMessage());
  66. }
  67. return $this->process($config);
  68. }
  69. /**
  70. * Process the array for @include
  71. *
  72. * @param array $data
  73. * @return array
  74. * @throws Exception\RuntimeException
  75. */
  76. protected function process(array $data)
  77. {
  78. foreach ($data as $key => $value) {
  79. if (is_array($value)) {
  80. $data[$key] = $this->process($value);
  81. }
  82. if (trim($key) === '@include') {
  83. if ($this->directory === null) {
  84. throw new Exception\RuntimeException('Cannot process @include statement for a JSON string');
  85. }
  86. $reader = clone $this;
  87. unset($data[$key]);
  88. $data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value));
  89. }
  90. }
  91. return $data;
  92. }
  93. }