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\Doctrine\DataFixtures\Executor; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger; |
15
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
16
|
|
|
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger; |
17
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
18
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager as MongoDBDocumentManager; |
19
|
|
|
use Doctrine\ODM\PHPCR\DocumentManager as PHPCRDocumentManager; |
20
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
21
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
22
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Théo FIDRY <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class FixturesExecutor implements FixturesExecutorInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
84 |
|
*/ |
32
|
|
|
public function execute( |
33
|
|
|
ObjectManager $manager, |
34
|
|
|
LoaderInterface $loader, |
35
|
|
|
array $fixturesFiles, |
36
|
|
|
$append, |
37
|
|
|
$loggerCallable, |
38
|
|
|
$truncate = false |
39
|
|
|
) { |
40
|
84 |
|
// Get executor |
41
|
84 |
|
$executor = $this->getExecutor($manager, $loader, $truncate); |
42
|
|
|
$executor->setLogger($loggerCallable); |
43
|
|
|
|
44
|
84 |
|
// Purge database and load fixtures |
45
|
84 |
|
$executor->execute($fixturesFiles, $append); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the executor for the matching the given object manager. |
50
|
|
|
* |
51
|
|
|
* @param ObjectManager $manager |
52
|
|
|
* @param LoaderInterface $loader |
53
|
|
|
* @param bool|null $purgeMode |
54
|
|
|
* |
55
|
|
|
* @return ExecutorInterface |
56
|
84 |
|
*/ |
57
|
|
|
private function getExecutor(ObjectManager $manager, LoaderInterface $loader, $purgeMode) |
58
|
84 |
|
{ |
59
|
84 |
|
switch (true) { |
60
|
78 |
|
case $manager instanceof EntityManagerInterface: |
61
|
78 |
|
$executor = new ORMExecutor($manager, $loader); |
62
|
78 |
|
$metaData = $manager->getMetadataFactory()->getAllMetadata(); |
63
|
|
|
|
64
|
78 |
|
$excluded = []; |
65
|
|
|
foreach ($metaData as $classMetadata) { |
66
|
78 |
|
/** @var ClassMetadata $classMetadata */ |
67
|
78 |
|
if ($classMetadata->isReadOnly) { |
68
|
|
|
$excluded[] = implode('.', [$classMetadata->getSchemaName(), $classMetadata->getTableName()]); |
69
|
12 |
|
} |
70
|
12 |
|
} |
71
|
12 |
|
$purger = new ORMPurger($manager, $excluded); |
|
|
|
|
72
|
12 |
|
$purger->setPurgeMode( |
73
|
|
|
$purgeMode |
74
|
6 |
|
? ORMPurger::PURGE_MODE_TRUNCATE |
75
|
6 |
|
: ORMPurger::PURGE_MODE_DELETE |
76
|
6 |
|
); |
77
|
6 |
|
break; |
78
|
|
|
|
79
|
6 |
|
case $manager instanceof MongoDBDocumentManager: |
80
|
6 |
|
$executor = new MongoDBExecutor($manager, $loader); |
81
|
6 |
|
$purger = new MongoDBPurger($manager); |
82
|
6 |
|
break; |
83
|
6 |
|
|
84
|
6 |
|
case $manager instanceof PHPCRDocumentManager: |
|
|
|
|
85
|
|
|
$executor = new PHPCRExecutor($manager, $loader); |
86
|
84 |
|
$purger = new PHPCRPurger($manager); |
87
|
|
|
break; |
88
|
84 |
|
|
89
|
|
|
default: |
90
|
|
|
throw new \InvalidArgumentException(sprintf( |
91
|
|
|
'Unsupported manager type %s', |
92
|
|
|
get_class($manager)) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$executor->setPurger($purger); |
97
|
|
|
|
98
|
|
|
return $executor; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.