-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
EntityClassGenerator.php
146 lines (125 loc) · 5.07 KB
/
EntityClassGenerator.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
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Doctrine;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Maker\Common\EntityIdTypeEnum;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\UX\Turbo\Attribute\Broadcast;
/**
* @internal
*/
final class EntityClassGenerator
{
public function __construct(
private Generator $generator,
private DoctrineHelper $doctrineHelper,
) {
}
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, EntityIdTypeEnum $useUuidIdentifier = EntityIdTypeEnum::INT): string
{
$repoClassDetails = $this->generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
'Repository\\',
'Repository'
);
$tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName());
$useStatements = new UseStatementGenerator([
$repoClassDetails->getFullName(),
['Doctrine\\ORM\\Mapping' => 'ORM'],
]);
if ($broadcast) {
$useStatements->addUseStatement(Broadcast::class);
}
if ($apiResource) {
$useStatements->addUseStatement(ApiResource::class);
}
if (EntityIdTypeEnum::UUID === $useUuidIdentifier) {
$useStatements->addUseStatement([
Uuid::class,
UuidType::class,
]);
}
if (EntityIdTypeEnum::ULID === $useUuidIdentifier) {
$useStatements->addUseStatement([
Ulid::class,
UlidType::class,
]);
}
$entityPath = $this->generator->generateClass(
$entityClassDetails->getFullName(),
'doctrine/Entity.tpl.php',
[
'use_statements' => $useStatements,
'repository_class_name' => $repoClassDetails->getShortName(),
'api_resource' => $apiResource,
'broadcast' => $broadcast,
'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName),
'table_name' => $tableName,
'id_type' => $useUuidIdentifier,
]
);
if ($generateRepositoryClass) {
$this->generateRepositoryClass(
$repoClassDetails->getFullName(),
$entityClassDetails->getFullName(),
$withPasswordUpgrade,
true
);
}
return $entityPath;
}
public function generateRepositoryClass(string $repositoryClass, string $entityClass, bool $withPasswordUpgrade, bool $includeExampleComments = true): void
{
$shortEntityClass = Str::getShortClassName($entityClass);
$entityAlias = strtolower($shortEntityClass[0]);
$passwordUserInterfaceName = UserInterface::class;
if (interface_exists(PasswordAuthenticatedUserInterface::class)) {
$passwordUserInterfaceName = PasswordAuthenticatedUserInterface::class;
}
$interfaceClassNameDetails = new ClassNameDetails($passwordUserInterfaceName, 'Symfony\Component\Security\Core\User');
$useStatements = new UseStatementGenerator([
$entityClass,
ManagerRegistry::class,
ServiceEntityRepository::class,
]);
if ($withPasswordUpgrade) {
$useStatements->addUseStatement([
$interfaceClassNameDetails->getFullName(),
PasswordUpgraderInterface::class,
UnsupportedUserException::class,
]);
}
$this->generator->generateClass(
$repositoryClass,
'doctrine/Repository.tpl.php',
[
'use_statements' => $useStatements,
'entity_class_name' => $shortEntityClass,
'entity_alias' => $entityAlias,
'with_password_upgrade' => $withPasswordUpgrade,
'password_upgrade_user_interface' => $interfaceClassNameDetails,
'include_example_comments' => $includeExampleComments,
]
);
}
}