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