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