Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/config' of https://github.com/ezimuel/zf2 into …
Browse files Browse the repository at this point in the history
…feature/config-json-yaml
  • Loading branch information
Show file tree
Hide file tree
Showing 20 changed files with 852 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Reader/Ini.php
Expand Up @@ -141,7 +141,7 @@ protected function process(array $data)
if (is_array($value)) {
$config[$section] = $this->processSection($value);
} else {
$config[$section] = $value;
$this->processKey($section, $value, $config);
}
}

Expand Down
113 changes: 113 additions & 0 deletions src/Reader/Json.php
@@ -0,0 +1,113 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @subpackage Reader
* @copyright Copyright (c) 2005-2012 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\Reader,
Zend\Config\Exception,
Zend\Json\Json as JsonFormat;

/**
* Json config reader.
*
* @category Zend
* @package Zend_Config
* @subpackage Reader
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Json implements Reader
{
/**
* Directory of the JSON file
*
* @var string
*/
protected $directory;
/**
* fromFile(): defined by Reader interface.
*
* @see Reader::fromFile()
* @param string $filename
* @return array
*/
public function fromFile($filename)
{
if (!file_exists($filename)) {
throw new Exception\RuntimeException("The file $filename doesn't exists.");
}

$this->directory = dirname($filename);

try {
$config = JsonFormat::decode(file_get_contents($filename), JsonFormat::TYPE_ARRAY);
} catch (\Zend\Json\Exception\RuntimeException $e) {
throw new Exception\RuntimeException($e->getMessage());
}

return $this->process($config);
}

/**
* fromString(): defined by Reader interface.
*
* @see Reader::fromString()
* @param string $string
* @return array
*/
public function fromString($string)
{
if (empty($string)) {
return array();
}
$this->directory = null;

try {
$config = JsonFormat::decode($string, JsonFormat::TYPE_ARRAY);
} catch (\Zend\Json\Exception\RuntimeException $e) {
throw new Exception\RuntimeException($e->getMessage());
}

return $this->process($config);
}
/**
* Process the array for @include
*
* @param array $data
* @return array
*/
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;
}
}
160 changes: 160 additions & 0 deletions src/Reader/Yaml.php
@@ -0,0 +1,160 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @subpackage Reader
* @copyright Copyright (c) 2005-2012 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\Reader,
Zend\Config\Exception;

/**
* Yaml config reader.
*
* @category Zend
* @package Zend_Config
* @subpackage Reader
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Yaml implements Reader
{
/**
* Directory of the JSON file
*
* @var string
*/
protected $directory;
/**
* Yaml decoder callback
*
* @var callable
*/
protected $yamlDecoder;
/**
* Constructor
*
* @param callable $yamlDecoder
*/
public function __construct($yamlDecoder=null) {
if (!empty($yamlDecoder)) {
$this->setYamlDecoder($yamlDecoder);
} else {
if (function_exists('yaml_parse')) {
$this->setYamlDecoder('yaml_parse');
}
}
}
/**
* Get callback for decoding YAML
*
* @return callable
*/
public function getYamlDecoder()
{
return $this->yamlDecoder;
}
/**
* Set callback for decoding YAML
*
* @param callable $yamlDecoder the decoder to set
* @return Yaml
*/
public function setYamlDecoder($yamlDecoder)
{
if (!is_callable($yamlDecoder)) {
throw new Exception\InvalidArgumentException('Invalid parameter to setYamlDecoder() - must be callable');
}
$this->yamlDecoder = $yamlDecoder;
return $this;
}
/**
* fromFile(): defined by Reader interface.
*
* @see Reader::fromFile()
* @param string $filename
* @return array
*/
public function fromFile($filename)
{
if (!file_exists($filename)) {
throw new Exception\RuntimeException("The file $filename doesn't exists.");
}
if (null === $this->getYamlDecoder()) {
throw new Exception\RuntimeException("You didn't specify a Yaml callback decoder");
}

$this->directory = dirname($filename);

$config = call_user_func($this->getYamlDecoder(), file_get_contents($filename));
if (null === $config) {
throw new Exception\RuntimeException("Error parsing YAML data");
}

return $this->process($config);
}

/**
* fromString(): defined by Reader interface.
*
* @see Reader::fromString()
* @param string $string
* @return array
*/
public function fromString($string)
{
if (null === $this->getYamlDecoder()) {
throw new Exception\RuntimeException("You didn't specify a Yaml callback decoder");
}
if (empty($string)) {
return array();
}

$this->directory = null;

$config = call_user_func($this->getYamlDecoder(), $string);
if (null === $config) {
throw new Exception\RuntimeException("Error parsing YAML data");
}

return $this->process($config);
}
/**
* Process the array for @include
*
* @param array $data
* @return array
*/
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;
}
}
43 changes: 43 additions & 0 deletions src/Writer/Json.php
@@ -0,0 +1,43 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Config\Writer;

use Zend\Config\Exception,
Zend\Json\Json as JsonFormat;
/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Json extends AbstractWriter
{
/**
* processConfig(): defined by AbstractWriter.
*
* @param array $config
* @return string
*/
public function processConfig(array $config)
{
return JsonFormat::encode($config);
}
}

0 comments on commit 1c0577b

Please sign in to comment.