1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context as ContextInterface; |
6
|
|
|
use Knp\FriendlyContexts\Dictionary\Backgroundable; |
7
|
|
|
use Knp\FriendlyContexts\Dictionary\Taggable; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
11
|
|
|
|
12
|
|
|
abstract class Context implements ContextInterface |
13
|
|
|
{ |
14
|
|
|
use Backgroundable, |
15
|
|
|
Taggable; |
16
|
|
|
|
17
|
|
|
protected $config = []; |
18
|
|
|
protected $container; |
19
|
|
|
|
20
|
|
|
public function initialize(array $config, ContainerInterface $container) |
21
|
|
|
{ |
22
|
|
|
$this->config = array_merge($this->getDefaultOptions(), $config); |
23
|
|
|
$this->container = $container; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function getRecordBag() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
return $this->get('friendly.record.bag'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getEntityHydrator() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return $this->get('friendly.entity.hydrator'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getEntityResolver() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
return $this->get('friendly.entity.resolver'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getTextFormater() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
return $this->get('friendly.text.formater'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getAsserter() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return $this->get('friendly.asserter'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getGuesserManager() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return $this->get('friendly.guesser.manager'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getObjectReflector() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return $this->get('friendly.object.reflector'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getFeatureWalker() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->get('friendly.feature.walker'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getAliceLoader() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return $this->get('friendly.alice.fixtures.loader'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getEntityManager() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return $this->get('doctrine')->getManager(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getUniqueCache() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return $this->get('friendly.unique_cache'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getPageClassResolver() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return $this->get('friendly.page.resolver'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getRequestBuilder() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return $this->get('friendly.builder.request_builder'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getHttpContentTypeGuesser() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return $this->get('friendly.http.http_content_type_guesser'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
protected function get($service) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ($this->container->has($service)) { |
99
|
|
|
return $this->container->get($service); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) { |
103
|
|
|
return $this->getKernel()->getContainer()->get($service); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new ServiceNotFoundException($service); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
protected function getParameter($name) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($this->container->hasParameter($name)) { |
112
|
|
|
return $this->container->getParameter($name); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) { |
116
|
|
|
return $this->getKernel()->getContainer()->getParameter($name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
throw new ParameterNotFoundException($name); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function getKernel() |
123
|
|
|
{ |
124
|
|
|
if ($this->container->has('friendly.symfony.kernel')) { |
125
|
|
|
$kernel = $this->container->get('friendly.symfony.kernel'); |
126
|
|
|
$kernel->boot(); |
127
|
|
|
|
128
|
|
|
return $kernel; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function resolveEntity($name) |
133
|
|
|
{ |
134
|
|
|
$namespaces = $this->getParameter('friendly.entities.namespaces'); |
135
|
|
|
|
136
|
|
|
$entities = $this |
137
|
|
|
->getEntityResolver() |
138
|
|
|
->resolve( |
139
|
|
|
$this->getEntityManager(), |
140
|
|
|
$name, |
141
|
|
|
empty($namespaces) ? '' : $namespaces |
142
|
|
|
) |
143
|
|
|
; |
144
|
|
|
|
145
|
|
|
switch (true) { |
146
|
|
|
case 1 < count($entities): |
147
|
|
|
throw new \Exception( |
148
|
|
|
sprintf( |
149
|
|
|
'Failed to find a unique model from the name "%s", "%s" found', |
150
|
|
|
$name, |
151
|
|
|
implode('" and "', array_map( |
152
|
|
|
function ($rfl) { |
153
|
|
|
return $rfl->getName(); |
154
|
|
|
}, |
155
|
|
|
$entities |
156
|
|
|
)) |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
break; |
|
|
|
|
160
|
|
|
case 0 === count($entities): |
161
|
|
|
throw new \Exception( |
162
|
|
|
sprintf( |
163
|
|
|
'Failed to find a model from the name "%s"', |
164
|
|
|
$name |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return current($entities); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function getDefaultOptions() |
173
|
|
|
{ |
174
|
|
|
return []; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.