1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Hautelook\AliceBundle\Alice\Generator\Instantiator\Chainable; |
15
|
|
|
|
16
|
|
|
use Nelmio\Alice\Definition\MethodCall\MethodCallWithReference; |
17
|
|
|
use Nelmio\Alice\Definition\Object\SimpleObject; |
18
|
|
|
use Nelmio\Alice\Definition\ServiceReference\InstantiatedReference; |
19
|
|
|
use Nelmio\Alice\FixtureInterface; |
20
|
|
|
use Nelmio\Alice\Generator\GenerationContext; |
21
|
|
|
use Nelmio\Alice\Generator\Instantiator\ChainableInstantiatorInterface; |
22
|
|
|
use Nelmio\Alice\Generator\ResolvedFixtureSet; |
23
|
|
|
use Nelmio\Alice\IsAServiceTrait; |
24
|
|
|
use Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
27
|
|
|
|
28
|
|
|
final class InstantiatedReferenceInstantiator implements ChainableInstantiatorInterface, ContainerAwareInterface |
29
|
|
|
{ |
30
|
|
|
use IsAServiceTrait; |
31
|
|
|
|
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function setContainer(ContainerInterface $container = null) |
38
|
|
|
{ |
39
|
|
|
$this->container = $container; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function canInstantiate(FixtureInterface $fixture): bool |
46
|
|
|
{ |
47
|
|
|
$constructor = $fixture->getSpecs()->getConstructor(); |
48
|
|
|
|
49
|
|
|
return ( |
50
|
|
|
null !== $constructor |
51
|
|
|
&& $constructor instanceof MethodCallWithReference |
52
|
|
|
&& $constructor->getCaller() instanceof InstantiatedReference |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function instantiate( |
60
|
|
|
FixtureInterface $fixture, |
61
|
|
|
ResolvedFixtureSet $fixtureSet, |
62
|
|
|
GenerationContext $context |
63
|
|
|
): ResolvedFixtureSet |
64
|
|
|
{ |
65
|
|
|
$this->checkContainer(__METHOD__); |
66
|
|
|
$instance = $this->createInstance($fixture); |
67
|
|
|
|
68
|
|
|
return $this->generateSet($fixture, $fixtureSet, $instance); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function checkContainer(string $method) |
72
|
|
|
{ |
73
|
|
|
if (null === $this->container) { |
74
|
|
|
throw new \LogicException( |
75
|
|
|
sprintf( |
76
|
|
|
'Expected instantiator method "%s" to be used only if it has a container, but no container could' |
77
|
|
|
.' be found.', |
78
|
|
|
$method |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function createInstance(FixtureInterface $fixture) |
85
|
|
|
{ |
86
|
|
|
$constructor = $fixture->getSpecs()->getConstructor(); |
87
|
|
|
list($class, $factoryReference, $method, $arguments) = [ |
88
|
|
|
$fixture->getClassName(), |
89
|
|
|
$constructor->getCaller()->getId(), |
90
|
|
|
$constructor->getMethod(), |
91
|
|
|
$constructor->getArguments() |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if (null === $arguments) { |
95
|
|
|
$arguments = []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$factory = $this->container->get($factoryReference); |
99
|
|
|
|
100
|
|
|
$instance = $factory->$method(...$arguments); |
101
|
|
|
if (false === $instance instanceof $class) { |
102
|
|
|
throw new InstantiationException( |
103
|
|
|
sprintf( |
104
|
|
|
'Instantiated fixture was expected to be an instance of "%s". Got "%s" instead.', |
105
|
|
|
$class, |
106
|
|
|
get_class($instance) |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $instance; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function generateSet( |
115
|
|
|
FixtureInterface $fixture, |
116
|
|
|
ResolvedFixtureSet $fixtureSet, |
117
|
|
|
$instance |
118
|
|
|
): ResolvedFixtureSet |
119
|
|
|
{ |
120
|
|
|
$objects = $fixtureSet->getObjects()->with( |
121
|
|
|
new SimpleObject( |
122
|
|
|
$fixture->getId(), |
123
|
|
|
$instance |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
return $fixtureSet->withObjects($objects); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|