1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
7
|
|
|
|
8
|
|
|
class BulkEntitySaver |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var EntityManagerInterface |
12
|
|
|
*/ |
13
|
|
|
protected $entityManager; |
14
|
|
|
|
15
|
|
|
private $entitiesToSave = []; |
16
|
|
|
|
17
|
|
|
private $chunkSize = 1000; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $gcWasEnabled; |
23
|
|
|
|
24
|
|
|
private $started = false; |
25
|
|
|
private $ended = false; |
26
|
|
|
|
27
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
28
|
|
|
{ |
29
|
|
|
$this->entityManager = $entityManager; |
30
|
|
|
$this->gcWasEnabled = gc_enabled(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __destruct() |
34
|
|
|
{ |
35
|
|
|
if (true === $this->started && false === $this->ended) { |
36
|
|
|
if (!$this->entityManager->isOpen()) { |
37
|
|
|
throw new \RuntimeException('Error in ' . __METHOD__ . ': Entity Manager has been closed'); |
38
|
|
|
} |
39
|
|
|
$this->endBulkProcess(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function endBulkProcess(): void |
44
|
|
|
{ |
45
|
|
|
$this->started = false; |
46
|
|
|
$this->ended = true; |
47
|
|
|
if ([] !== $this->entitiesToSave) { |
48
|
|
|
$this->doSave(); |
49
|
|
|
} |
50
|
|
|
if (false === $this->gcWasEnabled) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
gc_enable(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function doSave() |
57
|
|
|
{ |
58
|
|
|
foreach ($this->entitiesToSave as $entity) { |
59
|
|
|
$this->entityManager->persist($entity); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->entityManager->flush(); |
63
|
|
|
$this->freeResources(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function freeResources() |
67
|
|
|
{ |
68
|
|
|
gc_enable(); |
69
|
|
|
foreach ($this->entitiesToSave as $entity) { |
70
|
|
|
$this->entityManager->detach($entity); |
71
|
|
|
} |
72
|
|
|
$this->entitiesToSave = []; |
73
|
|
|
gc_collect_cycles(); |
74
|
|
|
gc_disable(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function addEntityToSave(EntityInterface $entity) |
78
|
|
|
{ |
79
|
|
|
if (false === $this->started) { |
80
|
|
|
$this->startBulkProcess(); |
81
|
|
|
} |
82
|
|
|
$this->entitiesToSave[] = $entity; |
83
|
|
|
$this->bulkSaveIfChunkBigEnough(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function startBulkProcess(): self |
87
|
|
|
{ |
88
|
|
|
gc_disable(); |
89
|
|
|
$this->started = true; |
90
|
|
|
$this->ended = false; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function bulkSaveIfChunkBigEnough() |
96
|
|
|
{ |
97
|
|
|
end($this->entitiesToSave); |
98
|
|
|
$key = key($this->entitiesToSave); |
99
|
|
|
$size = $key + 1; |
100
|
|
|
if (($size % $this->chunkSize) === 0) { |
101
|
|
|
$this->doSave(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getChunkSize(): int |
109
|
|
|
{ |
110
|
|
|
return $this->chunkSize; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $chunkSize |
115
|
|
|
* |
116
|
|
|
* @return BulkEntitySaver |
117
|
|
|
*/ |
118
|
|
|
public function setChunkSize(int $chunkSize): BulkEntitySaver |
119
|
|
|
{ |
120
|
|
|
$this->chunkSize = $chunkSize; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|