Source of file Json.php
Size: 2,889 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Config/Reader/Json.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Config\Reader; use Zend\Config\Exception; use Zend\Json\Exception as JsonException; use Zend\Json\Json as JsonFormat; /** * JSON config reader. */ class Json implements ReaderInterface { /** * Directory of the JSON file * * @var string */ protected $directory; /** * fromFile(): defined by Reader interface. * * @see ReaderInterface::fromFile() * @param string $filename * @return array * @throws Exception\RuntimeException */ public function fromFile($filename) { if (!is_file($filename) || !is_readable($filename)) { throw new Exception\RuntimeException(sprintf( "File '%s' doesn't exist or not readable", $filename )); } $this->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; } } |