Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

AnnotationArgumentResolver.php
Go to the documentation of this file.
1 <?php
2
7 namespace EzSystems\PlatformBehatBundle\Context\Argument;
8
9 use Behat\Behat\Context\Argument\ArgumentResolver;
10 use ReflectionClass;
11
15 class AnnotationArgumentResolver implements ArgumentResolver
16 {
20 const SERVICE_DOC_TAG = 'injectService';
21
29 public function resolveArguments(ReflectionClass $classReflection, array $arguments = [])
30 {
31 $injArguments = $this->parseAnnotations(
32 $this->getMethodAnnotations($classReflection)
33 );
34
35 if (!empty($injArguments)) {
36 $arguments = [];
37 foreach ($injArguments as $name => $service) {
38 $arguments[$name] = $service;
39 }
40 }
41
42 return $arguments;
43 }
44
50 private function getMethodAnnotations($refClass, $method = '__construct')
51 {
52 if ($refClass->hasMethod($method)) {
53 $refMethod = $refClass->getMethod($method);
54 preg_match_all('#@(.*?)\n#s', $refMethod->getDocComment(), $matches);
55
56 return $matches[1];
57 } else {
58 return [];
59 }
60 }
61
68 private function parseAnnotations($annotations)
69 {
70 // parse array from (numeric key => 'annotation <value>') to (annotation => value)
71 $methodServices = [];
72 foreach ($annotations as $annotation) {
73 if (!preg_match('/^(\w+)\s+\$(\w+)\s+([\w\.\\\@\%]+)/', $annotation, $matches)) {
74 continue;
75 }
76
77 array_shift($matches);
78 $tag = array_shift($matches);
79 if ($tag == self::SERVICE_DOC_TAG) {
80 list($argument, $service) = $matches;
81 $methodServices[$argument] = $service;
82 }
83 }
84
85 return $methodServices;
86 }
87 }
EzSystems\PlatformBehatBundle\Context\Argument\AnnotationArgumentResolver
Behat Context Argument Resolver.
Definition: AnnotationArgumentResolver.php:16
EzSystems\PlatformBehatBundle\Context\Argument\AnnotationArgumentResolver\resolveArguments
resolveArguments(ReflectionClass $classReflection, array $arguments=[])
Resolve service arguments for Behat Context constructor thru annotation.
Definition: AnnotationArgumentResolver.php:29
EzSystems\PlatformBehatBundle\Context\Argument\AnnotationArgumentResolver\getMethodAnnotations
getMethodAnnotations($refClass, $method='__construct')
Returns a array with the method annotations.
Definition: AnnotationArgumentResolver.php:50
EzSystems\PlatformBehatBundle\Context\Argument\AnnotationArgumentResolver\parseAnnotations
parseAnnotations($annotations)
Returns an array with the method arguments service requirements, if the methods use the service Annot...
Definition: AnnotationArgumentResolver.php:68
EzSystems\PlatformBehatBundle\Context\Argument\AnnotationArgumentResolver\SERVICE_DOC_TAG
const SERVICE_DOC_TAG
Service annotation tag.
Definition: AnnotationArgumentResolver.php:20
EzSystems\PlatformBehatBundle\Context\Argument
Definition: AnnotationArgumentResolver.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.