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\Exception\Generator\Instantiator\InstantiationException; |
20
|
|
|
use Nelmio\Alice\FixtureInterface; |
21
|
|
|
use Nelmio\Alice\Generator\GenerationContext; |
22
|
|
|
use Nelmio\Alice\Generator\Instantiator\ChainableInstantiatorInterface; |
23
|
|
|
use Nelmio\Alice\Generator\ResolvedFixtureSet; |
24
|
|
|
use Nelmio\Alice\NotClonableTrait; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Théo FIDRY <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class InstantiatedReferenceInstantiator implements ChainableInstantiatorInterface, ContainerAwareInterface |
32
|
|
|
{ |
33
|
|
|
use NotClonableTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ContainerInterface|null |
37
|
|
|
*/ |
38
|
|
|
private $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function setContainer(ContainerInterface $container = null) |
44
|
|
|
{ |
45
|
|
|
$this->container = $container; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function canInstantiate(FixtureInterface $fixture): bool |
52
|
|
|
{ |
53
|
|
|
$constructor = $fixture->getSpecs()->getConstructor(); |
54
|
|
|
|
55
|
|
|
return ( |
56
|
|
|
null !== $constructor |
57
|
|
|
&& $constructor instanceof MethodCallWithReference |
58
|
|
|
&& $constructor->getCaller() instanceof InstantiatedReference |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function instantiate( |
66
|
|
|
FixtureInterface $fixture, |
67
|
|
|
ResolvedFixtureSet $fixtureSet, |
68
|
|
|
GenerationContext $context |
69
|
|
|
): ResolvedFixtureSet |
70
|
|
|
{ |
71
|
|
|
$this->checkContainer(__METHOD__); |
72
|
|
|
$instance = $this->createInstance($fixture); |
73
|
|
|
|
74
|
|
|
return $this->generateSet($fixture, $fixtureSet, $instance); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function checkContainer(string $method) |
78
|
|
|
{ |
79
|
|
|
if (null === $this->container) { |
80
|
|
|
throw new \LogicException( |
81
|
|
|
sprintf( |
82
|
|
|
'Expected instantiator method "%s" to be used only if it has a container, but no container could' |
83
|
|
|
.' be found.', |
84
|
|
|
$method |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function createInstance(FixtureInterface $fixture) |
91
|
|
|
{ |
92
|
|
|
$constructor = $fixture->getSpecs()->getConstructor(); |
93
|
|
|
list($class, $factoryReference, $method, $arguments) = [ |
94
|
|
|
$fixture->getClassName(), |
95
|
|
|
$constructor->getCaller()->getId(), |
96
|
|
|
$constructor->getMethod(), |
97
|
|
|
$constructor->getArguments() |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if (null === $arguments) { |
101
|
|
|
$arguments = []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$factory = $this->container->get($factoryReference); |
105
|
|
|
|
106
|
|
|
$instance = $factory->$method(...$arguments); |
107
|
|
|
if (false === $instance instanceof $class) { |
108
|
|
|
throw new InstantiationException( |
109
|
|
|
sprintf( |
110
|
|
|
'Instantiated fixture was expected to be an instance of "%s". Got "%s" instead.', |
111
|
|
|
$class, |
112
|
|
|
get_class($instance) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $instance; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function generateSet( |
121
|
|
|
FixtureInterface $fixture, |
122
|
|
|
ResolvedFixtureSet $fixtureSet, |
123
|
|
|
$instance |
124
|
|
|
): ResolvedFixtureSet |
125
|
|
|
{ |
126
|
|
|
$objects = $fixtureSet->getObjects()->with( |
127
|
|
|
new SimpleObject( |
128
|
|
|
$fixture->getId(), |
129
|
|
|
$instance |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $fixtureSet->withObjects($objects); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|