4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Config\Adapters;
20
* Reading and generating NEON files.
24
class NeonAdapter extends Nette\Object implements Nette\Config\IAdapter
27
const INHERITING_SEPARATOR = '<', // child < parent
28
PREVENT_MERGING = '!';
31
* Reads configuration from NEON file.
32
* @param string file name
35
public function load($file)
37
return $this->process((array) Neon::decode(file_get_contents($file)));
41
private function process(array $arr)
44
foreach ($arr as $key => $val) {
45
if (substr($key, -1) === self::PREVENT_MERGING) {
46
if (!is_array($val) && $val !== NULL) {
47
throw new Nette\InvalidStateException("Replacing operator is available only for arrays, item '$key' is not array.");
49
$key = substr($key, 0, -1);
50
$val[Helpers::EXTENDS_KEY] = Helpers::OVERWRITE;
52
} elseif (preg_match('#^(\S+)\s+' . self::INHERITING_SEPARATOR . '\s+(\S+)\z#', $key, $matches)) {
53
if (!is_array($val) && $val !== NULL) {
54
throw new Nette\InvalidStateException("Inheritance operator is available only for arrays, item '$key' is not array.");
56
list(, $key, $val[Helpers::EXTENDS_KEY]) = $matches;
57
if (isset($res[$key])) {
58
throw new Nette\InvalidStateException("Duplicated key '$key'.");
63
$val = $this->process($val);
64
} elseif ($val instanceof Nette\Utils\NeonEntity) {
65
$val = (object) array('value' => $val->value, 'attributes' => $this->process($val->attributes));
74
* Generates configuration in NEON format.
77
public function dump(array $data)
80
foreach ($data as $name => $secData) {
81
if ($parent = Helpers::takeParent($secData)) {
82
$name .= ' ' . self::INHERITING_SEPARATOR . ' ' . $parent;
84
$tmp[$name] = $secData;
86
return "# generated by Nette\n\n" . Neon::encode($tmp, Neon::BLOCK);