@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | { |
95 | 95 | $arguments = []; |
96 | 96 | |
97 | - foreach ($reflection->getParameters() as $parameter) { |
|
97 | + foreach ($reflection->getParameters() as $parameter){ |
|
98 | 98 | $type = $parameter->getType(); |
99 | 99 | $name = $parameter->getName(); |
100 | 100 | $class = null; |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * Container do not currently support union types. In the future, we |
104 | 104 | * can provide the possibility of autowiring based on priorities (TBD). |
105 | 105 | */ |
106 | - if ($type instanceof \ReflectionUnionType) { |
|
106 | + if ($type instanceof \ReflectionUnionType){ |
|
107 | 107 | $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously'; |
108 | 108 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
109 | 109 | |
@@ -113,17 +113,17 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Container do not currently support intersection types. |
115 | 115 | */ |
116 | - if ($type instanceof \ReflectionIntersectionType) { |
|
116 | + if ($type instanceof \ReflectionIntersectionType){ |
|
117 | 117 | $error = 'Parameter $%s in %s contains a intersection type hint that cannot be inferred unambiguously'; |
118 | 118 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
119 | 119 | |
120 | 120 | throw new ContainerException($error); |
121 | 121 | } |
122 | 122 | |
123 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { |
|
124 | - try { |
|
123 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()){ |
|
124 | + try{ |
|
125 | 125 | $class = new \ReflectionClass($type->getName()); |
126 | - } catch (\ReflectionException $e) { |
|
126 | + }catch (\ReflectionException $e){ |
|
127 | 127 | $location = $this->getLocationString($reflection); |
128 | 128 | |
129 | 129 | $error = 'Unable to resolve `\$%s` parameter in %s: %s'; |
@@ -133,11 +133,11 @@ discard block |
||
133 | 133 | } |
134 | 134 | } |
135 | 135 | |
136 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
137 | - if ($parameters[$name] instanceof Autowire) { |
|
136 | + if (isset($parameters[$name]) && is_object($parameters[$name])){ |
|
137 | + if ($parameters[$name] instanceof Autowire){ |
|
138 | 138 | // Supplied by user as late dependency |
139 | 139 | $arguments[] = $parameters[$name]->resolve($this); |
140 | - } else { |
|
140 | + }else{ |
|
141 | 141 | // Supplied by user as object |
142 | 142 | $arguments[] = $parameters[$name]; |
143 | 143 | } |
@@ -145,16 +145,16 @@ discard block |
||
145 | 145 | } |
146 | 146 | |
147 | 147 | // no declared type or scalar type or array |
148 | - if (!isset($class)) { |
|
148 | + if (!isset($class)){ |
|
149 | 149 | // Provided from outside |
150 | - if (\array_key_exists($name, $parameters)) { |
|
150 | + if (\array_key_exists($name, $parameters)){ |
|
151 | 151 | // Make sure it's properly typed |
152 | 152 | $this->assertType($parameter, $reflection, $parameters[$name]); |
153 | 153 | $arguments[] = $parameters[$name]; |
154 | 154 | continue; |
155 | 155 | } |
156 | 156 | |
157 | - if ($parameter->isDefaultValueAvailable()) { |
|
157 | + if ($parameter->isDefaultValueAvailable()){ |
|
158 | 158 | //Default value |
159 | 159 | $arguments[] = $parameter->getDefaultValue(); |
160 | 160 | continue; |
@@ -164,12 +164,12 @@ discard block |
||
164 | 164 | throw new ArgumentException($parameter, $reflection); |
165 | 165 | } |
166 | 166 | |
167 | - try { |
|
167 | + try{ |
|
168 | 168 | //Requesting for contextual dependency |
169 | 169 | $arguments[] = $this->get($class->getName(), $name); |
170 | 170 | continue; |
171 | - } catch (AutowireException $e) { |
|
172 | - if ($parameter->isOptional()) { |
|
171 | + }catch (AutowireException $e){ |
|
172 | + if ($parameter->isOptional()){ |
|
173 | 173 | //This is optional dependency, skip |
174 | 174 | $arguments[] = null; |
175 | 175 | continue; |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | */ |
206 | 206 | public function get($id, string $context = null) |
207 | 207 | { |
208 | - if ($id instanceof Autowire) { |
|
208 | + if ($id instanceof Autowire){ |
|
209 | 209 | return $id->resolve($this); |
210 | 210 | } |
211 | 211 | |
@@ -226,34 +226,34 @@ discard block |
||
226 | 226 | */ |
227 | 227 | public function make(string $alias, array $parameters = [], string $context = null) |
228 | 228 | { |
229 | - if (!isset($this->bindings[$alias])) { |
|
229 | + if (!isset($this->bindings[$alias])){ |
|
230 | 230 | //No direct instructions how to construct class, make is automatically |
231 | 231 | return $this->autowire($alias, $parameters, $context); |
232 | 232 | } |
233 | 233 | |
234 | 234 | $binding = $this->bindings[$alias]; |
235 | - if (\is_object($binding)) { |
|
235 | + if (\is_object($binding)){ |
|
236 | 236 | //When binding is instance, assuming singleton |
237 | 237 | return $binding; |
238 | 238 | } |
239 | 239 | |
240 | - if (\is_string($binding)) { |
|
240 | + if (\is_string($binding)){ |
|
241 | 241 | //Binding is pointing to something else |
242 | 242 | return $this->make($binding, $parameters, $context); |
243 | 243 | } |
244 | 244 | |
245 | 245 | unset($this->bindings[$alias]); |
246 | - try { |
|
247 | - if ($binding[0] === $alias) { |
|
246 | + try{ |
|
247 | + if ($binding[0] === $alias){ |
|
248 | 248 | $instance = $this->autowire($alias, $parameters, $context); |
249 | - } else { |
|
249 | + }else{ |
|
250 | 250 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
251 | 251 | } |
252 | - } finally { |
|
252 | + }finally{ |
|
253 | 253 | $this->bindings[$alias] = $binding; |
254 | 254 | } |
255 | 255 | |
256 | - if ($binding[1]) { |
|
256 | + if ($binding[1]){ |
|
257 | 257 | //Indicates singleton |
258 | 258 | $this->bindings[$alias] = $instance; |
259 | 259 | } |
@@ -267,28 +267,28 @@ discard block |
||
267 | 267 | public function runScope(array $bindings, callable $scope) |
268 | 268 | { |
269 | 269 | $cleanup = $previous = []; |
270 | - foreach ($bindings as $alias => $resolver) { |
|
271 | - if (isset($this->bindings[$alias])) { |
|
270 | + foreach ($bindings as $alias => $resolver){ |
|
271 | + if (isset($this->bindings[$alias])){ |
|
272 | 272 | $previous[$alias] = $this->bindings[$alias]; |
273 | - } else { |
|
273 | + }else{ |
|
274 | 274 | $cleanup[] = $alias; |
275 | 275 | } |
276 | 276 | |
277 | 277 | $this->bind($alias, $resolver); |
278 | 278 | } |
279 | 279 | |
280 | - try { |
|
281 | - if (ContainerScope::getContainer() !== $this) { |
|
280 | + try{ |
|
281 | + if (ContainerScope::getContainer() !== $this){ |
|
282 | 282 | return ContainerScope::runScope($this, $scope); |
283 | 283 | } |
284 | 284 | |
285 | 285 | return $scope(); |
286 | - } finally { |
|
287 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
286 | + }finally{ |
|
287 | + foreach (\array_reverse($previous) as $alias => $resolver){ |
|
288 | 288 | $this->bindings[$alias] = $resolver; |
289 | 289 | } |
290 | 290 | |
291 | - foreach ($cleanup as $alias) { |
|
291 | + foreach ($cleanup as $alias){ |
|
292 | 292 | unset($this->bindings[$alias]); |
293 | 293 | } |
294 | 294 | } |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | */ |
304 | 304 | public function bind(string $alias, $resolver): void |
305 | 305 | { |
306 | - if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
306 | + if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire){ |
|
307 | 307 | // array means = execute me, false = not singleton |
308 | 308 | $this->bindings[$alias] = [$resolver, false]; |
309 | 309 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | */ |
322 | 322 | public function bindSingleton(string $alias, $resolver): void |
323 | 323 | { |
324 | - if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
324 | + if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire){ |
|
325 | 325 | // direct binding to an instance |
326 | 326 | $this->bindings[$alias] = $resolver; |
327 | 327 | |
@@ -336,11 +336,11 @@ discard block |
||
336 | 336 | */ |
337 | 337 | public function hasInstance(string $alias): bool |
338 | 338 | { |
339 | - if (!$this->has($alias)) { |
|
339 | + if (!$this->has($alias)){ |
|
340 | 340 | return false; |
341 | 341 | } |
342 | 342 | |
343 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
343 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){ |
|
344 | 344 | //Checking alias tree |
345 | 345 | $alias = $this->bindings[$alias]; |
346 | 346 | } |
@@ -366,18 +366,18 @@ discard block |
||
366 | 366 | */ |
367 | 367 | public function invoke($target, array $parameters = []) |
368 | 368 | { |
369 | - if (\is_array($target) && isset($target[1])) { |
|
369 | + if (\is_array($target) && isset($target[1])){ |
|
370 | 370 | // In a form of resolver and method |
371 | 371 | [$resolver, $method] = $target; |
372 | 372 | |
373 | 373 | // Resolver instance (i.e. [ClassName::class, 'method']) |
374 | - if (\is_string($resolver)) { |
|
374 | + if (\is_string($resolver)){ |
|
375 | 375 | $resolver = $this->get($resolver); |
376 | 376 | } |
377 | 377 | |
378 | - try { |
|
378 | + try{ |
|
379 | 379 | $method = new \ReflectionMethod($resolver, $method); |
380 | - } catch (\ReflectionException $e) { |
|
380 | + }catch (\ReflectionException $e){ |
|
381 | 381 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
382 | 382 | } |
383 | 383 | |
@@ -390,14 +390,14 @@ discard block |
||
390 | 390 | ); |
391 | 391 | } |
392 | 392 | |
393 | - if (\is_string($target) && \is_callable($target)) { |
|
393 | + if (\is_string($target) && \is_callable($target)){ |
|
394 | 394 | $target = Closure::fromCallable($target); |
395 | 395 | } |
396 | 396 | |
397 | - if ($target instanceof Closure) { |
|
398 | - try { |
|
397 | + if ($target instanceof Closure){ |
|
398 | + try{ |
|
399 | 399 | $reflection = new \ReflectionFunction($target); |
400 | - } catch (\ReflectionException $e) { |
|
400 | + }catch (\ReflectionException $e){ |
|
401 | 401 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
402 | 402 | } |
403 | 403 | |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | */ |
453 | 453 | protected function autowire(string $class, array $parameters, string $context = null) |
454 | 454 | { |
455 | - if (!\class_exists($class) && !isset($this->injectors[$class])) { |
|
455 | + if (!\class_exists($class) && !isset($this->injectors[$class])){ |
|
456 | 456 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
457 | 457 | } |
458 | 458 | |
@@ -467,7 +467,7 @@ discard block |
||
467 | 467 | { |
468 | 468 | $location = $reflection->getName(); |
469 | 469 | |
470 | - if ($reflection instanceof \ReflectionMethod) { |
|
470 | + if ($reflection instanceof \ReflectionMethod){ |
|
471 | 471 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
472 | 472 | } |
473 | 473 | |
@@ -483,8 +483,8 @@ discard block |
||
483 | 483 | */ |
484 | 484 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
485 | 485 | { |
486 | - if ($value === null) { |
|
487 | - if (!$parameter->allowsNull()) { |
|
486 | + if ($value === null){ |
|
487 | + if (!$parameter->allowsNull()){ |
|
488 | 488 | throw new ArgumentException($parameter, $context); |
489 | 489 | } |
490 | 490 | |
@@ -492,20 +492,20 @@ discard block |
||
492 | 492 | } |
493 | 493 | |
494 | 494 | $type = $parameter->getType(); |
495 | - if ($type === null) { |
|
495 | + if ($type === null){ |
|
496 | 496 | return; |
497 | 497 | } |
498 | 498 | |
499 | 499 | $typeName = $type->getName(); |
500 | - if ($typeName === 'array' && !\is_array($value)) { |
|
500 | + if ($typeName === 'array' && !\is_array($value)){ |
|
501 | 501 | throw new ArgumentException($parameter, $context); |
502 | 502 | } |
503 | 503 | |
504 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
504 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){ |
|
505 | 505 | throw new ArgumentException($parameter, $context); |
506 | 506 | } |
507 | 507 | |
508 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
508 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){ |
|
509 | 509 | throw new ArgumentException($parameter, $context); |
510 | 510 | } |
511 | 511 | } |
@@ -522,22 +522,22 @@ discard block |
||
522 | 522 | */ |
523 | 523 | private function createInstance(string $class, array $parameters, string $context = null) |
524 | 524 | { |
525 | - try { |
|
525 | + try{ |
|
526 | 526 | $reflection = new \ReflectionClass($class); |
527 | - } catch (\ReflectionException $e) { |
|
527 | + }catch (\ReflectionException $e){ |
|
528 | 528 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
529 | 529 | } |
530 | 530 | |
531 | 531 | //We have to construct class using external injector when we know exact context |
532 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
532 | + if ($parameters === [] && $this->checkInjector($reflection)){ |
|
533 | 533 | $injector = $this->injectors[$reflection->getName()]; |
534 | 534 | |
535 | 535 | $instance = null; |
536 | - try { |
|
536 | + try{ |
|
537 | 537 | /** @var InjectorInterface|mixed $injectorInstance */ |
538 | 538 | $injectorInstance = $this->get($injector); |
539 | 539 | |
540 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
540 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
541 | 541 | throw new InjectionException( |
542 | 542 | \sprintf( |
543 | 543 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -548,7 +548,7 @@ discard block |
||
548 | 548 | } |
549 | 549 | |
550 | 550 | $instance = $injectorInstance->createInjection($reflection, $context); |
551 | - if (!$reflection->isInstance($instance)) { |
|
551 | + if (!$reflection->isInstance($instance)){ |
|
552 | 552 | throw new InjectionException( |
553 | 553 | \sprintf( |
554 | 554 | "Invalid injection response for '%s'", |
@@ -556,23 +556,23 @@ discard block |
||
556 | 556 | ) |
557 | 557 | ); |
558 | 558 | } |
559 | - } finally { |
|
559 | + }finally{ |
|
560 | 560 | $this->injectors[$reflection->getName()] = $injector; |
561 | 561 | } |
562 | 562 | |
563 | 563 | return $instance; |
564 | 564 | } |
565 | 565 | |
566 | - if (!$reflection->isInstantiable()) { |
|
566 | + if (!$reflection->isInstantiable()){ |
|
567 | 567 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
568 | 568 | } |
569 | 569 | |
570 | 570 | $constructor = $reflection->getConstructor(); |
571 | 571 | |
572 | - if ($constructor !== null) { |
|
572 | + if ($constructor !== null){ |
|
573 | 573 | // Using constructor with resolved arguments |
574 | 574 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
575 | - } else { |
|
575 | + }else{ |
|
576 | 576 | // No constructor specified |
577 | 577 | $instance = $reflection->newInstance(); |
578 | 578 | } |
@@ -586,25 +586,25 @@ discard block |
||
586 | 586 | private function checkInjector(\ReflectionClass $reflection): bool |
587 | 587 | { |
588 | 588 | $class = $reflection->getName(); |
589 | - if (\array_key_exists($class, $this->injectors)) { |
|
589 | + if (\array_key_exists($class, $this->injectors)){ |
|
590 | 590 | return $this->injectors[$class] !== null; |
591 | 591 | } |
592 | 592 | |
593 | 593 | if ( |
594 | 594 | $reflection->implementsInterface(InjectableInterface::class) |
595 | 595 | && $reflection->hasConstant('INJECTOR') |
596 | - ) { |
|
596 | + ){ |
|
597 | 597 | $this->injectors[$class] = $reflection->getConstant('INJECTOR'); |
598 | 598 | |
599 | 599 | return true; |
600 | 600 | } |
601 | 601 | |
602 | 602 | // check interfaces |
603 | - foreach ($this->injectors as $target => $injector) { |
|
603 | + foreach ($this->injectors as $target => $injector){ |
|
604 | 604 | if ( |
605 | 605 | \class_exists($target, true) |
606 | 606 | && $reflection->isSubclassOf($target) |
607 | - ) { |
|
607 | + ){ |
|
608 | 608 | $this->injectors[$class] = $injector; |
609 | 609 | |
610 | 610 | return true; |
@@ -613,7 +613,7 @@ discard block |
||
613 | 613 | if ( |
614 | 614 | \interface_exists($target, true) |
615 | 615 | && $reflection->implementsInterface($target) |
616 | - ) { |
|
616 | + ){ |
|
617 | 617 | $this->injectors[$class] = $injector; |
618 | 618 | |
619 | 619 | return true; |
@@ -634,9 +634,9 @@ discard block |
||
634 | 634 | private function registerInstance($instance, array $parameters) |
635 | 635 | { |
636 | 636 | //Declarative singletons (only when class received via direct get) |
637 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
637 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
638 | 638 | $alias = \get_class($instance); |
639 | - if (!isset($this->bindings[$alias])) { |
|
639 | + if (!isset($this->bindings[$alias])){ |
|
640 | 640 | $this->bindings[$alias] = $instance; |
641 | 641 | } |
642 | 642 | } |
@@ -654,18 +654,18 @@ discard block |
||
654 | 654 | */ |
655 | 655 | private function evaluateBinding(string $alias, $target, array $parameters, string $context = null) |
656 | 656 | { |
657 | - if (\is_string($target)) { |
|
657 | + if (\is_string($target)){ |
|
658 | 658 | // Reference |
659 | 659 | return $this->make($target, $parameters, $context); |
660 | 660 | } |
661 | 661 | |
662 | - if ($target instanceof Autowire) { |
|
662 | + if ($target instanceof Autowire){ |
|
663 | 663 | return $target->resolve($this, $parameters); |
664 | 664 | } |
665 | 665 | |
666 | - try { |
|
666 | + try{ |
|
667 | 667 | return $this->invoke($target, $parameters); |
668 | - } catch (NotCallableException $e) { |
|
668 | + }catch (NotCallableException $e){ |
|
669 | 669 | throw new ContainerException(\sprintf("Invalid binding for '%s'", $alias), $e->getCode(), $e); |
670 | 670 | } |
671 | 671 | } |
@@ -94,7 +94,8 @@ discard block |
||
94 | 94 | { |
95 | 95 | $arguments = []; |
96 | 96 | |
97 | - foreach ($reflection->getParameters() as $parameter) { |
|
97 | + foreach ($reflection->getParameters() as $parameter) |
|
98 | + { |
|
98 | 99 | $type = $parameter->getType(); |
99 | 100 | $name = $parameter->getName(); |
100 | 101 | $class = null; |
@@ -103,7 +104,8 @@ discard block |
||
103 | 104 | * Container do not currently support union types. In the future, we |
104 | 105 | * can provide the possibility of autowiring based on priorities (TBD). |
105 | 106 | */ |
106 | - if ($type instanceof \ReflectionUnionType) { |
|
107 | + if ($type instanceof \ReflectionUnionType) |
|
108 | + { |
|
107 | 109 | $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously'; |
108 | 110 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
109 | 111 | |
@@ -113,17 +115,22 @@ discard block |
||
113 | 115 | /** |
114 | 116 | * Container do not currently support intersection types. |
115 | 117 | */ |
116 | - if ($type instanceof \ReflectionIntersectionType) { |
|
118 | + if ($type instanceof \ReflectionIntersectionType) |
|
119 | + { |
|
117 | 120 | $error = 'Parameter $%s in %s contains a intersection type hint that cannot be inferred unambiguously'; |
118 | 121 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
119 | 122 | |
120 | 123 | throw new ContainerException($error); |
121 | 124 | } |
122 | 125 | |
123 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { |
|
124 | - try { |
|
126 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) |
|
127 | + { |
|
128 | + try |
|
129 | + { |
|
125 | 130 | $class = new \ReflectionClass($type->getName()); |
126 | - } catch (\ReflectionException $e) { |
|
131 | + } |
|
132 | + catch (\ReflectionException $e) |
|
133 | + { |
|
127 | 134 | $location = $this->getLocationString($reflection); |
128 | 135 | |
129 | 136 | $error = 'Unable to resolve `\$%s` parameter in %s: %s'; |
@@ -133,11 +140,15 @@ discard block |
||
133 | 140 | } |
134 | 141 | } |
135 | 142 | |
136 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
137 | - if ($parameters[$name] instanceof Autowire) { |
|
143 | + if (isset($parameters[$name]) && is_object($parameters[$name])) |
|
144 | + { |
|
145 | + if ($parameters[$name] instanceof Autowire) |
|
146 | + { |
|
138 | 147 | // Supplied by user as late dependency |
139 | 148 | $arguments[] = $parameters[$name]->resolve($this); |
140 | - } else { |
|
149 | + } |
|
150 | + else |
|
151 | + { |
|
141 | 152 | // Supplied by user as object |
142 | 153 | $arguments[] = $parameters[$name]; |
143 | 154 | } |
@@ -145,16 +156,19 @@ discard block |
||
145 | 156 | } |
146 | 157 | |
147 | 158 | // no declared type or scalar type or array |
148 | - if (!isset($class)) { |
|
159 | + if (!isset($class)) |
|
160 | + { |
|
149 | 161 | // Provided from outside |
150 | - if (\array_key_exists($name, $parameters)) { |
|
162 | + if (\array_key_exists($name, $parameters)) |
|
163 | + { |
|
151 | 164 | // Make sure it's properly typed |
152 | 165 | $this->assertType($parameter, $reflection, $parameters[$name]); |
153 | 166 | $arguments[] = $parameters[$name]; |
154 | 167 | continue; |
155 | 168 | } |
156 | 169 | |
157 | - if ($parameter->isDefaultValueAvailable()) { |
|
170 | + if ($parameter->isDefaultValueAvailable()) |
|
171 | + { |
|
158 | 172 | //Default value |
159 | 173 | $arguments[] = $parameter->getDefaultValue(); |
160 | 174 | continue; |
@@ -164,12 +178,16 @@ discard block |
||
164 | 178 | throw new ArgumentException($parameter, $reflection); |
165 | 179 | } |
166 | 180 | |
167 | - try { |
|
181 | + try |
|
182 | + { |
|
168 | 183 | //Requesting for contextual dependency |
169 | 184 | $arguments[] = $this->get($class->getName(), $name); |
170 | 185 | continue; |
171 | - } catch (AutowireException $e) { |
|
172 | - if ($parameter->isOptional()) { |
|
186 | + } |
|
187 | + catch (AutowireException $e) |
|
188 | + { |
|
189 | + if ($parameter->isOptional()) |
|
190 | + { |
|
173 | 191 | //This is optional dependency, skip |
174 | 192 | $arguments[] = null; |
175 | 193 | continue; |
@@ -205,7 +223,8 @@ discard block |
||
205 | 223 | */ |
206 | 224 | public function get($id, string $context = null) |
207 | 225 | { |
208 | - if ($id instanceof Autowire) { |
|
226 | + if ($id instanceof Autowire) |
|
227 | + { |
|
209 | 228 | return $id->resolve($this); |
210 | 229 | } |
211 | 230 | |
@@ -226,34 +245,44 @@ discard block |
||
226 | 245 | */ |
227 | 246 | public function make(string $alias, array $parameters = [], string $context = null) |
228 | 247 | { |
229 | - if (!isset($this->bindings[$alias])) { |
|
248 | + if (!isset($this->bindings[$alias])) |
|
249 | + { |
|
230 | 250 | //No direct instructions how to construct class, make is automatically |
231 | 251 | return $this->autowire($alias, $parameters, $context); |
232 | 252 | } |
233 | 253 | |
234 | 254 | $binding = $this->bindings[$alias]; |
235 | - if (\is_object($binding)) { |
|
255 | + if (\is_object($binding)) |
|
256 | + { |
|
236 | 257 | //When binding is instance, assuming singleton |
237 | 258 | return $binding; |
238 | 259 | } |
239 | 260 | |
240 | - if (\is_string($binding)) { |
|
261 | + if (\is_string($binding)) |
|
262 | + { |
|
241 | 263 | //Binding is pointing to something else |
242 | 264 | return $this->make($binding, $parameters, $context); |
243 | 265 | } |
244 | 266 | |
245 | 267 | unset($this->bindings[$alias]); |
246 | - try { |
|
247 | - if ($binding[0] === $alias) { |
|
268 | + try |
|
269 | + { |
|
270 | + if ($binding[0] === $alias) |
|
271 | + { |
|
248 | 272 | $instance = $this->autowire($alias, $parameters, $context); |
249 | - } else { |
|
273 | + } |
|
274 | + else |
|
275 | + { |
|
250 | 276 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
251 | 277 | } |
252 | - } finally { |
|
278 | + } |
|
279 | + finally |
|
280 | + { |
|
253 | 281 | $this->bindings[$alias] = $binding; |
254 | 282 | } |
255 | 283 | |
256 | - if ($binding[1]) { |
|
284 | + if ($binding[1]) |
|
285 | + { |
|
257 | 286 | //Indicates singleton |
258 | 287 | $this->bindings[$alias] = $instance; |
259 | 288 | } |
@@ -267,28 +296,38 @@ discard block |
||
267 | 296 | public function runScope(array $bindings, callable $scope) |
268 | 297 | { |
269 | 298 | $cleanup = $previous = []; |
270 | - foreach ($bindings as $alias => $resolver) { |
|
271 | - if (isset($this->bindings[$alias])) { |
|
299 | + foreach ($bindings as $alias => $resolver) |
|
300 | + { |
|
301 | + if (isset($this->bindings[$alias])) |
|
302 | + { |
|
272 | 303 | $previous[$alias] = $this->bindings[$alias]; |
273 | - } else { |
|
304 | + } |
|
305 | + else |
|
306 | + { |
|
274 | 307 | $cleanup[] = $alias; |
275 | 308 | } |
276 | 309 | |
277 | 310 | $this->bind($alias, $resolver); |
278 | 311 | } |
279 | 312 | |
280 | - try { |
|
281 | - if (ContainerScope::getContainer() !== $this) { |
|
313 | + try |
|
314 | + { |
|
315 | + if (ContainerScope::getContainer() !== $this) |
|
316 | + { |
|
282 | 317 | return ContainerScope::runScope($this, $scope); |
283 | 318 | } |
284 | 319 | |
285 | 320 | return $scope(); |
286 | - } finally { |
|
287 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
321 | + } |
|
322 | + finally |
|
323 | + { |
|
324 | + foreach (\array_reverse($previous) as $alias => $resolver) |
|
325 | + { |
|
288 | 326 | $this->bindings[$alias] = $resolver; |
289 | 327 | } |
290 | 328 | |
291 | - foreach ($cleanup as $alias) { |
|
329 | + foreach ($cleanup as $alias) |
|
330 | + { |
|
292 | 331 | unset($this->bindings[$alias]); |
293 | 332 | } |
294 | 333 | } |
@@ -303,7 +342,8 @@ discard block |
||
303 | 342 | */ |
304 | 343 | public function bind(string $alias, $resolver): void |
305 | 344 | { |
306 | - if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
345 | + if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) |
|
346 | + { |
|
307 | 347 | // array means = execute me, false = not singleton |
308 | 348 | $this->bindings[$alias] = [$resolver, false]; |
309 | 349 | |
@@ -321,7 +361,8 @@ discard block |
||
321 | 361 | */ |
322 | 362 | public function bindSingleton(string $alias, $resolver): void |
323 | 363 | { |
324 | - if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
364 | + if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) |
|
365 | + { |
|
325 | 366 | // direct binding to an instance |
326 | 367 | $this->bindings[$alias] = $resolver; |
327 | 368 | |
@@ -336,11 +377,13 @@ discard block |
||
336 | 377 | */ |
337 | 378 | public function hasInstance(string $alias): bool |
338 | 379 | { |
339 | - if (!$this->has($alias)) { |
|
380 | + if (!$this->has($alias)) |
|
381 | + { |
|
340 | 382 | return false; |
341 | 383 | } |
342 | 384 | |
343 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
385 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) |
|
386 | + { |
|
344 | 387 | //Checking alias tree |
345 | 388 | $alias = $this->bindings[$alias]; |
346 | 389 | } |
@@ -366,18 +409,23 @@ discard block |
||
366 | 409 | */ |
367 | 410 | public function invoke($target, array $parameters = []) |
368 | 411 | { |
369 | - if (\is_array($target) && isset($target[1])) { |
|
412 | + if (\is_array($target) && isset($target[1])) |
|
413 | + { |
|
370 | 414 | // In a form of resolver and method |
371 | 415 | [$resolver, $method] = $target; |
372 | 416 | |
373 | 417 | // Resolver instance (i.e. [ClassName::class, 'method']) |
374 | - if (\is_string($resolver)) { |
|
418 | + if (\is_string($resolver)) |
|
419 | + { |
|
375 | 420 | $resolver = $this->get($resolver); |
376 | 421 | } |
377 | 422 | |
378 | - try { |
|
423 | + try |
|
424 | + { |
|
379 | 425 | $method = new \ReflectionMethod($resolver, $method); |
380 | - } catch (\ReflectionException $e) { |
|
426 | + } |
|
427 | + catch (\ReflectionException $e) |
|
428 | + { |
|
381 | 429 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
382 | 430 | } |
383 | 431 | |
@@ -390,14 +438,19 @@ discard block |
||
390 | 438 | ); |
391 | 439 | } |
392 | 440 | |
393 | - if (\is_string($target) && \is_callable($target)) { |
|
441 | + if (\is_string($target) && \is_callable($target)) |
|
442 | + { |
|
394 | 443 | $target = Closure::fromCallable($target); |
395 | 444 | } |
396 | 445 | |
397 | - if ($target instanceof Closure) { |
|
398 | - try { |
|
446 | + if ($target instanceof Closure) |
|
447 | + { |
|
448 | + try |
|
449 | + { |
|
399 | 450 | $reflection = new \ReflectionFunction($target); |
400 | - } catch (\ReflectionException $e) { |
|
451 | + } |
|
452 | + catch (\ReflectionException $e) |
|
453 | + { |
|
401 | 454 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
402 | 455 | } |
403 | 456 | |
@@ -452,7 +505,8 @@ discard block |
||
452 | 505 | */ |
453 | 506 | protected function autowire(string $class, array $parameters, string $context = null) |
454 | 507 | { |
455 | - if (!\class_exists($class) && !isset($this->injectors[$class])) { |
|
508 | + if (!\class_exists($class) && !isset($this->injectors[$class])) |
|
509 | + { |
|
456 | 510 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
457 | 511 | } |
458 | 512 | |
@@ -467,7 +521,8 @@ discard block |
||
467 | 521 | { |
468 | 522 | $location = $reflection->getName(); |
469 | 523 | |
470 | - if ($reflection instanceof \ReflectionMethod) { |
|
524 | + if ($reflection instanceof \ReflectionMethod) |
|
525 | + { |
|
471 | 526 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
472 | 527 | } |
473 | 528 | |
@@ -483,8 +538,10 @@ discard block |
||
483 | 538 | */ |
484 | 539 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
485 | 540 | { |
486 | - if ($value === null) { |
|
487 | - if (!$parameter->allowsNull()) { |
|
541 | + if ($value === null) |
|
542 | + { |
|
543 | + if (!$parameter->allowsNull()) |
|
544 | + { |
|
488 | 545 | throw new ArgumentException($parameter, $context); |
489 | 546 | } |
490 | 547 | |
@@ -492,20 +549,24 @@ discard block |
||
492 | 549 | } |
493 | 550 | |
494 | 551 | $type = $parameter->getType(); |
495 | - if ($type === null) { |
|
552 | + if ($type === null) |
|
553 | + { |
|
496 | 554 | return; |
497 | 555 | } |
498 | 556 | |
499 | 557 | $typeName = $type->getName(); |
500 | - if ($typeName === 'array' && !\is_array($value)) { |
|
558 | + if ($typeName === 'array' && !\is_array($value)) |
|
559 | + { |
|
501 | 560 | throw new ArgumentException($parameter, $context); |
502 | 561 | } |
503 | 562 | |
504 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
563 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) |
|
564 | + { |
|
505 | 565 | throw new ArgumentException($parameter, $context); |
506 | 566 | } |
507 | 567 | |
508 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
568 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) |
|
569 | + { |
|
509 | 570 | throw new ArgumentException($parameter, $context); |
510 | 571 | } |
511 | 572 | } |
@@ -522,22 +583,28 @@ discard block |
||
522 | 583 | */ |
523 | 584 | private function createInstance(string $class, array $parameters, string $context = null) |
524 | 585 | { |
525 | - try { |
|
586 | + try |
|
587 | + { |
|
526 | 588 | $reflection = new \ReflectionClass($class); |
527 | - } catch (\ReflectionException $e) { |
|
589 | + } |
|
590 | + catch (\ReflectionException $e) |
|
591 | + { |
|
528 | 592 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
529 | 593 | } |
530 | 594 | |
531 | 595 | //We have to construct class using external injector when we know exact context |
532 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
596 | + if ($parameters === [] && $this->checkInjector($reflection)) |
|
597 | + { |
|
533 | 598 | $injector = $this->injectors[$reflection->getName()]; |
534 | 599 | |
535 | 600 | $instance = null; |
536 | - try { |
|
601 | + try |
|
602 | + { |
|
537 | 603 | /** @var InjectorInterface|mixed $injectorInstance */ |
538 | 604 | $injectorInstance = $this->get($injector); |
539 | 605 | |
540 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
606 | + if (!$injectorInstance instanceof InjectorInterface) |
|
607 | + { |
|
541 | 608 | throw new InjectionException( |
542 | 609 | \sprintf( |
543 | 610 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -548,7 +615,8 @@ discard block |
||
548 | 615 | } |
549 | 616 | |
550 | 617 | $instance = $injectorInstance->createInjection($reflection, $context); |
551 | - if (!$reflection->isInstance($instance)) { |
|
618 | + if (!$reflection->isInstance($instance)) |
|
619 | + { |
|
552 | 620 | throw new InjectionException( |
553 | 621 | \sprintf( |
554 | 622 | "Invalid injection response for '%s'", |
@@ -556,23 +624,29 @@ discard block |
||
556 | 624 | ) |
557 | 625 | ); |
558 | 626 | } |
559 | - } finally { |
|
627 | + } |
|
628 | + finally |
|
629 | + { |
|
560 | 630 | $this->injectors[$reflection->getName()] = $injector; |
561 | 631 | } |
562 | 632 | |
563 | 633 | return $instance; |
564 | 634 | } |
565 | 635 | |
566 | - if (!$reflection->isInstantiable()) { |
|
636 | + if (!$reflection->isInstantiable()) |
|
637 | + { |
|
567 | 638 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
568 | 639 | } |
569 | 640 | |
570 | 641 | $constructor = $reflection->getConstructor(); |
571 | 642 | |
572 | - if ($constructor !== null) { |
|
643 | + if ($constructor !== null) |
|
644 | + { |
|
573 | 645 | // Using constructor with resolved arguments |
574 | 646 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
575 | - } else { |
|
647 | + } |
|
648 | + else |
|
649 | + { |
|
576 | 650 | // No constructor specified |
577 | 651 | $instance = $reflection->newInstance(); |
578 | 652 | } |
@@ -586,7 +660,8 @@ discard block |
||
586 | 660 | private function checkInjector(\ReflectionClass $reflection): bool |
587 | 661 | { |
588 | 662 | $class = $reflection->getName(); |
589 | - if (\array_key_exists($class, $this->injectors)) { |
|
663 | + if (\array_key_exists($class, $this->injectors)) |
|
664 | + { |
|
590 | 665 | return $this->injectors[$class] !== null; |
591 | 666 | } |
592 | 667 | |
@@ -600,7 +675,8 @@ discard block |
||
600 | 675 | } |
601 | 676 | |
602 | 677 | // check interfaces |
603 | - foreach ($this->injectors as $target => $injector) { |
|
678 | + foreach ($this->injectors as $target => $injector) |
|
679 | + { |
|
604 | 680 | if ( |
605 | 681 | \class_exists($target, true) |
606 | 682 | && $reflection->isSubclassOf($target) |
@@ -634,9 +710,11 @@ discard block |
||
634 | 710 | private function registerInstance($instance, array $parameters) |
635 | 711 | { |
636 | 712 | //Declarative singletons (only when class received via direct get) |
637 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
713 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
714 | + { |
|
638 | 715 | $alias = \get_class($instance); |
639 | - if (!isset($this->bindings[$alias])) { |
|
716 | + if (!isset($this->bindings[$alias])) |
|
717 | + { |
|
640 | 718 | $this->bindings[$alias] = $instance; |
641 | 719 | } |
642 | 720 | } |
@@ -654,18 +732,23 @@ discard block |
||
654 | 732 | */ |
655 | 733 | private function evaluateBinding(string $alias, $target, array $parameters, string $context = null) |
656 | 734 | { |
657 | - if (\is_string($target)) { |
|
735 | + if (\is_string($target)) |
|
736 | + { |
|
658 | 737 | // Reference |
659 | 738 | return $this->make($target, $parameters, $context); |
660 | 739 | } |
661 | 740 | |
662 | - if ($target instanceof Autowire) { |
|
741 | + if ($target instanceof Autowire) |
|
742 | + { |
|
663 | 743 | return $target->resolve($this, $parameters); |
664 | 744 | } |
665 | 745 | |
666 | - try { |
|
746 | + try |
|
747 | + { |
|
667 | 748 | return $this->invoke($target, $parameters); |
668 | - } catch (NotCallableException $e) { |
|
749 | + } |
|
750 | + catch (NotCallableException $e) |
|
751 | + { |
|
669 | 752 | throw new ContainerException(\sprintf("Invalid binding for '%s'", $alias), $e->getCode(), $e); |
670 | 753 | } |
671 | 754 | } |
@@ -13,11 +13,11 @@ |
||
13 | 13 | |
14 | 14 | class UnionTypes |
15 | 15 | { |
16 | - public static function example(SampleClass|TypedClass $example) |
|
16 | + public static function example(SampleClass | TypedClass $example) |
|
17 | 17 | { |
18 | 18 | } |
19 | 19 | |
20 | - public static function unionNull(null|string $nullable): null|string |
|
20 | + public static function unionNull(null | string $nullable): null | string |
|
21 | 21 | { |
22 | 22 | return $nullable; |
23 | 23 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
100 | 100 | |
101 | 101 | $result = $this->container->invoke( |
102 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
102 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz'){ |
|
103 | 103 | return \compact('bucket', 'class', 'name', 'path'); |
104 | 104 | }, |
105 | 105 | ['name' => 'bar'] |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | $this->expectErrorMessage("Unable to resolve 'name' argument in 'Spiral\Tests\Core\Fixtures\Bucket::__construct'"); |
118 | 118 | |
119 | 119 | $this->container->invoke( |
120 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
120 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz'){ |
|
121 | 121 | return \compact('bucket', 'class', 'name', 'path'); |
122 | 122 | }, |
123 | 123 | ['name' => 'bar'] |
@@ -99,7 +99,8 @@ discard block |
||
99 | 99 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
100 | 100 | |
101 | 101 | $result = $this->container->invoke( |
102 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
102 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') |
|
103 | + { |
|
103 | 104 | return \compact('bucket', 'class', 'name', 'path'); |
104 | 105 | }, |
105 | 106 | ['name' => 'bar'] |
@@ -117,7 +118,8 @@ discard block |
||
117 | 118 | $this->expectErrorMessage("Unable to resolve 'name' argument in 'Spiral\Tests\Core\Fixtures\Bucket::__construct'"); |
118 | 119 | |
119 | 120 | $this->container->invoke( |
120 | - static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') { |
|
121 | + static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') |
|
122 | + { |
|
121 | 123 | return \compact('bucket', 'class', 'name', 'path'); |
122 | 124 | }, |
123 | 125 | ['name' => 'bar'] |
@@ -28,12 +28,12 @@ |
||
28 | 28 | |
29 | 29 | private function defineValue(?string $sameSite, bool $secure): ?string |
30 | 30 | { |
31 | - if ($sameSite === null) { |
|
31 | + if ($sameSite === null){ |
|
32 | 32 | return null; |
33 | 33 | } |
34 | 34 | |
35 | 35 | $sameSite = ucfirst(strtolower($sameSite)); |
36 | - if (!in_array($sameSite, self::VALUES, true)) { |
|
36 | + if (!in_array($sameSite, self::VALUES, true)){ |
|
37 | 37 | return null; |
38 | 38 | } |
39 | 39 |
@@ -28,12 +28,14 @@ |
||
28 | 28 | |
29 | 29 | private function defineValue(?string $sameSite, bool $secure): ?string |
30 | 30 | { |
31 | - if ($sameSite === null) { |
|
31 | + if ($sameSite === null) |
|
32 | + { |
|
32 | 33 | return null; |
33 | 34 | } |
34 | 35 | |
35 | 36 | $sameSite = ucfirst(strtolower($sameSite)); |
36 | - if (!in_array($sameSite, self::VALUES, true)) { |
|
37 | + if (!in_array($sameSite, self::VALUES, true)) |
|
38 | + { |
|
37 | 39 | return null; |
38 | 40 | } |
39 | 41 |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | public const NONE = 'None'; |
12 | 12 | |
13 | 13 | private const VALUES = [self::STRICT, self::LAX, self::NONE]; |
14 | - private const DEFAULT = self::LAX; |
|
14 | + private const default = self::LAX; |
|
15 | 15 | |
16 | 16 | private ?string $sameSite; |
17 | 17 | |
@@ -36,6 +36,6 @@ discard block |
||
36 | 36 | return null; |
37 | 37 | } |
38 | 38 | |
39 | - return ($sameSite === self::NONE && !$secure) ? self::DEFAULT : $sameSite; |
|
39 | + return ($sameSite === self::NONE && !$secure) ? self::default : $sameSite; |
|
40 | 40 | } |
41 | 41 | } |
@@ -66,8 +66,8 @@ discard block |
||
66 | 66 | { |
67 | 67 | $cookies = $request->getCookieParams(); |
68 | 68 | |
69 | - foreach ($cookies as $name => $cookie) { |
|
70 | - if (!$this->isProtected($name)) { |
|
69 | + foreach ($cookies as $name => $cookie){ |
|
70 | + if (!$this->isProtected($name)){ |
|
71 | 71 | continue; |
72 | 72 | } |
73 | 73 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | protected function isProtected(string $cookie): bool |
84 | 84 | { |
85 | - if (in_array($cookie, $this->config->getExcludedCookies(), true)) { |
|
85 | + if (in_array($cookie, $this->config->getExcludedCookies(), true)){ |
|
86 | 86 | //Excluded |
87 | 87 | return false; |
88 | 88 | } |
@@ -98,14 +98,14 @@ discard block |
||
98 | 98 | */ |
99 | 99 | protected function packCookies(Response $response, CookieQueue $queue): Response |
100 | 100 | { |
101 | - if (empty($queue->getScheduled())) { |
|
101 | + if (empty($queue->getScheduled())){ |
|
102 | 102 | return $response; |
103 | 103 | } |
104 | 104 | |
105 | 105 | $cookies = $response->getHeader('Set-Cookie'); |
106 | 106 | |
107 | - foreach ($queue->getScheduled() as $cookie) { |
|
108 | - if (empty($cookie->getValue()) || !$this->isProtected($cookie->getName())) { |
|
107 | + foreach ($queue->getScheduled() as $cookie){ |
|
108 | + if (empty($cookie->getValue()) || !$this->isProtected($cookie->getName())){ |
|
109 | 109 | $cookies[] = $cookie->createHeader(); |
110 | 110 | continue; |
111 | 111 | } |
@@ -122,26 +122,26 @@ discard block |
||
122 | 122 | */ |
123 | 123 | private function decodeCookie($cookie) |
124 | 124 | { |
125 | - try { |
|
126 | - if (is_array($cookie)) { |
|
125 | + try{ |
|
126 | + if (is_array($cookie)){ |
|
127 | 127 | return array_map([$this, 'decodeCookie'], $cookie); |
128 | 128 | } |
129 | - } catch (DecryptException $exception) { |
|
129 | + }catch (DecryptException $exception){ |
|
130 | 130 | return null; |
131 | 131 | } |
132 | 132 | |
133 | - switch ($this->config->getProtectionMethod()) { |
|
133 | + switch ($this->config->getProtectionMethod()){ |
|
134 | 134 | case CookiesConfig::COOKIE_ENCRYPT: |
135 | - try { |
|
135 | + try{ |
|
136 | 136 | return $this->encryption->getEncrypter()->decrypt($cookie); |
137 | - } catch (DecryptException $e) { |
|
137 | + }catch (DecryptException $e){ |
|
138 | 138 | } |
139 | 139 | return null; |
140 | 140 | case CookiesConfig::COOKIE_HMAC: |
141 | 141 | $hmac = substr($cookie, -1 * CookiesConfig::MAC_LENGTH); |
142 | 142 | $value = substr($cookie, 0, strlen($cookie) - strlen($hmac)); |
143 | 143 | |
144 | - if (hash_equals($this->hmacSign($value), $hmac)) { |
|
144 | + if (hash_equals($this->hmacSign($value), $hmac)){ |
|
145 | 145 | return $value; |
146 | 146 | } |
147 | 147 | } |
@@ -165,13 +165,13 @@ discard block |
||
165 | 165 | |
166 | 166 | private function encodeCookie(Cookie $cookie): Cookie |
167 | 167 | { |
168 | - if ($this->config->getProtectionMethod() === CookiesConfig::COOKIE_ENCRYPT) { |
|
168 | + if ($this->config->getProtectionMethod() === CookiesConfig::COOKIE_ENCRYPT){ |
|
169 | 169 | $encryptor = $this->encryption->getEncrypter(); |
170 | 170 | |
171 | 171 | return $cookie->withValue($encryptor->encrypt($cookie->getValue())); |
172 | 172 | } |
173 | 173 | |
174 | 174 | //VALUE.HMAC |
175 | - return $cookie->withValue($cookie->getValue() . $this->hmacSign($cookie->getValue())); |
|
175 | + return $cookie->withValue($cookie->getValue().$this->hmacSign($cookie->getValue())); |
|
176 | 176 | } |
177 | 177 | } |
@@ -66,8 +66,10 @@ discard block |
||
66 | 66 | { |
67 | 67 | $cookies = $request->getCookieParams(); |
68 | 68 | |
69 | - foreach ($cookies as $name => $cookie) { |
|
70 | - if (!$this->isProtected($name)) { |
|
69 | + foreach ($cookies as $name => $cookie) |
|
70 | + { |
|
71 | + if (!$this->isProtected($name)) |
|
72 | + { |
|
71 | 73 | continue; |
72 | 74 | } |
73 | 75 | |
@@ -82,7 +84,8 @@ discard block |
||
82 | 84 | */ |
83 | 85 | protected function isProtected(string $cookie): bool |
84 | 86 | { |
85 | - if (in_array($cookie, $this->config->getExcludedCookies(), true)) { |
|
87 | + if (in_array($cookie, $this->config->getExcludedCookies(), true)) |
|
88 | + { |
|
86 | 89 | //Excluded |
87 | 90 | return false; |
88 | 91 | } |
@@ -98,14 +101,17 @@ discard block |
||
98 | 101 | */ |
99 | 102 | protected function packCookies(Response $response, CookieQueue $queue): Response |
100 | 103 | { |
101 | - if (empty($queue->getScheduled())) { |
|
104 | + if (empty($queue->getScheduled())) |
|
105 | + { |
|
102 | 106 | return $response; |
103 | 107 | } |
104 | 108 | |
105 | 109 | $cookies = $response->getHeader('Set-Cookie'); |
106 | 110 | |
107 | - foreach ($queue->getScheduled() as $cookie) { |
|
108 | - if (empty($cookie->getValue()) || !$this->isProtected($cookie->getName())) { |
|
111 | + foreach ($queue->getScheduled() as $cookie) |
|
112 | + { |
|
113 | + if (empty($cookie->getValue()) || !$this->isProtected($cookie->getName())) |
|
114 | + { |
|
109 | 115 | $cookies[] = $cookie->createHeader(); |
110 | 116 | continue; |
111 | 117 | } |
@@ -122,26 +128,35 @@ discard block |
||
122 | 128 | */ |
123 | 129 | private function decodeCookie($cookie) |
124 | 130 | { |
125 | - try { |
|
126 | - if (is_array($cookie)) { |
|
131 | + try |
|
132 | + { |
|
133 | + if (is_array($cookie)) |
|
134 | + { |
|
127 | 135 | return array_map([$this, 'decodeCookie'], $cookie); |
128 | 136 | } |
129 | - } catch (DecryptException $exception) { |
|
137 | + } |
|
138 | + catch (DecryptException $exception) |
|
139 | + { |
|
130 | 140 | return null; |
131 | 141 | } |
132 | 142 | |
133 | - switch ($this->config->getProtectionMethod()) { |
|
143 | + switch ($this->config->getProtectionMethod()) |
|
144 | + { |
|
134 | 145 | case CookiesConfig::COOKIE_ENCRYPT: |
135 | - try { |
|
146 | + try |
|
147 | + { |
|
136 | 148 | return $this->encryption->getEncrypter()->decrypt($cookie); |
137 | - } catch (DecryptException $e) { |
|
149 | + } |
|
150 | + catch (DecryptException $e) |
|
151 | + { |
|
138 | 152 | } |
139 | 153 | return null; |
140 | 154 | case CookiesConfig::COOKIE_HMAC: |
141 | 155 | $hmac = substr($cookie, -1 * CookiesConfig::MAC_LENGTH); |
142 | 156 | $value = substr($cookie, 0, strlen($cookie) - strlen($hmac)); |
143 | 157 | |
144 | - if (hash_equals($this->hmacSign($value), $hmac)) { |
|
158 | + if (hash_equals($this->hmacSign($value), $hmac)) |
|
159 | + { |
|
145 | 160 | return $value; |
146 | 161 | } |
147 | 162 | } |
@@ -165,7 +180,8 @@ discard block |
||
165 | 180 | |
166 | 181 | private function encodeCookie(Cookie $cookie): Cookie |
167 | 182 | { |
168 | - if ($this->config->getProtectionMethod() === CookiesConfig::COOKIE_ENCRYPT) { |
|
183 | + if ($this->config->getProtectionMethod() === CookiesConfig::COOKIE_ENCRYPT) |
|
184 | + { |
|
169 | 185 | $encryptor = $this->encryption->getEncrypter(); |
170 | 186 | |
171 | 187 | return $cookie->withValue($encryptor->encrypt($cookie->getValue())); |
@@ -39,17 +39,17 @@ discard block |
||
39 | 39 | |
40 | 40 | public function __toString(): string |
41 | 41 | { |
42 | - if ($this->value === '' || $this->value === null) { |
|
42 | + if ($this->value === '' || $this->value === null){ |
|
43 | 43 | return ''; |
44 | 44 | } |
45 | 45 | |
46 | 46 | $parts = [$this->value]; |
47 | 47 | |
48 | - if ($this->quality < 1) { |
|
48 | + if ($this->quality < 1){ |
|
49 | 49 | $parts[] = "q=$this->quality"; |
50 | 50 | } |
51 | 51 | |
52 | - foreach ($this->getParams() as $name => $value) { |
|
52 | + foreach ($this->getParams() as $name => $value){ |
|
53 | 53 | $parts[] = "$name=$value"; |
54 | 54 | } |
55 | 55 | |
@@ -67,20 +67,20 @@ discard block |
||
67 | 67 | $quality = 1.0; |
68 | 68 | $params = []; |
69 | 69 | |
70 | - foreach ($elements as $element) { |
|
70 | + foreach ($elements as $element){ |
|
71 | 71 | $parsed = \explode('=', \trim($element), 2); |
72 | 72 | |
73 | 73 | // Wrong params must be ignored |
74 | - if (\count($parsed) !== 2) { |
|
74 | + if (\count($parsed) !== 2){ |
|
75 | 75 | continue; |
76 | 76 | } |
77 | 77 | |
78 | 78 | $name = \trim($parsed[0]); |
79 | 79 | $value = \trim($parsed[1]); |
80 | 80 | |
81 | - if (\strcasecmp($name, 'q') === 0) { |
|
81 | + if (\strcasecmp($name, 'q') === 0){ |
|
82 | 82 | $quality = (float)$value; |
83 | - } else { |
|
83 | + }else{ |
|
84 | 84 | $params[$name] = $value; |
85 | 85 | } |
86 | 86 | } |
@@ -139,15 +139,15 @@ discard block |
||
139 | 139 | |
140 | 140 | private function setParams(array $params): void |
141 | 141 | { |
142 | - foreach ($params as $name => $value) { |
|
143 | - if (is_numeric($name) || !is_scalar($value)) { |
|
142 | + foreach ($params as $name => $value){ |
|
143 | + if (is_numeric($name) || !is_scalar($value)){ |
|
144 | 144 | continue; |
145 | 145 | } |
146 | 146 | |
147 | 147 | $name = trim($name); |
148 | 148 | $value = trim($value); |
149 | 149 | |
150 | - if ($name === '' || $value === '') { |
|
150 | + if ($name === '' || $value === ''){ |
|
151 | 151 | continue; |
152 | 152 | } |
153 | 153 |
@@ -39,17 +39,20 @@ discard block |
||
39 | 39 | |
40 | 40 | public function __toString(): string |
41 | 41 | { |
42 | - if ($this->value === '' || $this->value === null) { |
|
42 | + if ($this->value === '' || $this->value === null) |
|
43 | + { |
|
43 | 44 | return ''; |
44 | 45 | } |
45 | 46 | |
46 | 47 | $parts = [$this->value]; |
47 | 48 | |
48 | - if ($this->quality < 1) { |
|
49 | + if ($this->quality < 1) |
|
50 | + { |
|
49 | 51 | $parts[] = "q=$this->quality"; |
50 | 52 | } |
51 | 53 | |
52 | - foreach ($this->getParams() as $name => $value) { |
|
54 | + foreach ($this->getParams() as $name => $value) |
|
55 | + { |
|
53 | 56 | $parts[] = "$name=$value"; |
54 | 57 | } |
55 | 58 | |
@@ -67,20 +70,25 @@ discard block |
||
67 | 70 | $quality = 1.0; |
68 | 71 | $params = []; |
69 | 72 | |
70 | - foreach ($elements as $element) { |
|
73 | + foreach ($elements as $element) |
|
74 | + { |
|
71 | 75 | $parsed = \explode('=', \trim($element), 2); |
72 | 76 | |
73 | 77 | // Wrong params must be ignored |
74 | - if (\count($parsed) !== 2) { |
|
78 | + if (\count($parsed) !== 2) |
|
79 | + { |
|
75 | 80 | continue; |
76 | 81 | } |
77 | 82 | |
78 | 83 | $name = \trim($parsed[0]); |
79 | 84 | $value = \trim($parsed[1]); |
80 | 85 | |
81 | - if (\strcasecmp($name, 'q') === 0) { |
|
86 | + if (\strcasecmp($name, 'q') === 0) |
|
87 | + { |
|
82 | 88 | $quality = (float)$value; |
83 | - } else { |
|
89 | + } |
|
90 | + else |
|
91 | + { |
|
84 | 92 | $params[$name] = $value; |
85 | 93 | } |
86 | 94 | } |
@@ -139,15 +147,18 @@ discard block |
||
139 | 147 | |
140 | 148 | private function setParams(array $params): void |
141 | 149 | { |
142 | - foreach ($params as $name => $value) { |
|
143 | - if (is_numeric($name) || !is_scalar($value)) { |
|
150 | + foreach ($params as $name => $value) |
|
151 | + { |
|
152 | + if (is_numeric($name) || !is_scalar($value)) |
|
153 | + { |
|
144 | 154 | continue; |
145 | 155 | } |
146 | 156 | |
147 | 157 | $name = trim($name); |
148 | 158 | $value = trim($value); |
149 | 159 | |
150 | - if ($name === '' || $value === '') { |
|
160 | + if ($name === '' || $value === '') |
|
161 | + { |
|
151 | 162 | continue; |
152 | 163 | } |
153 | 164 |
@@ -50,9 +50,9 @@ discard block |
||
50 | 50 | |
51 | 51 | public function all(): array |
52 | 52 | { |
53 | - try { |
|
53 | + try{ |
|
54 | 54 | return $this->dotGet(''); |
55 | - } catch (DotNotFoundException $e) { |
|
55 | + }catch (DotNotFoundException $e){ |
|
56 | 56 | return []; |
57 | 57 | } |
58 | 58 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | { |
78 | 78 | $result = array_intersect_key($this->all(), array_flip($keys)); |
79 | 79 | ; |
80 | - if (!$fill) { |
|
80 | + if (!$fill){ |
|
81 | 81 | return $result; |
82 | 82 | } |
83 | 83 | |
@@ -97,9 +97,9 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function has(string $name): bool |
99 | 99 | { |
100 | - try { |
|
100 | + try{ |
|
101 | 101 | $this->dotGet($name); |
102 | - } catch (DotNotFoundException $e) { |
|
102 | + }catch (DotNotFoundException $e){ |
|
103 | 103 | return false; |
104 | 104 | } |
105 | 105 | |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | */ |
124 | 124 | public function get(string $name, $default = null) |
125 | 125 | { |
126 | - try { |
|
126 | + try{ |
|
127 | 127 | return $this->dotGet($name); |
128 | - } catch (DotNotFoundException $e) { |
|
128 | + }catch (DotNotFoundException $e){ |
|
129 | 129 | return $default; |
130 | 130 | } |
131 | 131 | } |
@@ -161,15 +161,15 @@ discard block |
||
161 | 161 | $data = $this->data; |
162 | 162 | |
163 | 163 | //Generating path relative to a given name and prefix |
164 | - $path = (!empty($this->prefix) ? $this->prefix . '.' : '') . $name; |
|
165 | - if (empty($path)) { |
|
164 | + $path = (!empty($this->prefix) ? $this->prefix.'.' : '').$name; |
|
165 | + if (empty($path)){ |
|
166 | 166 | return $data; |
167 | 167 | } |
168 | 168 | |
169 | 169 | $path = explode('.', rtrim($path, '.')); |
170 | 170 | |
171 | - foreach ($path as $step) { |
|
172 | - if (!is_array($data) || !array_key_exists($step, $data)) { |
|
171 | + foreach ($path as $step){ |
|
172 | + if (!is_array($data) || !array_key_exists($step, $data)){ |
|
173 | 173 | throw new DotNotFoundException("Unable to find requested element '{$name}'"); |
174 | 174 | } |
175 | 175 |
@@ -50,9 +50,12 @@ discard block |
||
50 | 50 | |
51 | 51 | public function all(): array |
52 | 52 | { |
53 | - try { |
|
53 | + try |
|
54 | + { |
|
54 | 55 | return $this->dotGet(''); |
55 | - } catch (DotNotFoundException $e) { |
|
56 | + } |
|
57 | + catch (DotNotFoundException $e) |
|
58 | + { |
|
56 | 59 | return []; |
57 | 60 | } |
58 | 61 | } |
@@ -77,7 +80,8 @@ discard block |
||
77 | 80 | { |
78 | 81 | $result = array_intersect_key($this->all(), array_flip($keys)); |
79 | 82 | ; |
80 | - if (!$fill) { |
|
83 | + if (!$fill) |
|
84 | + { |
|
81 | 85 | return $result; |
82 | 86 | } |
83 | 87 | |
@@ -97,9 +101,12 @@ discard block |
||
97 | 101 | */ |
98 | 102 | public function has(string $name): bool |
99 | 103 | { |
100 | - try { |
|
104 | + try |
|
105 | + { |
|
101 | 106 | $this->dotGet($name); |
102 | - } catch (DotNotFoundException $e) { |
|
107 | + } |
|
108 | + catch (DotNotFoundException $e) |
|
109 | + { |
|
103 | 110 | return false; |
104 | 111 | } |
105 | 112 | |
@@ -123,9 +130,12 @@ discard block |
||
123 | 130 | */ |
124 | 131 | public function get(string $name, $default = null) |
125 | 132 | { |
126 | - try { |
|
133 | + try |
|
134 | + { |
|
127 | 135 | return $this->dotGet($name); |
128 | - } catch (DotNotFoundException $e) { |
|
136 | + } |
|
137 | + catch (DotNotFoundException $e) |
|
138 | + { |
|
129 | 139 | return $default; |
130 | 140 | } |
131 | 141 | } |
@@ -162,14 +172,17 @@ discard block |
||
162 | 172 | |
163 | 173 | //Generating path relative to a given name and prefix |
164 | 174 | $path = (!empty($this->prefix) ? $this->prefix . '.' : '') . $name; |
165 | - if (empty($path)) { |
|
175 | + if (empty($path)) |
|
176 | + { |
|
166 | 177 | return $data; |
167 | 178 | } |
168 | 179 | |
169 | 180 | $path = explode('.', rtrim($path, '.')); |
170 | 181 | |
171 | - foreach ($path as $step) { |
|
172 | - if (!is_array($data) || !array_key_exists($step, $data)) { |
|
182 | + foreach ($path as $step) |
|
183 | + { |
|
184 | + if (!is_array($data) || !array_key_exists($step, $data)) |
|
185 | + { |
|
173 | 186 | throw new DotNotFoundException("Unable to find requested element '{$name}'"); |
174 | 187 | } |
175 | 188 |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | UriFactoryInterface $uriFactory, |
72 | 72 | StreamFactoryInterface $streamFactory, |
73 | 73 | UploadedFileFactoryInterface $uploadedFileFactory |
74 | - ) { |
|
74 | + ){ |
|
75 | 75 | $this->requestFactory = $requestFactory; |
76 | 76 | $this->uriFactory = $uriFactory; |
77 | 77 | $this->streamFactory = $streamFactory; |
@@ -108,12 +108,12 @@ discard block |
||
108 | 108 | $uri = $this->getUri($server); |
109 | 109 | |
110 | 110 | $request = $this->requestFactory->createServerRequest($method, $uri, $server); |
111 | - foreach ($headers as $name => $value) { |
|
111 | + foreach ($headers as $name => $value){ |
|
112 | 112 | $request = $request->withAddedHeader($name, $value); |
113 | 113 | } |
114 | 114 | |
115 | 115 | $protocol = '1.1'; |
116 | - if (!empty($_SERVER['SERVER_PROTOCOL'])) { |
|
116 | + if (!empty($_SERVER['SERVER_PROTOCOL'])){ |
|
117 | 117 | $protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); |
118 | 118 | } |
119 | 119 | |
@@ -124,15 +124,15 @@ discard block |
||
124 | 124 | ->withCookieParams($cookies) |
125 | 125 | ->withUploadedFiles($this->getUploadedFilesArray($files)); |
126 | 126 | |
127 | - if ($body === null) { |
|
127 | + if ($body === null){ |
|
128 | 128 | return $request; |
129 | 129 | } |
130 | 130 | |
131 | - if (\is_resource($body)) { |
|
131 | + if (\is_resource($body)){ |
|
132 | 132 | $body = $this->streamFactory->createStreamFromResource($body); |
133 | - } elseif (\is_string($body)) { |
|
133 | + } elseif (\is_string($body)){ |
|
134 | 134 | $body = $this->streamFactory->createStream($body); |
135 | - } elseif (!$body instanceof StreamInterface) { |
|
135 | + } elseif (!$body instanceof StreamInterface){ |
|
136 | 136 | throw new \InvalidArgumentException( |
137 | 137 | 'Body parameter for ServerRequestFactory::createFromParameters() ' |
138 | 138 | . 'must be instance of StreamInterface, resource or null.' |
@@ -145,29 +145,29 @@ discard block |
||
145 | 145 | private function getUri(array $server): UriInterface |
146 | 146 | { |
147 | 147 | $uri = $this->uriFactory->createUri(); |
148 | - if (isset($server['HTTPS'])) { |
|
148 | + if (isset($server['HTTPS'])){ |
|
149 | 149 | $uri = $uri->withScheme($server['HTTPS'] === 'on' ? 'https' : 'http'); |
150 | 150 | } |
151 | 151 | |
152 | - if (isset($server['HTTP_HOST'])) { |
|
153 | - if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) { |
|
152 | + if (isset($server['HTTP_HOST'])){ |
|
153 | + if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)){ |
|
154 | 154 | $uri = $uri->withHost($matches[1])->withPort($matches[2]); |
155 | - } else { |
|
155 | + }else{ |
|
156 | 156 | $uri = $uri->withHost($server['HTTP_HOST']); |
157 | 157 | } |
158 | - } elseif (isset($server['SERVER_NAME'])) { |
|
158 | + } elseif (isset($server['SERVER_NAME'])){ |
|
159 | 159 | $uri = $uri->withHost($server['SERVER_NAME']); |
160 | 160 | } |
161 | 161 | |
162 | - if (isset($server['SERVER_PORT'])) { |
|
162 | + if (isset($server['SERVER_PORT'])){ |
|
163 | 163 | $uri = $uri->withPort($server['SERVER_PORT']); |
164 | 164 | } |
165 | 165 | |
166 | - if (isset($server['REQUEST_URI'])) { |
|
166 | + if (isset($server['REQUEST_URI'])){ |
|
167 | 167 | $uri = $uri->withPath(\explode('?', $server['REQUEST_URI'])[0]); |
168 | 168 | } |
169 | 169 | |
170 | - if (isset($server['QUERY_STRING'])) { |
|
170 | + if (isset($server['QUERY_STRING'])){ |
|
171 | 171 | $uri = $uri->withQuery($server['QUERY_STRING']); |
172 | 172 | } |
173 | 173 | |
@@ -176,12 +176,12 @@ discard block |
||
176 | 176 | |
177 | 177 | private static function getHeadersFromGlobals(): array |
178 | 178 | { |
179 | - if (\function_exists('getallheaders')) { |
|
179 | + if (\function_exists('getallheaders')){ |
|
180 | 180 | $headers = getallheaders(); |
181 | - } else { |
|
181 | + }else{ |
|
182 | 182 | $headers = []; |
183 | - foreach ($_SERVER as $name => $value) { |
|
184 | - if (strncmp($name, 'HTTP_', 5) === 0) { |
|
183 | + foreach ($_SERVER as $name => $value){ |
|
184 | + if (strncmp($name, 'HTTP_', 5) === 0){ |
|
185 | 185 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
186 | 186 | $headers[$name] = $value; |
187 | 187 | } |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | private function getUploadedFilesArray(array $filesArray): array |
195 | 195 | { |
196 | 196 | $files = []; |
197 | - foreach ($filesArray as $class => $info) { |
|
197 | + foreach ($filesArray as $class => $info){ |
|
198 | 198 | $files[$class] = []; |
199 | 199 | $this->populateUploadedFileRecursive( |
200 | 200 | $files[$class], |
@@ -222,8 +222,8 @@ discard block |
||
222 | 222 | */ |
223 | 223 | private function populateUploadedFileRecursive(&$files, $names, $tempNames, $types, $sizes, $errors): void |
224 | 224 | { |
225 | - if (\is_array($names)) { |
|
226 | - foreach ($names as $i => $name) { |
|
225 | + if (\is_array($names)){ |
|
226 | + foreach ($names as $i => $name){ |
|
227 | 227 | $files[$i] = []; |
228 | 228 | $this->populateUploadedFileRecursive( |
229 | 229 | $files[$i], |
@@ -234,10 +234,10 @@ discard block |
||
234 | 234 | $errors[$i] |
235 | 235 | ); |
236 | 236 | } |
237 | - } else { |
|
238 | - try { |
|
237 | + }else{ |
|
238 | + try{ |
|
239 | 239 | $stream = $this->streamFactory->createStreamFromFile($tempNames); |
240 | - } catch (\RuntimeException $e) { |
|
240 | + }catch (\RuntimeException $e){ |
|
241 | 241 | $stream = $this->streamFactory->createStream(); |
242 | 242 | } |
243 | 243 |
@@ -108,12 +108,14 @@ discard block |
||
108 | 108 | $uri = $this->getUri($server); |
109 | 109 | |
110 | 110 | $request = $this->requestFactory->createServerRequest($method, $uri, $server); |
111 | - foreach ($headers as $name => $value) { |
|
111 | + foreach ($headers as $name => $value) |
|
112 | + { |
|
112 | 113 | $request = $request->withAddedHeader($name, $value); |
113 | 114 | } |
114 | 115 | |
115 | 116 | $protocol = '1.1'; |
116 | - if (!empty($_SERVER['SERVER_PROTOCOL'])) { |
|
117 | + if (!empty($_SERVER['SERVER_PROTOCOL'])) |
|
118 | + { |
|
117 | 119 | $protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); |
118 | 120 | } |
119 | 121 | |
@@ -124,15 +126,21 @@ discard block |
||
124 | 126 | ->withCookieParams($cookies) |
125 | 127 | ->withUploadedFiles($this->getUploadedFilesArray($files)); |
126 | 128 | |
127 | - if ($body === null) { |
|
129 | + if ($body === null) |
|
130 | + { |
|
128 | 131 | return $request; |
129 | 132 | } |
130 | 133 | |
131 | - if (\is_resource($body)) { |
|
134 | + if (\is_resource($body)) |
|
135 | + { |
|
132 | 136 | $body = $this->streamFactory->createStreamFromResource($body); |
133 | - } elseif (\is_string($body)) { |
|
137 | + } |
|
138 | + elseif (\is_string($body)) |
|
139 | + { |
|
134 | 140 | $body = $this->streamFactory->createStream($body); |
135 | - } elseif (!$body instanceof StreamInterface) { |
|
141 | + } |
|
142 | + elseif (!$body instanceof StreamInterface) |
|
143 | + { |
|
136 | 144 | throw new \InvalidArgumentException( |
137 | 145 | 'Body parameter for ServerRequestFactory::createFromParameters() ' |
138 | 146 | . 'must be instance of StreamInterface, resource or null.' |
@@ -145,29 +153,39 @@ discard block |
||
145 | 153 | private function getUri(array $server): UriInterface |
146 | 154 | { |
147 | 155 | $uri = $this->uriFactory->createUri(); |
148 | - if (isset($server['HTTPS'])) { |
|
156 | + if (isset($server['HTTPS'])) |
|
157 | + { |
|
149 | 158 | $uri = $uri->withScheme($server['HTTPS'] === 'on' ? 'https' : 'http'); |
150 | 159 | } |
151 | 160 | |
152 | - if (isset($server['HTTP_HOST'])) { |
|
153 | - if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) { |
|
161 | + if (isset($server['HTTP_HOST'])) |
|
162 | + { |
|
163 | + if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) |
|
164 | + { |
|
154 | 165 | $uri = $uri->withHost($matches[1])->withPort($matches[2]); |
155 | - } else { |
|
166 | + } |
|
167 | + else |
|
168 | + { |
|
156 | 169 | $uri = $uri->withHost($server['HTTP_HOST']); |
157 | 170 | } |
158 | - } elseif (isset($server['SERVER_NAME'])) { |
|
171 | + } |
|
172 | + elseif (isset($server['SERVER_NAME'])) |
|
173 | + { |
|
159 | 174 | $uri = $uri->withHost($server['SERVER_NAME']); |
160 | 175 | } |
161 | 176 | |
162 | - if (isset($server['SERVER_PORT'])) { |
|
177 | + if (isset($server['SERVER_PORT'])) |
|
178 | + { |
|
163 | 179 | $uri = $uri->withPort($server['SERVER_PORT']); |
164 | 180 | } |
165 | 181 | |
166 | - if (isset($server['REQUEST_URI'])) { |
|
182 | + if (isset($server['REQUEST_URI'])) |
|
183 | + { |
|
167 | 184 | $uri = $uri->withPath(\explode('?', $server['REQUEST_URI'])[0]); |
168 | 185 | } |
169 | 186 | |
170 | - if (isset($server['QUERY_STRING'])) { |
|
187 | + if (isset($server['QUERY_STRING'])) |
|
188 | + { |
|
171 | 189 | $uri = $uri->withQuery($server['QUERY_STRING']); |
172 | 190 | } |
173 | 191 | |
@@ -176,12 +194,17 @@ discard block |
||
176 | 194 | |
177 | 195 | private static function getHeadersFromGlobals(): array |
178 | 196 | { |
179 | - if (\function_exists('getallheaders')) { |
|
197 | + if (\function_exists('getallheaders')) |
|
198 | + { |
|
180 | 199 | $headers = getallheaders(); |
181 | - } else { |
|
200 | + } |
|
201 | + else |
|
202 | + { |
|
182 | 203 | $headers = []; |
183 | - foreach ($_SERVER as $name => $value) { |
|
184 | - if (strncmp($name, 'HTTP_', 5) === 0) { |
|
204 | + foreach ($_SERVER as $name => $value) |
|
205 | + { |
|
206 | + if (strncmp($name, 'HTTP_', 5) === 0) |
|
207 | + { |
|
185 | 208 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
186 | 209 | $headers[$name] = $value; |
187 | 210 | } |
@@ -194,7 +217,8 @@ discard block |
||
194 | 217 | private function getUploadedFilesArray(array $filesArray): array |
195 | 218 | { |
196 | 219 | $files = []; |
197 | - foreach ($filesArray as $class => $info) { |
|
220 | + foreach ($filesArray as $class => $info) |
|
221 | + { |
|
198 | 222 | $files[$class] = []; |
199 | 223 | $this->populateUploadedFileRecursive( |
200 | 224 | $files[$class], |
@@ -222,8 +246,10 @@ discard block |
||
222 | 246 | */ |
223 | 247 | private function populateUploadedFileRecursive(&$files, $names, $tempNames, $types, $sizes, $errors): void |
224 | 248 | { |
225 | - if (\is_array($names)) { |
|
226 | - foreach ($names as $i => $name) { |
|
249 | + if (\is_array($names)) |
|
250 | + { |
|
251 | + foreach ($names as $i => $name) |
|
252 | + { |
|
227 | 253 | $files[$i] = []; |
228 | 254 | $this->populateUploadedFileRecursive( |
229 | 255 | $files[$i], |
@@ -234,10 +260,15 @@ discard block |
||
234 | 260 | $errors[$i] |
235 | 261 | ); |
236 | 262 | } |
237 | - } else { |
|
238 | - try { |
|
263 | + } |
|
264 | + else |
|
265 | + { |
|
266 | + try |
|
267 | + { |
|
239 | 268 | $stream = $this->streamFactory->createStreamFromFile($tempNames); |
240 | - } catch (\RuntimeException $e) { |
|
269 | + } |
|
270 | + catch (\RuntimeException $e) |
|
271 | + { |
|
241 | 272 | $stream = $this->streamFactory->createStream(); |
242 | 273 | } |
243 | 274 |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function hasField(string $name): bool |
69 | 69 | { |
70 | - if (!array_key_exists($name, $this->fields)) { |
|
70 | + if (!array_key_exists($name, $this->fields)){ |
|
71 | 71 | return false; |
72 | 72 | } |
73 | 73 | |
@@ -83,14 +83,14 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public function setField(string $name, $value, bool $filter = true): void |
85 | 85 | { |
86 | - if ($value instanceof ValueInterface) { |
|
86 | + if ($value instanceof ValueInterface){ |
|
87 | 87 | //In case of non scalar values filters must be bypassed (check accessor compatibility?) |
88 | 88 | $this->fields[$name] = clone $value; |
89 | 89 | |
90 | 90 | return; |
91 | 91 | } |
92 | 92 | |
93 | - if (!$filter || (is_null($value) && $this->isNullable($name))) { |
|
93 | + if (!$filter || (is_null($value) && $this->isNullable($name))){ |
|
94 | 94 | //Bypassing all filters |
95 | 95 | $this->fields[$name] = $value; |
96 | 96 | |
@@ -100,10 +100,10 @@ discard block |
||
100 | 100 | //Checking if field have accessor |
101 | 101 | $accessor = $this->getMutator($name, ModelSchema::MUTATOR_ACCESSOR); |
102 | 102 | |
103 | - if ($accessor !== null) { |
|
103 | + if ($accessor !== null){ |
|
104 | 104 | //Setting value thought associated accessor |
105 | 105 | $this->thoughValue($accessor, $name, $value); |
106 | - } else { |
|
106 | + }else{ |
|
107 | 107 | //Setting value thought setter filter (if any) |
108 | 108 | $this->setMutated($name, $value); |
109 | 109 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | { |
121 | 121 | $value = $this->hasField($name) ? $this->fields[$name] : $default; |
122 | 122 | |
123 | - if ($value instanceof ValueInterface || (is_null($value) && $this->isNullable($name))) { |
|
123 | + if ($value instanceof ValueInterface || (is_null($value) && $this->isNullable($name))){ |
|
124 | 124 | //Direct access to value when value is accessor or null and declared as nullable |
125 | 125 | return $value; |
126 | 126 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | //Checking if field have accessor (decorator) |
129 | 129 | $accessor = $this->getMutator($name, ModelSchema::MUTATOR_ACCESSOR); |
130 | 130 | |
131 | - if (!empty($accessor)) { |
|
131 | + if (!empty($accessor)){ |
|
132 | 132 | return $this->fields[$name] = $this->createValue($accessor, $name, $value); |
133 | 133 | } |
134 | 134 | |
@@ -148,15 +148,15 @@ discard block |
||
148 | 148 | */ |
149 | 149 | public function setFields(iterable $fields = [], bool $all = false) |
150 | 150 | { |
151 | - if (!\is_array($fields) && !$fields instanceof \Traversable) { |
|
151 | + if (!\is_array($fields) && !$fields instanceof \Traversable){ |
|
152 | 152 | return $this; |
153 | 153 | } |
154 | 154 | |
155 | - foreach ($fields as $name => $value) { |
|
156 | - if ($all || $this->isFillable($name)) { |
|
157 | - try { |
|
155 | + foreach ($fields as $name => $value){ |
|
156 | + if ($all || $this->isFillable($name)){ |
|
157 | + try{ |
|
158 | 158 | $this->setField($name, $value, true); |
159 | - } catch (AccessExceptionInterface $e) { |
|
159 | + }catch (AccessExceptionInterface $e){ |
|
160 | 160 | // We are suppressing field setting exceptions |
161 | 161 | } |
162 | 162 | } |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | public function getFields(bool $filter = true): array |
178 | 178 | { |
179 | 179 | $result = []; |
180 | - foreach ($this->fields as $name => $_) { |
|
180 | + foreach ($this->fields as $name => $_){ |
|
181 | 181 | $result[$name] = $this->getField($name, null, $filter); |
182 | 182 | } |
183 | 183 | |
@@ -242,10 +242,10 @@ discard block |
||
242 | 242 | public function getValue(): array |
243 | 243 | { |
244 | 244 | $result = []; |
245 | - foreach ($this->fields as $field => $value) { |
|
246 | - if ($value instanceof ValueInterface) { |
|
245 | + foreach ($this->fields as $field => $value){ |
|
246 | + if ($value instanceof ValueInterface){ |
|
247 | 247 | $result[$field] = $value->getValue(); |
248 | - } else { |
|
248 | + }else{ |
|
249 | 249 | $result[$field] = $value; |
250 | 250 | } |
251 | 251 | } |
@@ -327,9 +327,9 @@ discard block |
||
327 | 327 | $value, |
328 | 328 | array $context = [] |
329 | 329 | ): ValueInterface { |
330 | - if (!is_string($type) || !class_exists($type)) { |
|
330 | + if (!is_string($type) || !class_exists($type)){ |
|
331 | 331 | throw new EntityException( |
332 | - "Unable to create accessor for field `{$name}` in " . static::class |
|
332 | + "Unable to create accessor for field `{$name}` in ".static::class |
|
333 | 333 | ); |
334 | 334 | } |
335 | 335 | |
@@ -347,10 +347,10 @@ discard block |
||
347 | 347 | { |
348 | 348 | $getter = $this->getMutator($name, ModelSchema::MUTATOR_GETTER); |
349 | 349 | |
350 | - if ($filter && !empty($getter)) { |
|
351 | - try { |
|
350 | + if ($filter && !empty($getter)){ |
|
351 | + try{ |
|
352 | 352 | return call_user_func($getter, $value); |
353 | - } catch (\Exception $e) { |
|
353 | + }catch (\Exception $e){ |
|
354 | 354 | //Trying to filter null value, every filter must support it |
355 | 355 | return call_user_func($getter, null); |
356 | 356 | } |
@@ -368,13 +368,13 @@ discard block |
||
368 | 368 | { |
369 | 369 | $setter = $this->getMutator($name, ModelSchema::MUTATOR_SETTER); |
370 | 370 | |
371 | - if (!empty($setter)) { |
|
372 | - try { |
|
371 | + if (!empty($setter)){ |
|
372 | + try{ |
|
373 | 373 | $this->fields[$name] = call_user_func($setter, $value); |
374 | - } catch (\Exception $e) { |
|
374 | + }catch (\Exception $e){ |
|
375 | 375 | //Exceptional situation, we are choosing to keep original field value |
376 | 376 | } |
377 | - } else { |
|
377 | + }else{ |
|
378 | 378 | $this->fields[$name] = $value; |
379 | 379 | } |
380 | 380 | } |
@@ -387,13 +387,13 @@ discard block |
||
387 | 387 | */ |
388 | 388 | private function thoughValue($type, string $name, $value): void |
389 | 389 | { |
390 | - if (array_key_exists($name, $this->fields)) { |
|
390 | + if (array_key_exists($name, $this->fields)){ |
|
391 | 391 | $field = $this->fields[$name]; |
392 | - } else { |
|
392 | + }else{ |
|
393 | 393 | $field = null; |
394 | 394 | } |
395 | 395 | |
396 | - if (empty($field) || !($field instanceof ValueInterface)) { |
|
396 | + if (empty($field) || !($field instanceof ValueInterface)){ |
|
397 | 397 | //New field representation |
398 | 398 | $field = $this->createValue($type, $name, $value); |
399 | 399 |
@@ -67,7 +67,8 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function hasField(string $name): bool |
69 | 69 | { |
70 | - if (!array_key_exists($name, $this->fields)) { |
|
70 | + if (!array_key_exists($name, $this->fields)) |
|
71 | + { |
|
71 | 72 | return false; |
72 | 73 | } |
73 | 74 | |
@@ -83,14 +84,16 @@ discard block |
||
83 | 84 | */ |
84 | 85 | public function setField(string $name, $value, bool $filter = true): void |
85 | 86 | { |
86 | - if ($value instanceof ValueInterface) { |
|
87 | + if ($value instanceof ValueInterface) |
|
88 | + { |
|
87 | 89 | //In case of non scalar values filters must be bypassed (check accessor compatibility?) |
88 | 90 | $this->fields[$name] = clone $value; |
89 | 91 | |
90 | 92 | return; |
91 | 93 | } |
92 | 94 | |
93 | - if (!$filter || (is_null($value) && $this->isNullable($name))) { |
|
95 | + if (!$filter || (is_null($value) && $this->isNullable($name))) |
|
96 | + { |
|
94 | 97 | //Bypassing all filters |
95 | 98 | $this->fields[$name] = $value; |
96 | 99 | |
@@ -100,10 +103,13 @@ discard block |
||
100 | 103 | //Checking if field have accessor |
101 | 104 | $accessor = $this->getMutator($name, ModelSchema::MUTATOR_ACCESSOR); |
102 | 105 | |
103 | - if ($accessor !== null) { |
|
106 | + if ($accessor !== null) |
|
107 | + { |
|
104 | 108 | //Setting value thought associated accessor |
105 | 109 | $this->thoughValue($accessor, $name, $value); |
106 | - } else { |
|
110 | + } |
|
111 | + else |
|
112 | + { |
|
107 | 113 | //Setting value thought setter filter (if any) |
108 | 114 | $this->setMutated($name, $value); |
109 | 115 | } |
@@ -120,7 +126,8 @@ discard block |
||
120 | 126 | { |
121 | 127 | $value = $this->hasField($name) ? $this->fields[$name] : $default; |
122 | 128 | |
123 | - if ($value instanceof ValueInterface || (is_null($value) && $this->isNullable($name))) { |
|
129 | + if ($value instanceof ValueInterface || (is_null($value) && $this->isNullable($name))) |
|
130 | + { |
|
124 | 131 | //Direct access to value when value is accessor or null and declared as nullable |
125 | 132 | return $value; |
126 | 133 | } |
@@ -128,7 +135,8 @@ discard block |
||
128 | 135 | //Checking if field have accessor (decorator) |
129 | 136 | $accessor = $this->getMutator($name, ModelSchema::MUTATOR_ACCESSOR); |
130 | 137 | |
131 | - if (!empty($accessor)) { |
|
138 | + if (!empty($accessor)) |
|
139 | + { |
|
132 | 140 | return $this->fields[$name] = $this->createValue($accessor, $name, $value); |
133 | 141 | } |
134 | 142 | |
@@ -148,15 +156,21 @@ discard block |
||
148 | 156 | */ |
149 | 157 | public function setFields(iterable $fields = [], bool $all = false) |
150 | 158 | { |
151 | - if (!\is_array($fields) && !$fields instanceof \Traversable) { |
|
159 | + if (!\is_array($fields) && !$fields instanceof \Traversable) |
|
160 | + { |
|
152 | 161 | return $this; |
153 | 162 | } |
154 | 163 | |
155 | - foreach ($fields as $name => $value) { |
|
156 | - if ($all || $this->isFillable($name)) { |
|
157 | - try { |
|
164 | + foreach ($fields as $name => $value) |
|
165 | + { |
|
166 | + if ($all || $this->isFillable($name)) |
|
167 | + { |
|
168 | + try |
|
169 | + { |
|
158 | 170 | $this->setField($name, $value, true); |
159 | - } catch (AccessExceptionInterface $e) { |
|
171 | + } |
|
172 | + catch (AccessExceptionInterface $e) |
|
173 | + { |
|
160 | 174 | // We are suppressing field setting exceptions |
161 | 175 | } |
162 | 176 | } |
@@ -177,7 +191,8 @@ discard block |
||
177 | 191 | public function getFields(bool $filter = true): array |
178 | 192 | { |
179 | 193 | $result = []; |
180 | - foreach ($this->fields as $name => $_) { |
|
194 | + foreach ($this->fields as $name => $_) |
|
195 | + { |
|
181 | 196 | $result[$name] = $this->getField($name, null, $filter); |
182 | 197 | } |
183 | 198 | |
@@ -242,10 +257,14 @@ discard block |
||
242 | 257 | public function getValue(): array |
243 | 258 | { |
244 | 259 | $result = []; |
245 | - foreach ($this->fields as $field => $value) { |
|
246 | - if ($value instanceof ValueInterface) { |
|
260 | + foreach ($this->fields as $field => $value) |
|
261 | + { |
|
262 | + if ($value instanceof ValueInterface) |
|
263 | + { |
|
247 | 264 | $result[$field] = $value->getValue(); |
248 | - } else { |
|
265 | + } |
|
266 | + else |
|
267 | + { |
|
249 | 268 | $result[$field] = $value; |
250 | 269 | } |
251 | 270 | } |
@@ -327,7 +346,8 @@ discard block |
||
327 | 346 | $value, |
328 | 347 | array $context = [] |
329 | 348 | ): ValueInterface { |
330 | - if (!is_string($type) || !class_exists($type)) { |
|
349 | + if (!is_string($type) || !class_exists($type)) |
|
350 | + { |
|
331 | 351 | throw new EntityException( |
332 | 352 | "Unable to create accessor for field `{$name}` in " . static::class |
333 | 353 | ); |
@@ -347,10 +367,14 @@ discard block |
||
347 | 367 | { |
348 | 368 | $getter = $this->getMutator($name, ModelSchema::MUTATOR_GETTER); |
349 | 369 | |
350 | - if ($filter && !empty($getter)) { |
|
351 | - try { |
|
370 | + if ($filter && !empty($getter)) |
|
371 | + { |
|
372 | + try |
|
373 | + { |
|
352 | 374 | return call_user_func($getter, $value); |
353 | - } catch (\Exception $e) { |
|
375 | + } |
|
376 | + catch (\Exception $e) |
|
377 | + { |
|
354 | 378 | //Trying to filter null value, every filter must support it |
355 | 379 | return call_user_func($getter, null); |
356 | 380 | } |
@@ -368,13 +392,19 @@ discard block |
||
368 | 392 | { |
369 | 393 | $setter = $this->getMutator($name, ModelSchema::MUTATOR_SETTER); |
370 | 394 | |
371 | - if (!empty($setter)) { |
|
372 | - try { |
|
395 | + if (!empty($setter)) |
|
396 | + { |
|
397 | + try |
|
398 | + { |
|
373 | 399 | $this->fields[$name] = call_user_func($setter, $value); |
374 | - } catch (\Exception $e) { |
|
400 | + } |
|
401 | + catch (\Exception $e) |
|
402 | + { |
|
375 | 403 | //Exceptional situation, we are choosing to keep original field value |
376 | 404 | } |
377 | - } else { |
|
405 | + } |
|
406 | + else |
|
407 | + { |
|
378 | 408 | $this->fields[$name] = $value; |
379 | 409 | } |
380 | 410 | } |
@@ -387,13 +417,17 @@ discard block |
||
387 | 417 | */ |
388 | 418 | private function thoughValue($type, string $name, $value): void |
389 | 419 | { |
390 | - if (array_key_exists($name, $this->fields)) { |
|
420 | + if (array_key_exists($name, $this->fields)) |
|
421 | + { |
|
391 | 422 | $field = $this->fields[$name]; |
392 | - } else { |
|
423 | + } |
|
424 | + else |
|
425 | + { |
|
393 | 426 | $field = null; |
394 | 427 | } |
395 | 428 | |
396 | - if (empty($field) || !($field instanceof ValueInterface)) { |
|
429 | + if (empty($field) || !($field instanceof ValueInterface)) |
|
430 | + { |
|
397 | 431 | //New field representation |
398 | 432 | $field = $this->createValue($type, $name, $value); |
399 | 433 |