1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\DI;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: final class Helpers
24: {
25:
26: 27: 28: 29: 30: 31: 32: 33:
34: public static function expand($var, array $params, $recursive = FALSE)
35: {
36: if (is_array($var)) {
37: $res = array();
38: foreach ($var as $key => $val) {
39: $res[$key] = self::expand($val, $params, $recursive);
40: }
41: return $res;
42:
43: } elseif ($var instanceof Statement) {
44: return new Statement(self::expand($var->entity, $params, $recursive), self::expand($var->arguments, $params, $recursive));
45:
46: } elseif (!is_string($var)) {
47: return $var;
48: }
49:
50: $parts = preg_split('#%([\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE);
51: $res = '';
52: foreach ($parts as $n => $part) {
53: if ($n % 2 === 0) {
54: $res .= $part;
55:
56: } elseif ($part === '') {
57: $res .= '%';
58:
59: } elseif (isset($recursive[$part])) {
60: throw new Nette\InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($recursive)) . '.');
61:
62: } else {
63: $val = Nette\Utils\Arrays::get($params, explode('.', $part));
64: if ($recursive) {
65: $val = self::expand($val, $params, (is_array($recursive) ? $recursive : array()) + array($part => 1));
66: }
67: if (strlen($part) + 2 === strlen($var)) {
68: return $val;
69: }
70: if (!is_scalar($val)) {
71: throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '$part' into '$var'.");
72: }
73: $res .= $val;
74: }
75: }
76: return $res;
77: }
78:
79:
80:
81: 82: 83: 84: 85:
86: public static function escape($value)
87: {
88: if (is_array($value)) {
89: array_walk_recursive($value, function(&$val) {
90: $val = is_string($val) ? str_replace('%', '%%', $val) : $val;
91: });
92: } elseif (is_string($value)) {
93: $value = str_replace('%', '%%', $value);
94: }
95: return $value;
96: }
97:
98:
99:
100: 101: 102: 103: 104:
105: public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container)
106: {
107: $optCount = 0;
108: $num = -1;
109: $res = array();
110:
111: foreach ($method->getParameters() as $num => $parameter) {
112: if (array_key_exists($num, $arguments)) {
113: $res[$num] = $arguments[$num];
114: unset($arguments[$num]);
115: $optCount = 0;
116:
117: } elseif (array_key_exists($parameter->getName(), $arguments)) {
118: $res[$num] = $arguments[$parameter->getName()];
119: unset($arguments[$parameter->getName()]);
120: $optCount = 0;
121:
122: } elseif ($class = $parameter->getClassName()) {
123: $res[$num] = $container->getByType($class, FALSE);
124: if ($res[$num] === NULL) {
125: if ($parameter->allowsNull()) {
126: $optCount++;
127: } else {
128: throw new Nette\InvalidArgumentException("No service of type {$class} found");
129: }
130: } else {
131: if ($container instanceof ContainerBuilder) {
132: $res[$num] = '@' . $res[$num];
133: }
134: $optCount = 0;
135: }
136:
137: } elseif ($parameter->isOptional()) {
138:
139: $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
140: $optCount++;
141:
142: } else {
143: throw new Nette\InvalidArgumentException("$parameter is missing.");
144: }
145: }
146:
147:
148: while (array_key_exists(++$num, $arguments)) {
149: $res[$num] = $arguments[$num];
150: unset($arguments[$num]);
151: $optCount = 0;
152: }
153: if ($arguments) {
154: throw new Nette\InvalidArgumentException("Unable to pass specified arguments to $method.");
155: }
156:
157: return $optCount ? array_slice($res, 0, -$optCount) : $res;
158: }
159:
160: }
161: