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