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