1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2019 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\RDMBundle\Symfony; |
14
|
|
|
|
15
|
|
|
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicEntityCodeGenerator; |
16
|
|
|
use Doctrine\Persistence\Mapping\Driver\MappingDriver as DoctrineMappingDriver; |
17
|
|
|
use Composer\Autoload\ClassLoader; |
18
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface as RDMMappingDriver; |
19
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
21
|
|
|
use Addiks\RDMBundle\AddiksRDMBundle; |
22
|
|
|
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicDataLoader; |
23
|
|
|
|
24
|
|
|
final class RDMBlackMagickCacheWarmer implements CacheWarmerInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
private DoctrineMappingDriver $doctrineMappingDriver, |
29
|
|
|
private RDMMappingDriver $rdmMappingDriver, |
30
|
|
|
private BlackMagicDataLoader $dataLoader, |
31
|
|
|
public readonly string $folderNameInCache = 'symfony_rdm_entities' |
32
|
|
|
) { |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function warmUp($cacheDirectory): array |
36
|
|
|
{ |
37
|
|
|
if (!str_ends_with($cacheDirectory, '/')) { |
38
|
|
|
$cacheDirectory .= '/'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @var mixed $entitiesFolder */ |
42
|
|
|
$entitiesFolder = $cacheDirectory . $this->folderNameInCache; |
43
|
|
|
|
44
|
|
|
$codeGenerator = new BlackMagicEntityCodeGenerator( |
45
|
|
|
$entitiesFolder, |
46
|
|
|
$this->dataLoader |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** @var array<int, array{0:string, 1:string> $classmap */ |
50
|
|
|
$classmap = array(); |
51
|
|
|
|
52
|
|
|
foreach ($this->doctrineMappingDriver->getAllClassNames() as $entityClass) { |
53
|
|
|
|
54
|
|
|
/** @var EntityMappingInterface|null $mapping */ |
55
|
|
|
$mapping = $this->rdmMappingDriver->loadRDMMetadataForClass($entityClass); |
56
|
|
|
|
57
|
|
|
if (is_object($mapping)) { |
58
|
|
|
/** @var string|null $processedEntityFilePath */ |
59
|
|
|
$processedEntityFilePath = $codeGenerator->processMapping( |
60
|
|
|
$mapping, |
61
|
|
|
AddiksRDMBundle::classLoader() |
|
|
|
|
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if (!empty($processedEntityFilePath)) { |
65
|
|
|
$processedEntityFilePath = realpath($processedEntityFilePath); |
66
|
|
|
|
67
|
|
|
if (str_starts_with($processedEntityFilePath, $cacheDirectory)) { |
68
|
|
|
$processedEntityFilePath = substr( |
69
|
|
|
$processedEntityFilePath, |
70
|
|
|
strlen($cacheDirectory) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (is_string($processedEntityFilePath)) { |
76
|
|
|
$classmap[] = [$entityClass, $processedEntityFilePath]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!is_dir($entitiesFolder)) { |
82
|
|
|
mkdir($entitiesFolder, 0777, true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
file_put_contents( |
86
|
|
|
$entitiesFolder . '/classmap', |
87
|
|
|
implode("\n", array_map( |
88
|
|
|
fn ($l) => $l[0] . ':' . $l[1], |
89
|
|
|
$classmap |
90
|
|
|
)) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return []; # Files to preload |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isOptional(): bool |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|