|
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\PurgerLoader; |
|
10
|
|
|
use Fidry\AliceDataFixtures\LoaderInterface; |
|
11
|
|
|
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface; |
|
12
|
|
|
use Hautelook\AliceBundle\BundleResolverInterface; |
|
13
|
|
|
use Hautelook\AliceBundle\FixtureLocatorInterface; |
|
14
|
|
|
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface; |
|
15
|
|
|
use Hautelook\AliceBundle\LoggerAwareInterface; |
|
16
|
|
|
use Nelmio\Alice\NotClonableTrait; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
19
|
|
|
|
|
20
|
|
|
final class DoctrineOrmLoader implements AliceBundleLoaderInterface, LoggerAwareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use NotClonableTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var BundleResolverInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $bundleResolver; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var FixtureLocatorInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $fixtureLocator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoaderInterface|PersisterAwareInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $loader; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var LoggerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $logger; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
BundleResolverInterface $bundleResolver, |
|
46
|
|
|
FixtureLocatorInterface $fixtureLocator, |
|
47
|
|
|
LoaderInterface $loader, |
|
48
|
|
|
LoggerInterface $logger |
|
49
|
|
|
) { |
|
50
|
|
|
$this->bundleResolver = $bundleResolver; |
|
51
|
|
|
$this->fixtureLocator = $fixtureLocator; |
|
52
|
|
|
if (false === $loader instanceof PersisterAwareInterface) { |
|
|
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException( |
|
54
|
|
|
'Expected loader to be an instance of "%s".', |
|
55
|
|
|
PersisterAwareInterface::class |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
$this->loader = $loader; |
|
59
|
|
|
$this->logger = $logger; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function withLogger(LoggerInterface $logger): self |
|
66
|
|
|
{ |
|
67
|
|
|
return new self($this->bundleResolver, $this->fixtureLocator, $this->loader, $logger); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
|
|
public function load( |
|
74
|
|
|
Application $application, |
|
75
|
|
|
EntityManagerInterface $manager, |
|
76
|
|
|
array $bundles, |
|
77
|
|
|
bool $append, |
|
78
|
|
|
string $environment = null, |
|
79
|
|
|
string $shard = null, |
|
80
|
|
|
bool $purgeWithTruncate = null |
|
81
|
|
|
) { |
|
82
|
|
|
$bundles = $this->bundleResolver->resolveBundles($application, $bundles); |
|
83
|
|
|
$fixtureFiles = $this->fixtureLocator->locate($bundles, $environment); |
|
84
|
|
|
|
|
85
|
|
|
$this->logger->info(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures found:')); |
|
86
|
|
|
foreach ($fixtureFiles as $fixture) { |
|
87
|
|
|
$this->logger->info(sprintf(sprintf(' <comment>-</comment> <info>%s</info>', $fixture))); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ('' !== $shard) { |
|
91
|
|
|
$this->connectToShardConnection($manager, $shard); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$fixtures = $this->loadFixtures( |
|
95
|
|
|
$this->loader, |
|
96
|
|
|
$manager, |
|
97
|
|
|
$fixtureFiles, |
|
98
|
|
|
$application->getKernel()->getContainer()->getParameterBag()->all(), |
|
|
|
|
|
|
99
|
|
|
$append, |
|
100
|
|
|
$purgeWithTruncate |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$this->logger->info(sprintf(' <comment>></comment> <info>%s</info>', 'fixtures loaded')); |
|
104
|
|
|
|
|
105
|
|
|
return $fixtures; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function connectToShardConnection(EntityManagerInterface $manager, string $shard) |
|
109
|
|
|
{ |
|
110
|
|
|
$connection = $manager->getConnection(); |
|
111
|
|
|
if ($connection instanceof PoolingShardConnection) { |
|
112
|
|
|
$connection->connect($shard); |
|
113
|
|
|
|
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
throw new \InvalidArgumentException( |
|
118
|
|
|
sprintf( |
|
119
|
|
|
'Could not establish a shard connection for the shard "%s". The connection must be an instance' |
|
120
|
|
|
.' of "%s", got "%s" instead.', |
|
121
|
|
|
$shard, |
|
122
|
|
|
PoolingShardConnection::class, |
|
123
|
|
|
get_class($connection) |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param LoaderInterface|PersisterAwareInterface $loader |
|
130
|
|
|
* @param EntityManagerInterface $manager |
|
131
|
|
|
* @param string[] $files |
|
132
|
|
|
* @param array $parameters |
|
133
|
|
|
* @param bool $append |
|
134
|
|
|
* @param bool|null $purgeWithTruncate |
|
135
|
|
|
* |
|
136
|
|
|
* @return object[] |
|
137
|
|
|
*/ |
|
138
|
|
|
private function loadFixtures( |
|
139
|
|
|
LoaderInterface $loader, |
|
140
|
|
|
EntityManagerInterface $manager, |
|
141
|
|
|
array $files, |
|
142
|
|
|
array $parameters, |
|
143
|
|
|
bool $append, |
|
144
|
|
|
bool $purgeWithTruncate = null |
|
145
|
|
|
) { |
|
146
|
|
|
if ($append && $purgeWithTruncate !== null) { |
|
147
|
|
|
throw new \LogicException( |
|
148
|
|
|
'Cannot append loaded fixtures and at the same time purge the database. Choose one.' |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$loader = $loader->withPersister(new ObjectManagerPersister($manager)); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
if ($append) { |
|
155
|
|
|
return $loader->load($files, $parameters); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$purgeMode = (null === $purgeWithTruncate || false === $purgeWithTruncate) |
|
159
|
|
|
? \Doctrine\Common\DataFixtures\Purger\ORMPurger::PURGE_MODE_DELETE |
|
160
|
|
|
: \Doctrine\Common\DataFixtures\Purger\ORMPurger::PURGE_MODE_TRUNCATE |
|
161
|
|
|
; |
|
162
|
|
|
$purger = new OrmPurger($manager, $purgeMode); |
|
|
|
|
|
|
163
|
|
|
$loader = new PurgerLoader($loader, $purger, $purger); |
|
164
|
|
|
|
|
165
|
|
|
return $loader->load($files, $parameters); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.