~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Config/Adapters/NeonAdapter.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * This file is part of the Nette Framework (http://nette.org)
5
 
 *
6
 
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7
 
 *
8
 
 * For the full copyright and license information, please view
9
 
 * the file license.txt that was distributed with this source code.
10
 
 */
11
 
 
12
 
namespace Nette\Config\Adapters;
13
 
 
14
 
use Nette,
15
 
        Nette\Config\Helpers,
16
 
        Nette\Utils\Neon;
17
 
 
18
 
 
19
 
/**
20
 
 * Reading and generating NEON files.
21
 
 *
22
 
 * @author     David Grudl
23
 
 */
24
 
class NeonAdapter extends Nette\Object implements Nette\Config\IAdapter
25
 
{
26
 
        /** @internal */
27
 
        const INHERITING_SEPARATOR = '<', // child < parent
28
 
                PREVENT_MERGING = '!';
29
 
 
30
 
        /**
31
 
         * Reads configuration from NEON file.
32
 
         * @param  string  file name
33
 
         * @return array
34
 
         */
35
 
        public function load($file)
36
 
        {
37
 
                return $this->process((array) Neon::decode(file_get_contents($file)));
38
 
        }
39
 
 
40
 
 
41
 
        private function process(array $arr)
42
 
        {
43
 
                $res = array();
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.");
48
 
                                }
49
 
                                $key = substr($key, 0, -1);
50
 
                                $val[Helpers::EXTENDS_KEY] = Helpers::OVERWRITE;
51
 
 
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.");
55
 
                                }
56
 
                                list(, $key, $val[Helpers::EXTENDS_KEY]) = $matches;
57
 
                                if (isset($res[$key])) {
58
 
                                        throw new Nette\InvalidStateException("Duplicated key '$key'.");
59
 
                                }
60
 
                        }
61
 
 
62
 
                        if (is_array($val)) {
63
 
                                $val = $this->process($val);
64
 
                        } elseif ($val instanceof Nette\Utils\NeonEntity) {
65
 
                                $val = (object) array('value' => $val->value, 'attributes' => $this->process($val->attributes));
66
 
                        }
67
 
                        $res[$key] = $val;
68
 
                }
69
 
                return $res;
70
 
        }
71
 
 
72
 
 
73
 
        /**
74
 
         * Generates configuration in NEON format.
75
 
         * @return string
76
 
         */
77
 
        public function dump(array $data)
78
 
        {
79
 
                $tmp = array();
80
 
                foreach ($data as $name => $secData) {
81
 
                        if ($parent = Helpers::takeParent($secData)) {
82
 
                                $name .= ' ' . self::INHERITING_SEPARATOR . ' ' . $parent;
83
 
                        }
84
 
                        $tmp[$name] = $secData;
85
 
                }
86
 
                return "# generated by Nette\n\n" . Neon::encode($tmp, Neon::BLOCK);
87
 
        }
88
 
 
89
 
}