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
|
|
|
public readonly string|null $projectRootFolder = null |
33
|
|
|
) { |
34
|
|
|
if (!empty($this->projectRootFolder)) { |
35
|
|
|
$this->projectRootFolder = realpath($projectRootFolder) . '/'; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function warmUp($cacheDirectory): array |
40
|
|
|
{ |
41
|
|
|
/** @var mixed $entitiesFolder */ |
42
|
|
|
$entitiesFolder = sprintf( |
43
|
|
|
'%s/%s', |
44
|
|
|
$cacheDirectory, |
45
|
|
|
$this->folderNameInCache |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$codeGenerator = new BlackMagicEntityCodeGenerator( |
49
|
|
|
$entitiesFolder, |
50
|
|
|
$this->dataLoader |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** @var array<int, array{0:string, 1:string> $classmap */ |
54
|
|
|
$classmap = array(); |
55
|
|
|
|
56
|
|
|
foreach ($this->doctrineMappingDriver->getAllClassNames() as $entityClass) { |
57
|
|
|
|
58
|
|
|
/** @var EntityMappingInterface|null $mapping */ |
59
|
|
|
$mapping = $this->rdmMappingDriver->loadRDMMetadataForClass($entityClass); |
60
|
|
|
|
61
|
|
|
if (is_object($mapping)) { |
62
|
|
|
/** @var string|null $processedEntityFilePath */ |
63
|
|
|
$processedEntityFilePath = $codeGenerator->processMapping( |
64
|
|
|
$mapping, |
65
|
|
|
AddiksRDMBundle::classLoader() |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if (!empty($this->projectRootFolder)) { |
69
|
|
|
$processedEntityFilePath = realpath($processedEntityFilePath); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if (str_starts_with($processedEntityFilePath, $this->projectRootFolder)) { |
72
|
|
|
$processedEntityFilePath = substr( |
73
|
|
|
$processedEntityFilePath, |
74
|
|
|
strlen($this->projectRootFolder) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (is_string($processedEntityFilePath)) { |
80
|
|
|
$classmap[] = [$entityClass, $processedEntityFilePath]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!is_dir($entitiesFolder)) { |
86
|
|
|
mkdir($entitiesFolder, 0777, true); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
file_put_contents( |
90
|
|
|
$entitiesFolder . '/classmap', |
91
|
|
|
implode("\n", array_map( |
92
|
|
|
fn ($l) => $l[0] . ':' . $l[1], |
93
|
|
|
$classmap |
94
|
|
|
)) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return []; # Files to preload |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function isOptional(): bool |
101
|
|
|
{ |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|