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: * @package Nette\Config
11: */
12:
13:
14:
15: /**
16: * Configuration helpers.
17: *
18: * @author David Grudl
19: * @package Nette\Config
20: */
21: class NConfigHelpers
22: {
23: const EXTENDS_KEY = '_extends',
24: OVERWRITE = TRUE;
25:
26:
27: /**
28: * Merges configurations. Left has higher priority than right one.
29: * @return array
30: */
31: public static function merge($left, $right)
32: {
33: if (is_array($left) && is_array($right)) {
34: foreach ($left as $key => $val) {
35: if (is_int($key)) {
36: $right[] = $val;
37: } else {
38: if (is_array($val) && isset($val[self::EXTENDS_KEY])) {
39: if ($val[self::EXTENDS_KEY] === self::OVERWRITE) {
40: unset($val[self::EXTENDS_KEY]);
41: }
42: } elseif (isset($right[$key])) {
43: $val = self::merge($val, $right[$key]);
44: }
45: $right[$key] = $val;
46: }
47: }
48: return $right;
49:
50: } elseif ($left === NULL && is_array($right)) {
51: return $right;
52:
53: } else {
54: return $left;
55: }
56: }
57:
58:
59: /**
60: * Finds out and removes information about the parent.
61: * @return mixed
62: */
63: public static function takeParent(& $data)
64: {
65: if (is_array($data) && isset($data[self::EXTENDS_KEY])) {
66: $parent = $data[self::EXTENDS_KEY];
67: unset($data[self::EXTENDS_KEY]);
68: return $parent;
69: }
70: }
71:
72:
73: /**
74: * @return bool
75: */
76: public static function isOverwriting(& $data)
77: {
78: return is_array($data) && isset($data[self::EXTENDS_KEY]) && $data[self::EXTENDS_KEY] === self::OVERWRITE;
79: }
80:
81:
82: /**
83: * @return bool
84: */
85: public static function isInheriting(& $data)
86: {
87: return is_array($data) && isset($data[self::EXTENDS_KEY]) && $data[self::EXTENDS_KEY] !== self::OVERWRITE;
88: }
89:
90: }
91: