directory = dirname($filename); try { $config = JsonFormat::decode(file_get_contents($filename), JsonFormat::TYPE_ARRAY); } catch (JsonException\RuntimeException $e) { throw new Exception\RuntimeException($e->getMessage()); } return $this->process($config); } /** * fromString(): defined by Reader interface. * * @see ReaderInterface::fromString() * @param string $string * @return array|bool * @throws Exception\RuntimeException */ public function fromString($string) { if (empty($string)) { return array(); } $this->directory = null; try { $config = JsonFormat::decode($string, JsonFormat::TYPE_ARRAY); } catch (JsonException\RuntimeException $e) { throw new Exception\RuntimeException($e->getMessage()); } return $this->process($config); } /** * Process the array for @include * * @param array $data * @return array * @throws Exception\RuntimeException */ protected function process(array $data) { foreach ($data as $key => $value) { if (is_array($value)) { $data[$key] = $this->process($value); } if (trim($key) === '@include') { if ($this->directory === null) { throw new Exception\RuntimeException('Cannot process @include statement for a JSON string'); } $reader = clone $this; unset($data[$key]); $data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value)); } } return $data; } }