|
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\Executor\ORMExecutor as DoctrineORMExecutor; |
|
15
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
|
16
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class responsible for executing data fixtures. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Théo FIDRY <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ORMExecutor extends DoctrineORMExecutor implements ExecutorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ExecutorTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LoaderInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $loader; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Construct new fixtures loader instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @param EntityManagerInterface $manager EntityManagerInterface instance used for persistence. |
|
38
|
|
|
* @param LoaderInterface $loader |
|
39
|
|
|
* @param ORMPurger $purger |
|
40
|
49 |
|
*/ |
|
41
|
|
|
public function __construct(EntityManagerInterface $manager, LoaderInterface $loader, ORMPurger $purger = null) |
|
42
|
49 |
|
{ |
|
43
|
|
|
parent::__construct($manager, $purger); |
|
44
|
49 |
|
|
|
45
|
49 |
|
$this->loader = $loader; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
48 |
|
*/ |
|
51
|
|
|
public function purge() |
|
52
|
48 |
|
{ |
|
53
|
48 |
|
$connection = $this->getObjectManager()->getConnection(); |
|
54
|
|
|
$mysqlPlatform = $this->purger->getPurgeMode() === ORMPurger::PURGE_MODE_TRUNCATE |
|
|
|
|
|
|
55
|
|
|
&& $connection->getDatabasePlatform() instanceof MySqlPlatform; |
|
56
|
|
|
if ($mysqlPlatform) { |
|
57
|
|
|
$connection->exec('SET FOREIGN_KEY_CHECKS = 0;'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
parent::purge(); |
|
61
|
|
|
|
|
62
|
|
|
if ($mysqlPlatform) { |
|
63
|
|
|
$connection->exec('SET FOREIGN_KEY_CHECKS = 1;'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function execute(array $fixtures, $append = false) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->executeExecutor($this, $this->getObjectManager(), $this->loader, $fixtures, $append); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: