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: public static function escape($value)
84: {
85: if (is_array($value)) {
86: array_walk_recursive($value, create_function('&$val', '
87: $val = is_string($val) ? str_replace(\'%\', \'%%\', $val) : $val;
88: '));
89: } elseif (is_string($value)) {
90: $value = str_replace('%', '%%', $value);
91: }
92: return $value;
93: }
94:
95:
96: 97: 98: 99: 100:
101: public static function autowireArguments(ReflectionFunctionAbstract $method, array $arguments, $container)
102: {
103: $optCount = 0;
104: $num = -1;
105: $res = array();
106:
107: foreach ($method->getParameters() as $num => $parameter) {
108: if (array_key_exists($num, $arguments)) {
109: $res[$num] = $arguments[$num];
110: unset($arguments[$num]);
111: $optCount = 0;
112:
113: } elseif (array_key_exists($parameter->getName(), $arguments)) {
114: $res[$num] = $arguments[$parameter->getName()];
115: unset($arguments[$parameter->getName()]);
116: $optCount = 0;
117:
118: } elseif ($class = $parameter->getClassName()) {
119: $res[$num] = $container->getByType($class, FALSE);
120: if ($res[$num] === NULL) {
121: if ($parameter->allowsNull()) {
122: $optCount++;
123: } else {
124: throw new ServiceCreationException("No service of type {$class} found. Make sure the type hint in $method is written correctly and service of this type is registered.");
125: }
126: } else {
127: if ($container instanceof DIContainerBuilder) {
128: $res[$num] = '@' . $res[$num];
129: }
130: $optCount = 0;
131: }
132:
133: } elseif ($parameter->isOptional()) {
134:
135: $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
136: $optCount++;
137:
138: } else {
139: throw new ServiceCreationException("$parameter has no type hint, so its value must be specified.");
140: }
141: }
142:
143:
144: while (array_key_exists(++$num, $arguments)) {
145: $res[$num] = $arguments[$num];
146: unset($arguments[$num]);
147: $optCount = 0;
148: }
149: if ($arguments) {
150: throw new ServiceCreationException("Unable to pass specified arguments to $method.");
151: }
152:
153: return $optCount ? array_slice($res, 0, -$optCount) : $res;
154: }
155:
156: }
157: