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