-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathAbstractExecutor.php
161 lines (138 loc) · 3.82 KB
/
AbstractExecutor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace Doctrine\Common\DataFixtures\Executor;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\Common\DataFixtures\SharedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Exception;
use function get_class;
use function interface_exists;
use function sprintf;
/**
* Abstract fixture executor.
*/
abstract class AbstractExecutor
{
/**
* Purger instance for purging database before loading data fixtures
*
* @var PurgerInterface
*/
protected $purger;
/**
* Logger callback for logging messages when loading data fixtures
*
* @var callable
*/
protected $logger;
/**
* Fixture reference repository
*
* @var ReferenceRepository
*/
protected $referenceRepository;
public function __construct(ObjectManager $manager)
{
$this->referenceRepository = new ReferenceRepository($manager);
}
/** @return ReferenceRepository */
public function getReferenceRepository()
{
return $this->referenceRepository;
}
public function setReferenceRepository(ReferenceRepository $referenceRepository)
{
$this->referenceRepository = $referenceRepository;
}
/**
* Sets the Purger instance to use for this executor instance.
*
* @return void
*/
public function setPurger(PurgerInterface $purger)
{
$this->purger = $purger;
}
/** @return PurgerInterface */
public function getPurger()
{
return $this->purger;
}
/**
* Set the logger callable to execute with the log() method.
*
* @param callable $logger
*
* @return void
*/
public function setLogger($logger)
{
$this->logger = $logger;
}
/**
* Logs a message using the logger.
*
* @param string $message
*
* @return void
*/
public function log($message)
{
$logger = $this->logger;
$logger($message);
}
/**
* Load a fixture with the given persistence manager.
*
* @return void
*/
public function load(ObjectManager $manager, FixtureInterface $fixture)
{
if ($this->logger) {
$prefix = '';
if ($fixture instanceof OrderedFixtureInterface) {
$prefix = sprintf('[%d] ', $fixture->getOrder());
}
$this->log('loading ' . $prefix . get_class($fixture));
}
// additionally pass the instance of reference repository to shared fixtures
if ($fixture instanceof SharedFixtureInterface) {
$fixture->setReferenceRepository($this->referenceRepository);
}
$fixture->load($manager);
$manager->clear();
}
/**
* Purges the database before loading.
*
* @return void
*
* @throws Exception if the purger is not defined.
*/
public function purge()
{
if ($this->purger === null) {
throw new Exception(
PurgerInterface::class .
' instance is required if you want to purge the database before loading your data fixtures.'
);
}
if ($this->logger) {
$this->log('purging database');
}
$this->purger->purge();
}
/**
* Executes the given array of data fixtures.
*
* @param FixtureInterface[] $fixtures Array of fixtures to execute.
* @param bool $append Whether to append the data fixtures or purge the database before loading.
*
* @return void
*/
abstract public function execute(array $fixtures, $append = false);
}
interface_exists(ObjectManager::class);