2
2
3
3
namespace Matthias \SymfonyServiceDefinitionValidator ;
4
4
5
+ use Matthias \SymfonyServiceDefinitionValidator \Exception \ClassNotFoundException ;
5
6
use Matthias \SymfonyServiceDefinitionValidator \Exception \MethodNotFoundException ;
6
7
use Symfony \Component \DependencyInjection \ContainerBuilder ;
7
8
use Symfony \Component \DependencyInjection \Definition ;
8
- use Matthias \SymfonyServiceDefinitionValidator \Exception \ClassNotFoundException ;
9
9
10
10
class ConstructorResolver implements ConstructorResolverInterface
11
11
{
@@ -24,39 +24,58 @@ public function __construct(
24
24
public function resolve (Definition $ definition )
25
25
{
26
26
if ($ definition ->getFactoryClass () && $ definition ->getFactoryMethod ()) {
27
- $ factoryClass = $ this ->resolvePlaceholders ($ definition ->getFactoryClass ());
28
- if (!class_exists ($ factoryClass )) {
29
- throw new ClassNotFoundException ($ factoryClass );
30
- }
27
+ return $ this ->resolveFactoryClassWithMethod (
28
+ $ definition ->getFactoryClass (),
29
+ $ definition ->getFactoryMethod ()
30
+ );
31
+ } elseif ($ definition ->getFactoryService () && $ definition ->getFactoryMethod ()) {
32
+ return $ this ->resolveFactoryServiceWithMethod (
33
+ $ definition ->getFactoryService (),
34
+ $ definition ->getFactoryMethod ()
35
+ );
36
+ } elseif ($ definition ->getClass ()) {
37
+ return $ this ->resolveClassWithConstructor ($ definition ->getClass ());
38
+ }
31
39
32
- $ factoryMethod = $ this ->resolvePlaceholders ($ definition ->getFactoryMethod ());
33
- if (!method_exists ($ factoryClass , $ factoryMethod )) {
34
- throw new MethodNotFoundException ($ factoryClass , $ factoryMethod );
35
- }
40
+ return null ;
41
+ }
36
42
37
- return new \ReflectionMethod ($ factoryClass , $ factoryMethod );
38
- } elseif ($ definition ->getFactoryService () && $ definition ->getFactoryMethod ()) {
39
- $ factoryServiceId = $ this ->resolvePlaceholders ($ definition ->getFactoryService ());
40
- $ factoryDefinition = $ this ->containerBuilder ->findDefinition ($ factoryServiceId );
43
+ private function resolveFactoryClassWithMethod ($ factoryClass , $ factoryMethod )
44
+ {
45
+ if (!class_exists ($ factoryClass )) {
46
+ throw new ClassNotFoundException ($ factoryClass );
47
+ }
41
48
42
- $ factoryClass = $ this ->resultingClassResolver ->resolve ($ factoryDefinition );
49
+ if (!method_exists ($ factoryClass , $ factoryMethod )) {
50
+ throw new MethodNotFoundException ($ factoryClass , $ factoryMethod );
51
+ }
43
52
44
- $ factoryMethod = $ this ->resolvePlaceholders ($ definition ->getFactoryMethod ());
45
- if (!method_exists ($ factoryClass , $ factoryMethod )) {
46
- throw new MethodNotFoundException ($ factoryClass , $ factoryMethod );
47
- }
53
+ return new \ReflectionMethod ($ factoryClass , $ factoryMethod );
54
+ }
55
+
56
+ private function resolveFactoryServiceWithMethod ($ factoryServiceId , $ factoryMethod )
57
+ {
58
+ $ factoryDefinition = $ this ->containerBuilder ->findDefinition ($ factoryServiceId );
59
+
60
+ $ factoryClass = $ this ->resultingClassResolver ->resolve ($ factoryDefinition );
48
61
49
- return new \ReflectionMethod ($ factoryClass , $ factoryMethod );
50
- } else {
51
- $ class = $ this ->resolvePlaceholders ($ definition ->getClass ());
62
+ if (!method_exists ($ factoryClass , $ factoryMethod )) {
63
+ throw new MethodNotFoundException ($ factoryClass , $ factoryMethod );
64
+ }
65
+
66
+ return new \ReflectionMethod ($ factoryClass , $ factoryMethod );
67
+ }
68
+
69
+ private function resolveClassWithConstructor ($ class )
70
+ {
71
+ $ class = $ this ->resolvePlaceholders ($ class );
52
72
53
- $ reflectionClass = new \ReflectionClass ($ class );
73
+ $ reflectionClass = new \ReflectionClass ($ class );
54
74
55
- if ($ reflectionClass ->hasMethod ('__construct ' )) {
56
- $ constructMethod = $ reflectionClass ->getMethod ('__construct ' );
57
- if ($ constructMethod ->isPublic ()) {
58
- return $ constructMethod ;
59
- }
75
+ if ($ reflectionClass ->hasMethod ('__construct ' )) {
76
+ $ constructMethod = $ reflectionClass ->getMethod ('__construct ' );
77
+ if ($ constructMethod ->isPublic ()) {
78
+ return $ constructMethod ;
60
79
}
61
80
}
62
81
0 commit comments