1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hautelook\AliceBundle\Loader; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Fidry\AliceDataFixtures\Bridge\Doctrine\Persister\ObjectManagerPersister; |
8
|
|
|
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\OrmPurger; |
9
|
|
|
use Fidry\AliceDataFixtures\Loader\FileResolverLoader; |
10
|
|
|
use Fidry\AliceDataFixtures\Loader\PurgerLoader; |
11
|
|
|
use Fidry\AliceDataFixtures\LoaderInterface; |
12
|
|
|
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface; |
13
|
|
|
use Fidry\AliceDataFixtures\Persistence\PurgeMode; |
14
|
|
|
use Hautelook\AliceBundle\BundleResolverInterface; |
15
|
|
|
use Hautelook\AliceBundle\FixtureLocatorInterface; |
16
|
|
|
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface; |
17
|
|
|
use Hautelook\AliceBundle\LoggerAwareInterface; |
18
|
|
|
use Hautelook\AliceBundle\Resolver\File\KernelFileResolver; |
19
|
|
|
use Nelmio\Alice\NotClonableTrait; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
22
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
23
|
|
|
|
24
|
|
|
final class DoctrineOrmLoader implements AliceBundleLoaderInterface, LoggerAwareInterface |
25
|
|
|
{ |
26
|
|
|
use NotClonableTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BundleResolverInterface |
30
|
|
|
*/ |
31
|
|
|
private $bundleResolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var FixtureLocatorInterface |
35
|
|
|
*/ |
36
|
|
|
private $fixtureLocator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoaderInterface|PersisterAwareInterface |
40
|
|
|
*/ |
41
|
|
|
private $loader; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var LoggerInterface |
45
|
|
|
*/ |
46
|
|
|
private $logger; |
47
|
|
|
|
48
|
|
|
public function __construct( |
49
|
|
|
BundleResolverInterface $bundleResolver, |
50
|
|
|
FixtureLocatorInterface $fixtureLocator, |
51
|
|
|
LoaderInterface $loader, |
52
|
|
|
LoggerInterface $logger |
53
|
|
|
) { |
54
|
|
|
$this->bundleResolver = $bundleResolver; |
55
|
|
|
$this->fixtureLocator = $fixtureLocator; |
56
|
|
|
if (false === $loader instanceof PersisterAwareInterface) { |
57
|
|
|
throw new \InvalidArgumentException( |
58
|
|
|
sprintf( |
59
|
|
|
'Expected loader to be an instance of "%s".', |
60
|
|
|
PersisterAwareInterface::class |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
$this->loader = $loader; |
65
|
|
|
$this->logger = $logger; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function withLogger(LoggerInterface $logger): self |
72
|
|
|
{ |
73
|
|
|
return new self($this->bundleResolver, $this->fixtureLocator, $this->loader, $logger); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function load( |
80
|
|
|
Application $application, |
81
|
|
|
EntityManagerInterface $manager, |
82
|
|
|
array $bundles, |
83
|
|
|
string $environment, |
84
|
|
|
bool $append, |
85
|
|
|
bool $purgeWithTruncate, |
86
|
|
|
string $shard = null |
87
|
|
|
) { |
88
|
|
|
$bundles = $this->bundleResolver->resolveBundles($application, $bundles); |
89
|
|
|
$fixtureFiles = $this->fixtureLocator->locateFiles($bundles, $environment); |
90
|
|
|
|
91
|
|
|
$this->logger->info('fixtures found', ['files' => $fixtureFiles]); |
92
|
|
|
|
93
|
|
|
if (null !== $shard) { |
94
|
|
|
$this->connectToShardConnection($manager, $shard); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$fixtures = $this->loadFixtures( |
98
|
|
|
$this->loader, |
|
|
|
|
99
|
|
|
$application->getKernel(), |
100
|
|
|
$manager, |
101
|
|
|
$fixtureFiles, |
102
|
|
|
$application->getKernel()->getContainer()->getParameterBag()->all(), |
|
|
|
|
103
|
|
|
$append, |
104
|
|
|
$purgeWithTruncate |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->logger->info('fixtures loaded'); |
108
|
|
|
|
109
|
|
|
return $fixtures; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function connectToShardConnection(EntityManagerInterface $manager, string $shard) |
113
|
|
|
{ |
114
|
|
|
$connection = $manager->getConnection(); |
115
|
|
|
if ($connection instanceof PoolingShardConnection) { |
116
|
|
|
$connection->connect($shard); |
117
|
|
|
|
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
throw new \InvalidArgumentException( |
122
|
|
|
sprintf( |
123
|
|
|
'Could not establish a shard connection for the shard "%s". The connection must be an instance' |
124
|
|
|
.' of "%s", got "%s" instead.', |
125
|
|
|
$shard, |
126
|
|
|
PoolingShardConnection::class, |
127
|
|
|
get_class($connection) |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param LoaderInterface|PersisterAwareInterface $loader |
134
|
|
|
* @param KernelInterface $kernel |
135
|
|
|
* @param EntityManagerInterface $manager |
136
|
|
|
* @param string[] $files |
137
|
|
|
* @param array $parameters |
138
|
|
|
* @param bool $append |
139
|
|
|
* @param bool|null $purgeWithTruncate |
140
|
|
|
* |
141
|
|
|
* @return \object[] |
142
|
|
|
*/ |
143
|
|
|
private function loadFixtures( |
144
|
|
|
LoaderInterface $loader, |
145
|
|
|
KernelInterface $kernel, |
146
|
|
|
EntityManagerInterface $manager, |
147
|
|
|
array $files, |
148
|
|
|
array $parameters, |
149
|
|
|
bool $append, |
150
|
|
|
bool $purgeWithTruncate = null |
151
|
|
|
) { |
152
|
|
|
if ($append && $purgeWithTruncate !== null) { |
153
|
|
|
throw new \LogicException( |
154
|
|
|
'Cannot append loaded fixtures and at the same time purge the database. Choose one.' |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$loader = $loader->withPersister(new ObjectManagerPersister($manager)); |
159
|
|
|
if (true === $append) { |
160
|
|
|
return $loader->load($files, $parameters); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$purgeMode = (true === $purgeWithTruncate) |
164
|
|
|
? PurgeMode::createTruncateMode() |
165
|
|
|
: PurgeMode::createDeleteMode() |
166
|
|
|
; |
167
|
|
|
|
168
|
|
|
$purger = new OrmPurger($manager, $purgeMode); |
169
|
|
|
$loader = new PurgerLoader($loader, $purger, $purger); |
|
|
|
|
170
|
|
|
$loader = new FileResolverLoader($loader, new KernelFileResolver($kernel)); |
171
|
|
|
|
172
|
|
|
return $loader->load($files, $parameters); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.