@@ -85,7 +85,8 @@ discard block |
||
| 85 | 85 | ): Response { |
| 86 | 86 | /** @var CookieQueue $cookieQueue */ |
| 87 | 87 | $cookieQueue = $request->getAttribute(CookieQueue::ATTRIBUTE); |
| 88 | - if ($cookieQueue === null) { |
|
| 88 | + if ($cookieQueue === null) |
|
| 89 | + { |
|
| 89 | 90 | return $response->withAddedHeader( |
| 90 | 91 | 'Set-Cookie', |
| 91 | 92 | Cookie::create( |
@@ -101,9 +102,12 @@ discard block |
||
| 101 | 102 | ); |
| 102 | 103 | } |
| 103 | 104 | |
| 104 | - if ($tokenID === null) { |
|
| 105 | + if ($tokenID === null) |
|
| 106 | + { |
|
| 105 | 107 | $cookieQueue->delete($this->cookie); |
| 106 | - } else { |
|
| 108 | + } |
|
| 109 | + else |
|
| 110 | + { |
|
| 107 | 111 | $cookieQueue->set( |
| 108 | 112 | $this->cookie, |
| 109 | 113 | $tokenID, |
@@ -134,7 +138,8 @@ discard block |
||
| 134 | 138 | */ |
| 135 | 139 | private function getLifetime(\DateTimeInterface $expiresAt = null): ?int |
| 136 | 140 | { |
| 137 | - if ($expiresAt === null) { |
|
| 141 | + if ($expiresAt === null) |
|
| 142 | + { |
|
| 138 | 143 | return null; |
| 139 | 144 | } |
| 140 | 145 | |
@@ -19,7 +19,8 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function __construct(array $visitors = []) |
| 21 | 21 | { |
| 22 | - foreach ($visitors as $visitor) { |
|
| 22 | + foreach ($visitors as $visitor) |
|
| 23 | + { |
|
| 23 | 24 | $this->addVisitor($visitor); |
| 24 | 25 | } |
| 25 | 26 | } |
@@ -34,8 +35,10 @@ discard block |
||
| 34 | 35 | |
| 35 | 36 | public function removeVisitor(VisitorInterface $visitor): void |
| 36 | 37 | { |
| 37 | - foreach ($this->visitors as $index => $added) { |
|
| 38 | - if ($added === $visitor) { |
|
| 38 | + foreach ($this->visitors as $index => $added) |
|
| 39 | + { |
|
| 40 | + if ($added === $visitor) |
|
| 41 | + { |
|
| 39 | 42 | unset($this->visitors[$index]); |
| 40 | 43 | break; |
| 41 | 44 | } |
@@ -56,22 +59,27 @@ discard block |
||
| 56 | 59 | $context ??= new VisitorContext(); |
| 57 | 60 | |
| 58 | 61 | $ctx = clone $context; |
| 59 | - foreach ($nodes as $index => $node) { |
|
| 60 | - if ($this->stopTraversal) { |
|
| 62 | + foreach ($nodes as $index => $node) |
|
| 63 | + { |
|
| 64 | + if ($this->stopTraversal) |
|
| 65 | + { |
|
| 61 | 66 | break; |
| 62 | 67 | } |
| 63 | 68 | |
| 64 | 69 | $traverseChildren = true; |
| 65 | 70 | $breakVisitorID = null; |
| 66 | 71 | |
| 67 | - if ($node instanceof NodeInterface) { |
|
| 72 | + if ($node instanceof NodeInterface) |
|
| 73 | + { |
|
| 68 | 74 | $ctx = $context->withNode($node); |
| 69 | 75 | } |
| 70 | 76 | |
| 71 | - foreach ($this->visitors as $visitorID => $visitor) { |
|
| 77 | + foreach ($this->visitors as $visitorID => $visitor) |
|
| 78 | + { |
|
| 72 | 79 | $result = $visitor->enterNode($node, $ctx); |
| 73 | 80 | |
| 74 | - switch (true) { |
|
| 81 | + switch (true) |
|
| 82 | + { |
|
| 75 | 83 | case $result === null: |
| 76 | 84 | break; |
| 77 | 85 | |
@@ -101,17 +109,21 @@ discard block |
||
| 101 | 109 | } |
| 102 | 110 | |
| 103 | 111 | // sub nodes |
| 104 | - if ($traverseChildren && $node instanceof NodeInterface) { |
|
| 112 | + if ($traverseChildren && $node instanceof NodeInterface) |
|
| 113 | + { |
|
| 105 | 114 | $nodes[$index] = $this->traverseNode($node, $ctx); |
| 106 | - if ($this->stopTraversal) { |
|
| 115 | + if ($this->stopTraversal) |
|
| 116 | + { |
|
| 107 | 117 | break; |
| 108 | 118 | } |
| 109 | 119 | } |
| 110 | 120 | |
| 111 | - foreach ($this->visitors as $visitorID => $visitor) { |
|
| 121 | + foreach ($this->visitors as $visitorID => $visitor) |
|
| 122 | + { |
|
| 112 | 123 | $result = $visitor->leaveNode($node, $ctx); |
| 113 | 124 | |
| 114 | - switch (true) { |
|
| 125 | + switch (true) |
|
| 126 | + { |
|
| 115 | 127 | case $result === null: |
| 116 | 128 | break; |
| 117 | 129 | |
@@ -133,7 +145,8 @@ discard block |
||
| 133 | 145 | ); |
| 134 | 146 | } |
| 135 | 147 | |
| 136 | - if ($breakVisitorID === $visitorID) { |
|
| 148 | + if ($breakVisitorID === $visitorID) |
|
| 149 | + { |
|
| 137 | 150 | break; |
| 138 | 151 | } |
| 139 | 152 | } |
@@ -148,18 +161,22 @@ discard block |
||
| 148 | 161 | private function traverseNode(NodeInterface $node, VisitorContext $context): NodeInterface |
| 149 | 162 | { |
| 150 | 163 | $ctx = clone $context; |
| 151 | - foreach ($node as $name => $_) { |
|
| 164 | + foreach ($node as $name => $_) |
|
| 165 | + { |
|
| 152 | 166 | $_child = &$node->$name; |
| 153 | - if (\is_array($_child)) { |
|
| 167 | + if (\is_array($_child)) |
|
| 168 | + { |
|
| 154 | 169 | $_child = $this->traverse($_child, $ctx); |
| 155 | - if ($this->stopTraversal) { |
|
| 170 | + if ($this->stopTraversal) |
|
| 171 | + { |
|
| 156 | 172 | break; |
| 157 | 173 | } |
| 158 | 174 | |
| 159 | 175 | continue; |
| 160 | 176 | } |
| 161 | 177 | |
| 162 | - if (!$_child instanceof NodeInterface) { |
|
| 178 | + if (!$_child instanceof NodeInterface) |
|
| 179 | + { |
|
| 163 | 180 | continue; |
| 164 | 181 | } |
| 165 | 182 | |
@@ -168,9 +185,11 @@ discard block |
||
| 168 | 185 | $traverseChildren = true; |
| 169 | 186 | $breakVisitorID = null; |
| 170 | 187 | |
| 171 | - foreach ($this->visitors as $visitorID => $visitor) { |
|
| 188 | + foreach ($this->visitors as $visitorID => $visitor) |
|
| 189 | + { |
|
| 172 | 190 | $result = $visitor->enterNode($_child, $ctx); |
| 173 | - switch (true) { |
|
| 191 | + switch (true) |
|
| 192 | + { |
|
| 174 | 193 | case $result === null: |
| 175 | 194 | break; |
| 176 | 195 | |
@@ -198,17 +217,21 @@ discard block |
||
| 198 | 217 | } |
| 199 | 218 | } |
| 200 | 219 | |
| 201 | - if ($traverseChildren) { |
|
| 220 | + if ($traverseChildren) |
|
| 221 | + { |
|
| 202 | 222 | $_child = $this->traverseNode($_child, $ctx); |
| 203 | - if ($this->stopTraversal) { |
|
| 223 | + if ($this->stopTraversal) |
|
| 224 | + { |
|
| 204 | 225 | break; |
| 205 | 226 | } |
| 206 | 227 | } |
| 207 | 228 | |
| 208 | - foreach ($this->visitors as $visitorID => $visitor) { |
|
| 229 | + foreach ($this->visitors as $visitorID => $visitor) |
|
| 230 | + { |
|
| 209 | 231 | $result = $visitor->leaveNode($_child, $ctx); |
| 210 | 232 | |
| 211 | - switch (true) { |
|
| 233 | + switch (true) |
|
| 234 | + { |
|
| 212 | 235 | case $result === null: |
| 213 | 236 | break; |
| 214 | 237 | |
@@ -226,7 +249,8 @@ discard block |
||
| 226 | 249 | ); |
| 227 | 250 | } |
| 228 | 251 | |
| 229 | - if ($breakVisitorID === $visitorID) { |
|
| 252 | + if ($breakVisitorID === $visitorID) |
|
| 253 | + { |
|
| 230 | 254 | break; |
| 231 | 255 | } |
| 232 | 256 | } |
@@ -25,15 +25,21 @@ |
||
| 25 | 25 | |
| 26 | 26 | public function patch(array $config): array |
| 27 | 27 | { |
| 28 | - try { |
|
| 28 | + try |
|
| 29 | + { |
|
| 29 | 30 | $_target = &$this->dotGet($config, $this->position); |
| 30 | 31 | |
| 31 | - if ($this->key !== null) { |
|
| 32 | + if ($this->key !== null) |
|
| 33 | + { |
|
| 32 | 34 | $_target = \array_merge([$this->key => $this->value], $_target); |
| 33 | - } else { |
|
| 35 | + } |
|
| 36 | + else |
|
| 37 | + { |
|
| 34 | 38 | \array_unshift($_target, $this->value); |
| 35 | 39 | } |
| 36 | - } catch (DotNotFoundException $e) { |
|
| 40 | + } |
|
| 41 | + catch (DotNotFoundException $e) |
|
| 42 | + { |
|
| 37 | 43 | throw new PatchException($e->getMessage(), $e->getCode(), $e); |
| 38 | 44 | } |
| 39 | 45 | |
@@ -24,10 +24,13 @@ |
||
| 24 | 24 | |
| 25 | 25 | public function patch(array $config): array |
| 26 | 26 | { |
| 27 | - try { |
|
| 27 | + try |
|
| 28 | + { |
|
| 28 | 29 | $_target = &$this->dotGet($config, $this->key); |
| 29 | 30 | $_target = $this->value; |
| 30 | - } catch (DotNotFoundException $e) { |
|
| 31 | + } |
|
| 32 | + catch (DotNotFoundException $e) |
|
| 33 | + { |
|
| 31 | 34 | throw new PatchException($e->getMessage(), $e->getCode(), $e); |
| 32 | 35 | } |
| 33 | 36 | |
@@ -37,40 +37,55 @@ discard block |
||
| 37 | 37 | $setters = []; |
| 38 | 38 | $class = new \ReflectionClass($filter); |
| 39 | 39 | |
| 40 | - foreach ($class->getProperties() as $property) { |
|
| 40 | + foreach ($class->getProperties() as $property) |
|
| 41 | + { |
|
| 41 | 42 | /** @var object $attribute */ |
| 42 | - foreach ($this->reader->getPropertyMetadata($property) as $attribute) { |
|
| 43 | - if ($attribute instanceof AbstractInput) { |
|
| 43 | + foreach ($this->reader->getPropertyMetadata($property) as $attribute) |
|
| 44 | + { |
|
| 45 | + if ($attribute instanceof AbstractInput) |
|
| 46 | + { |
|
| 44 | 47 | $this->setValue($filter, $property, $attribute->getValue($input, $property)); |
| 45 | 48 | $schema[$property->getName()] = $attribute->getSchema($property); |
| 46 | - } elseif ($attribute instanceof NestedFilter) { |
|
| 49 | + } |
|
| 50 | + elseif ($attribute instanceof NestedFilter) |
|
| 51 | + { |
|
| 47 | 52 | $prefix = $attribute->prefix ?? $property->name; |
| 48 | - try { |
|
| 53 | + try |
|
| 54 | + { |
|
| 49 | 55 | $value = $this->provider->createFilter( |
| 50 | 56 | $attribute->class, |
| 51 | 57 | $input->withPrefix($prefix) |
| 52 | 58 | ); |
| 53 | 59 | |
| 54 | 60 | $this->setValue($filter, $property, $value); |
| 55 | - } catch (ValidationException $e) { |
|
| 61 | + } |
|
| 62 | + catch (ValidationException $e) |
|
| 63 | + { |
|
| 56 | 64 | $errors[$prefix] = $e->errors; |
| 57 | 65 | } |
| 58 | 66 | |
| 59 | 67 | $schema[$property->getName()] = $attribute->getSchema($property); |
| 60 | - } elseif ($attribute instanceof NestedArray) { |
|
| 68 | + } |
|
| 69 | + elseif ($attribute instanceof NestedArray) |
|
| 70 | + { |
|
| 61 | 71 | $values = $attribute->getValue($input, $property); |
| 62 | 72 | $propertyValues = []; |
| 63 | 73 | |
| 64 | 74 | $prefix = $attribute->input->key ?? $attribute->prefix ?? $property->getName(); |
| 65 | 75 | |
| 66 | - if (\is_array($values)) { |
|
| 67 | - foreach (\array_keys($values) as $key) { |
|
| 68 | - try { |
|
| 76 | + if (\is_array($values)) |
|
| 77 | + { |
|
| 78 | + foreach (\array_keys($values) as $key) |
|
| 79 | + { |
|
| 80 | + try |
|
| 81 | + { |
|
| 69 | 82 | $propertyValues[$key] = $this->provider->createFilter( |
| 70 | 83 | $attribute->class, |
| 71 | 84 | $input->withPrefix($prefix . '.' . $key) |
| 72 | 85 | ); |
| 73 | - } catch (ValidationException $e) { |
|
| 86 | + } |
|
| 87 | + catch (ValidationException $e) |
|
| 88 | + { |
|
| 74 | 89 | $errors[$property->getName()][$key] = $e->errors; |
| 75 | 90 | } |
| 76 | 91 | } |
@@ -78,7 +93,9 @@ discard block |
||
| 78 | 93 | |
| 79 | 94 | $this->setValue($filter, $property, $propertyValues); |
| 80 | 95 | $schema[$property->getName()] = [$attribute->class, $prefix . '.*']; |
| 81 | - } elseif ($attribute instanceof Setter) { |
|
| 96 | + } |
|
| 97 | + elseif ($attribute instanceof Setter) |
|
| 98 | + { |
|
| 82 | 99 | $setters[$property->getName()][] = $attribute; |
| 83 | 100 | } |
| 84 | 101 | } |
@@ -89,13 +106,15 @@ discard block |
||
| 89 | 106 | |
| 90 | 107 | private function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void |
| 91 | 108 | { |
| 92 | - if ($value === null) { |
|
| 109 | + if ($value === null) |
|
| 110 | + { |
|
| 93 | 111 | return; |
| 94 | 112 | } |
| 95 | 113 | |
| 96 | 114 | $setters = $this->reader->getPropertyMetadata($property, Setter::class); |
| 97 | 115 | |
| 98 | - foreach ($setters as $setter) { |
|
| 116 | + foreach ($setters as $setter) |
|
| 117 | + { |
|
| 99 | 118 | $value = $setter->updateValue($value); |
| 100 | 119 | } |
| 101 | 120 | |
@@ -16,7 +16,8 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | public function __clone() |
| 18 | 18 | { |
| 19 | - foreach ($this->routes as $name => $route) { |
|
| 19 | + foreach ($this->routes as $name => $route) |
|
| 20 | + { |
|
| 20 | 21 | $this->routes[$name] = clone $route; |
| 21 | 22 | } |
| 22 | 23 | } |
@@ -92,7 +93,8 @@ discard block |
||
| 92 | 93 | */ |
| 93 | 94 | public function remove(string|array $name) |
| 94 | 95 | { |
| 95 | - foreach ((array) $name as $n) { |
|
| 96 | + foreach ((array) $name as $n) |
|
| 97 | + { |
|
| 96 | 98 | unset($this->routes[$n]); |
| 97 | 99 | } |
| 98 | 100 | } |
@@ -104,7 +106,8 @@ discard block |
||
| 104 | 106 | */ |
| 105 | 107 | public function addCollection(self $collection) |
| 106 | 108 | { |
| 107 | - foreach ($collection->all() as $name => $route) { |
|
| 109 | + foreach ($collection->all() as $name => $route) |
|
| 110 | + { |
|
| 108 | 111 | $this->routes[$name] = $route; |
| 109 | 112 | } |
| 110 | 113 | } |
@@ -120,7 +123,8 @@ discard block |
||
| 120 | 123 | */ |
| 121 | 124 | public function group(string $group) |
| 122 | 125 | { |
| 123 | - foreach ($this->routes as $route) { |
|
| 126 | + foreach ($this->routes as $route) |
|
| 127 | + { |
|
| 124 | 128 | $route->group($group); |
| 125 | 129 | } |
| 126 | 130 | } |
@@ -28,7 +28,8 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | public function getGroup(string $name): RouteGroup |
| 30 | 30 | { |
| 31 | - if (!isset($this->groups[$name])) { |
|
| 31 | + if (!isset($this->groups[$name])) |
|
| 32 | + { |
|
| 32 | 33 | $this->groups[$name] = $this->factory->make(RouteGroup::class); |
| 33 | 34 | } |
| 34 | 35 | |
@@ -60,7 +61,8 @@ discard block |
||
| 60 | 61 | */ |
| 61 | 62 | public function registerRoutes(RouterInterface $router): void |
| 62 | 63 | { |
| 63 | - foreach ($this->groups as $group) { |
|
| 64 | + foreach ($this->groups as $group) |
|
| 65 | + { |
|
| 64 | 66 | $group->register($router, $this->factory); |
| 65 | 67 | } |
| 66 | 68 | } |
@@ -25,7 +25,8 @@ discard block |
||
| 25 | 25 | $targets = \iterator_to_array($this->parseAttributes($listener)); |
| 26 | 26 | |
| 27 | 27 | // If there are no targets, then listener can't be cached. |
| 28 | - if ($targets === []) { |
|
| 28 | + if ($targets === []) |
|
| 29 | + { |
|
| 29 | 30 | return false; |
| 30 | 31 | } |
| 31 | 32 | |
@@ -34,11 +35,13 @@ discard block |
||
| 34 | 35 | // We decided to load classes for each target separately. |
| 35 | 36 | // It allows us to cache classes for each target separately and if we reuse the |
| 36 | 37 | // same target in multiple listeners, we will not have to load classes for the same target. |
| 37 | - foreach ($targets as $target) { |
|
| 38 | + foreach ($targets as $target) |
|
| 39 | + { |
|
| 38 | 40 | $cacheKey = (string)$target; |
| 39 | 41 | |
| 40 | 42 | $classes = $this->readCache ? $this->memory->loadData($cacheKey) : null; |
| 41 | - if ($classes === null) { |
|
| 43 | + if ($classes === null) |
|
| 44 | + { |
|
| 42 | 45 | $this->memory->saveData( |
| 43 | 46 | $cacheKey, |
| 44 | 47 | $classes = $this->locator->getClasses($target), |
@@ -64,13 +64,15 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 66 | 66 | { |
| 67 | - if (!isset($this->state->bindings[$alias])) { |
|
| 67 | + if (!isset($this->state->bindings[$alias])) |
|
| 68 | + { |
|
| 68 | 69 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 69 | 70 | } |
| 70 | 71 | |
| 71 | 72 | $avoidCache = $parameters !== []; |
| 72 | 73 | $binding = $this->state->bindings[$alias]; |
| 73 | - try { |
|
| 74 | + try |
|
| 75 | + { |
|
| 74 | 76 | $this->tracer->push( |
| 75 | 77 | false, |
| 76 | 78 | action: 'resolve from binding', |
@@ -81,8 +83,10 @@ discard block |
||
| 81 | 83 | ); |
| 82 | 84 | $this->tracer->push(true); |
| 83 | 85 | |
| 84 | - if (\is_object($binding)) { |
|
| 85 | - if ($binding::class === WeakReference::class) { |
|
| 86 | + if (\is_object($binding)) |
|
| 87 | + { |
|
| 88 | + if ($binding::class === WeakReference::class) |
|
| 89 | + { |
|
| 86 | 90 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
| 87 | 91 | } |
| 88 | 92 | |
@@ -96,7 +100,8 @@ discard block |
||
| 96 | 100 | } |
| 97 | 101 | |
| 98 | 102 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
| 99 | - if (\is_string($binding)) { |
|
| 103 | + if (\is_string($binding)) |
|
| 104 | + { |
|
| 100 | 105 | $ctx->class = $binding; |
| 101 | 106 | return $binding === $alias |
| 102 | 107 | ? $this->autowire($ctx, $parameters) |
@@ -104,18 +109,24 @@ discard block |
||
| 104 | 109 | : $this->make($binding, $parameters, $context); |
| 105 | 110 | } |
| 106 | 111 | |
| 107 | - if ($binding[1] === true) { |
|
| 112 | + if ($binding[1] === true) |
|
| 113 | + { |
|
| 108 | 114 | $ctx->singleton = true; |
| 109 | 115 | } |
| 110 | 116 | unset($this->state->bindings[$alias]); |
| 111 | - try { |
|
| 117 | + try |
|
| 118 | + { |
|
| 112 | 119 | return $binding[0] === $alias |
| 113 | 120 | ? $this->autowire($ctx, $parameters) |
| 114 | 121 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
| 115 | - } finally { |
|
| 122 | + } |
|
| 123 | + finally |
|
| 124 | + { |
|
| 116 | 125 | $this->state->bindings[$alias] ??= $binding; |
| 117 | 126 | } |
| 118 | - } finally { |
|
| 127 | + } |
|
| 128 | + finally |
|
| 129 | + { |
|
| 119 | 130 | $this->tracer->pop(true); |
| 120 | 131 | $this->tracer->pop(false); |
| 121 | 132 | } |
@@ -129,19 +140,24 @@ discard block |
||
| 129 | 140 | ): ?object { |
| 130 | 141 | $avoidCache = $parameters !== []; |
| 131 | 142 | |
| 132 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
| 133 | - try { |
|
| 143 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
| 144 | + { |
|
| 145 | + try |
|
| 146 | + { |
|
| 134 | 147 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 135 | 148 | |
| 136 | 149 | $object = $this->createInstance( |
| 137 | 150 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 138 | 151 | $parameters, |
| 139 | 152 | ); |
| 140 | - if ($avoidCache) { |
|
| 153 | + if ($avoidCache) |
|
| 154 | + { |
|
| 141 | 155 | return $object; |
| 142 | 156 | } |
| 143 | 157 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 144 | - } catch (\Throwable) { |
|
| 158 | + } |
|
| 159 | + catch (\Throwable) |
|
| 160 | + { |
|
| 145 | 161 | throw new ContainerException( |
| 146 | 162 | $this->tracer->combineTraceMessage( |
| 147 | 163 | \sprintf( |
@@ -151,7 +167,9 @@ discard block |
||
| 151 | 167 | ) |
| 152 | 168 | ) |
| 153 | 169 | ); |
| 154 | - } finally { |
|
| 170 | + } |
|
| 171 | + finally |
|
| 172 | + { |
|
| 155 | 173 | $this->tracer->pop(); |
| 156 | 174 | } |
| 157 | 175 | } |
@@ -163,18 +181,25 @@ discard block |
||
| 163 | 181 | { |
| 164 | 182 | $parent = $this->scope->getParent(); |
| 165 | 183 | |
| 166 | - if ($parent !== null) { |
|
| 167 | - try { |
|
| 184 | + if ($parent !== null) |
|
| 185 | + { |
|
| 186 | + try |
|
| 187 | + { |
|
| 168 | 188 | $this->tracer->push(false, ...[ |
| 169 | 189 | 'current scope' => $this->scope->getScopeName(), |
| 170 | 190 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 171 | 191 | ]); |
| 172 | 192 | return $parent->make($alias, $parameters, $context); |
| 173 | - } catch (BadScopeException $e) { |
|
| 174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 193 | + } |
|
| 194 | + catch (BadScopeException $e) |
|
| 195 | + { |
|
| 196 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 197 | + { |
|
| 175 | 198 | throw $e; |
| 176 | 199 | } |
| 177 | - } catch (ContainerExceptionInterface $e) { |
|
| 200 | + } |
|
| 201 | + catch (ContainerExceptionInterface $e) |
|
| 202 | + { |
|
| 178 | 203 | $className = match (true) { |
| 179 | 204 | $e instanceof NotFoundException => NotFoundException::class, |
| 180 | 205 | default => ContainerException::class, |
@@ -183,19 +208,24 @@ discard block |
||
| 183 | 208 | 'Can\'t resolve `%s`.', |
| 184 | 209 | $alias, |
| 185 | 210 | )), previous: $e); |
| 186 | - } finally { |
|
| 211 | + } |
|
| 212 | + finally |
|
| 213 | + { |
|
| 187 | 214 | $this->tracer->pop(false); |
| 188 | 215 | } |
| 189 | 216 | } |
| 190 | 217 | |
| 191 | 218 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 192 | - try { |
|
| 219 | + try |
|
| 220 | + { |
|
| 193 | 221 | //No direct instructions how to construct class, make is automatically |
| 194 | 222 | return $this->autowire( |
| 195 | 223 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 196 | 224 | $parameters, |
| 197 | 225 | ); |
| 198 | - } finally { |
|
| 226 | + } |
|
| 227 | + finally |
|
| 228 | + { |
|
| 199 | 229 | $this->tracer->pop(false); |
| 200 | 230 | } |
| 201 | 231 | } |
@@ -245,16 +275,25 @@ discard block |
||
| 245 | 275 | mixed $target, |
| 246 | 276 | array $arguments, |
| 247 | 277 | ): mixed { |
| 248 | - if (\is_string($target)) { |
|
| 278 | + if (\is_string($target)) |
|
| 279 | + { |
|
| 249 | 280 | // Reference |
| 250 | 281 | $instance = $this->make($target, $arguments, $ctx->parameter); |
| 251 | - } else { |
|
| 252 | - if ($target instanceof Autowire) { |
|
| 282 | + } |
|
| 283 | + else |
|
| 284 | + { |
|
| 285 | + if ($target instanceof Autowire) |
|
| 286 | + { |
|
| 253 | 287 | $instance = $target->resolve($this, $arguments); |
| 254 | - } else { |
|
| 255 | - try { |
|
| 288 | + } |
|
| 289 | + else |
|
| 290 | + { |
|
| 291 | + try |
|
| 292 | + { |
|
| 256 | 293 | $instance = $this->invoker->invoke($target, $arguments); |
| 257 | - } catch (NotCallableException $e) { |
|
| 294 | + } |
|
| 295 | + catch (NotCallableException $e) |
|
| 296 | + { |
|
| 258 | 297 | throw new ContainerException( |
| 259 | 298 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 260 | 299 | $e->getCode(), |
@@ -264,10 +303,12 @@ discard block |
||
| 264 | 303 | } |
| 265 | 304 | |
| 266 | 305 | // Check scope name |
| 267 | - if (\is_object($instance)) { |
|
| 306 | + if (\is_object($instance)) |
|
| 307 | + { |
|
| 268 | 308 | $ctx->reflection = new \ReflectionClass($instance); |
| 269 | 309 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 270 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 310 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
| 311 | + { |
|
| 271 | 312 | throw new BadScopeException($scopeName, $instance::class); |
| 272 | 313 | } |
| 273 | 314 | } |
@@ -296,26 +337,33 @@ discard block |
||
| 296 | 337 | array $parameters, |
| 297 | 338 | ): object { |
| 298 | 339 | $class = $ctx->class; |
| 299 | - try { |
|
| 340 | + try |
|
| 341 | + { |
|
| 300 | 342 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 301 | - } catch (\ReflectionException $e) { |
|
| 343 | + } |
|
| 344 | + catch (\ReflectionException $e) |
|
| 345 | + { |
|
| 302 | 346 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 303 | 347 | } |
| 304 | 348 | |
| 305 | 349 | // Check scope name |
| 306 | 350 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 307 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 351 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 352 | + { |
|
| 308 | 353 | throw new BadScopeException($scope, $class); |
| 309 | 354 | } |
| 310 | 355 | |
| 311 | 356 | //We have to construct class using external injector when we know exact context |
| 312 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 357 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 358 | + { |
|
| 313 | 359 | $injector = $this->state->injectors[$reflection->getName()]; |
| 314 | 360 | |
| 315 | - try { |
|
| 361 | + try |
|
| 362 | + { |
|
| 316 | 363 | $injectorInstance = $this->container->get($injector); |
| 317 | 364 | |
| 318 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 365 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 366 | + { |
|
| 319 | 367 | throw new InjectionException( |
| 320 | 368 | \sprintf( |
| 321 | 369 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -330,7 +378,8 @@ discard block |
||
| 330 | 378 | * @psalm-suppress RedundantCondition |
| 331 | 379 | */ |
| 332 | 380 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
| 333 | - if (!$reflection->isInstance($instance)) { |
|
| 381 | + if (!$reflection->isInstance($instance)) |
|
| 382 | + { |
|
| 334 | 383 | throw new InjectionException( |
| 335 | 384 | \sprintf( |
| 336 | 385 | "Invalid injection response for '%s'.", |
@@ -340,12 +389,15 @@ discard block |
||
| 340 | 389 | } |
| 341 | 390 | |
| 342 | 391 | return $instance; |
| 343 | - } finally { |
|
| 392 | + } |
|
| 393 | + finally |
|
| 394 | + { |
|
| 344 | 395 | $this->state->injectors[$reflection->getName()] = $injector; |
| 345 | 396 | } |
| 346 | 397 | } |
| 347 | 398 | |
| 348 | - if (!$reflection->isInstantiable()) { |
|
| 399 | + if (!$reflection->isInstantiable()) |
|
| 400 | + { |
|
| 349 | 401 | $itIs = match (true) { |
| 350 | 402 | $reflection->isEnum() => 'Enum', |
| 351 | 403 | $reflection->isAbstract() => 'Abstract class', |
@@ -358,12 +410,16 @@ discard block |
||
| 358 | 410 | |
| 359 | 411 | $constructor = $reflection->getConstructor(); |
| 360 | 412 | |
| 361 | - if ($constructor !== null) { |
|
| 362 | - try { |
|
| 413 | + if ($constructor !== null) |
|
| 414 | + { |
|
| 415 | + try |
|
| 416 | + { |
|
| 363 | 417 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 364 | 418 | $this->tracer->push(true); |
| 365 | 419 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 366 | - } catch (ValidationException $e) { |
|
| 420 | + } |
|
| 421 | + catch (ValidationException $e) |
|
| 422 | + { |
|
| 367 | 423 | throw new ContainerException( |
| 368 | 424 | $this->tracer->combineTraceMessage( |
| 369 | 425 | \sprintf( |
@@ -373,22 +429,31 @@ discard block |
||
| 373 | 429 | ) |
| 374 | 430 | ), |
| 375 | 431 | ); |
| 376 | - } finally { |
|
| 432 | + } |
|
| 433 | + finally |
|
| 434 | + { |
|
| 377 | 435 | $this->tracer->pop(true); |
| 378 | 436 | $this->tracer->pop(false); |
| 379 | 437 | } |
| 380 | - try { |
|
| 438 | + try |
|
| 439 | + { |
|
| 381 | 440 | // Using constructor with resolved arguments |
| 382 | 441 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 383 | 442 | $this->tracer->push(true); |
| 384 | 443 | $instance = new $class(...$arguments); |
| 385 | - } catch (\TypeError $e) { |
|
| 444 | + } |
|
| 445 | + catch (\TypeError $e) |
|
| 446 | + { |
|
| 386 | 447 | throw new WrongTypeException($constructor, $e); |
| 387 | - } finally { |
|
| 448 | + } |
|
| 449 | + finally |
|
| 450 | + { |
|
| 388 | 451 | $this->tracer->pop(true); |
| 389 | 452 | $this->tracer->pop(false); |
| 390 | 453 | } |
| 391 | - } else { |
|
| 454 | + } |
|
| 455 | + else |
|
| 456 | + { |
|
| 392 | 457 | // No constructor specified |
| 393 | 458 | $instance = $reflection->newInstance(); |
| 394 | 459 | } |
@@ -412,13 +477,15 @@ discard block |
||
| 412 | 477 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 413 | 478 | |
| 414 | 479 | //Declarative singletons |
| 415 | - if ($this->isSingleton($ctx)) { |
|
| 480 | + if ($this->isSingleton($ctx)) |
|
| 481 | + { |
|
| 416 | 482 | $this->state->bindings[$ctx->alias] = $instance; |
| 417 | 483 | } |
| 418 | 484 | |
| 419 | 485 | // Register finalizer |
| 420 | 486 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 421 | - if ($finalizer !== null) { |
|
| 487 | + if ($finalizer !== null) |
|
| 488 | + { |
|
| 422 | 489 | $this->state->finalizers[] = $finalizer; |
| 423 | 490 | } |
| 424 | 491 | |
@@ -430,11 +497,13 @@ discard block |
||
| 430 | 497 | */ |
| 431 | 498 | private function isSingleton(Ctx $ctx): bool |
| 432 | 499 | { |
| 433 | - if ($ctx->singleton === true) { |
|
| 500 | + if ($ctx->singleton === true) |
|
| 501 | + { |
|
| 434 | 502 | return true; |
| 435 | 503 | } |
| 436 | 504 | |
| 437 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 505 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 506 | + { |
|
| 438 | 507 | return true; |
| 439 | 508 | } |
| 440 | 509 | |
@@ -448,7 +517,8 @@ discard block |
||
| 448 | 517 | * @var Finalize|null $attribute |
| 449 | 518 | */ |
| 450 | 519 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 451 | - if ($attribute === null) { |
|
| 520 | + if ($attribute === null) |
|
| 521 | + { |
|
| 452 | 522 | return null; |
| 453 | 523 | } |
| 454 | 524 | |