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 ConfigHelpers
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: /**
61: * Finds out and removes information about the parent.
62: * @return mixed
63: */
64: public static function takeParent(& $data)
65: {
66: if (is_array($data) && isset($data[self::EXTENDS_KEY])) {
67: $parent = $data[self::EXTENDS_KEY];
68: unset($data[self::EXTENDS_KEY]);
69: return $parent;
70: }
71: }
72:
73:
74:
75: /**
76: * @return bool
77: */
78: public static function isOverwriting(& $data)
79: {
80: return is_array($data) && isset($data[self::EXTENDS_KEY]) && $data[self::EXTENDS_KEY] === self::OVERWRITE;
81: }
82:
83:
84:
85: /**
86: * @return bool
87: */
88: public static function isInheriting(& $data)
89: {
90: return is_array($data) && isset($data[self::EXTENDS_KEY]) && $data[self::EXTENDS_KEY] !== self::OVERWRITE;
91: }
92:
93: }
94: