-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DatabaseToDoctrineEntityExtractorCommand.php
203 lines (160 loc) · 7.8 KB
/
DatabaseToDoctrineEntityExtractorCommand.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace Baraja\Doctrine;
use Baraja\Doctrine\Cache\ArrayCache;
use Doctrine\Common\ClassLoader;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Tools\EntityGenerator;
use InvalidArgumentException;
use Nette\Utils\FileSystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* @deprecated because internal Doctrine class EntityGenerator is being removed from the ORM and won't have any replacement
*/
final class DatabaseToDoctrineEntityExtractorCommand extends Command
{
public function __construct(
private string $rootDir,
private EntityManagerInterface $entityManager,
) {
parent::__construct();
$realPath = realpath($rootDir);
$this->rootDir = $realPath === false ? $rootDir : $realPath;
}
public function configure(): void
{
$this->setName('orm:database-to-doctrine-entity-extractor')
->setDescription('Scan current database schema and generate valid Doctrine entities.')
->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Entity namespace.')
->addOption('path', null, InputOption::VALUE_REQUIRED, 'Where generated entities will be stored.');
}
public function execute(InputInterface $input, OutputInterface $output): int
{
echo '-------------------------' . "\n";
echo 'CRITICAL WARNING' . "\n\n";
echo 'This tool is deprecated, because internal Doctrine class EntityGenerator is being removed from the ORM and won\'t have any replacement.';
echo "\n\n";
$namespace = $input->getOption('namespace');
if ($namespace === null) {
throw new InvalidArgumentException('Option "namespace" is required.');
}
if (is_string($namespace) === false) {
throw new InvalidArgumentException('Option "namespace" must be string.');
}
$path = $input->getOption('path');
if ($path === null) {
throw new InvalidArgumentException('Option "path" is required.');
}
if (\is_string($path) === false) {
throw new InvalidArgumentException('Option "path" must be string.');
}
$entityNamespace = (string) preg_replace_callback(
'/(^(?:[a-z])|(?:\\\\[a-z]))/',
static fn(array $match): string => strtoupper($match[1]),
$namespace,
);
$output->writeln("\n");
$output->writeln('Welcome to <comment>Baraja Database to Doctrine entity Extractor</comment>!');
$output->writeln('<info>This tool analyzes your database and automatically generates valid entities.</info>');
$output->writeln("\n");
$helper = $this->getHelper('question');
assert($helper instanceof QuestionHelper);
$questionNamespace = new ConfirmationQuestion(
'Given namespace is "<comment>' . $namespace . '</comment>", '
. 'so entity class-name should be "<comment>' . $entityNamespace . '\User</comment>" for example?',
false,
);
$realPath = rtrim($this->rootDir . '/' . $path, '/');
$questionPath = new ConfirmationQuestion(
'Given path is "<comment>' . $path . '</comment>", '
. 'your project root dir is "<comment>' . $this->rootDir . '</comment>", '
. 'so entity can be stored in directory "<comment>' . $realPath . '</comment>"?',
false,
);
$questionEntityDir = new ConfirmationQuestion('Can the "<comment>Entity</comment>" directory be added to the end of the path?', false);
if ($helper->ask($input, $output, $questionNamespace) === false) {
$output->writeln('<error>Please use different "--namespace" argument.</error>');
return 1;
}
if ($helper->ask($input, $output, $questionPath) === false) {
$output->writeln('<error>Please specify relative path in "--path" argument.</error>');
return 1;
}
$realPath .= ((bool) $helper->ask($input, $output, $questionEntityDir)) ? '/Entity' : '';
FileSystem::createDir($realPath);
$output->writeln("\n\n" . '<comment>Scaning your database...</comment>' . "\n\n");
$connection = $this->entityManager->getConnection();
$output->writeln('<info>Available tables</info> (database "<comment>' . $connection->getDatabase() . '</comment>"):');
$showTablesStatement = $connection->executeQuery('SHOW TABLES');
$tableMapper = static fn(array $item): string => (string) (array_values($item)[0] ?? '');
$tables = array_map(
$tableMapper,
$showTablesStatement->fetchAllAssociative(),
);
$output->writeln('"<comment>' . implode('</comment>", "<comment>', $tables) . '</comment>".');
$output->writeln('<info>Count tables:</info> ' . \count($tables));
$output->writeln('Generating...');
$classLoader = new ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new ClassLoader('Proxies', __DIR__);
$classLoader->register();
$config = new Configuration;
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__ . '/Entities']));
$config->setMetadataCacheImpl(new ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$em = EntityManager::create($connection, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new DatabaseDriver($em->getConnection()->getSchemaManager());
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new DisconnectedClassMetadataFactory;
$cmf->setEntityManager($em);
/** @var ClassMetadataInfo[] $metadata */
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator;
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, $realPath);
$output->writeln('Done. Formatting...');
$entityFilePaths = glob($realPath . '/*.php');
foreach (is_array($entityFilePaths) ? $entityFilePaths : [] as $entityFilePath) {
$this->formatCodingStandard($entityFilePath, $entityNamespace);
}
$output->writeln('<info>All tasks successfully done.</info>');
return 0;
}
private function formatCodingStandard(string $path, string $namespace): void
{
$content = FileSystem::read($path);
// 1. Convert spaces to tabs
$content = str_replace(' ', "\t", $content);
// 2. Add strict declare and namespace
$content = (string) preg_replace_callback('/^<\?php(\s+)use\s/', static fn(array $match): string => '<?php' . "\n\n"
. 'declare(strict_types=1);' . "\n\n"
. 'namespace ' . $namespace . ';' . "\n\n\n"
. 'use ', $content);
// 3. Fix relations to other entity
$content = (string) preg_replace_callback('/@(param|return|var)\s(\\\\\w+)/', static fn(array $match): string => '@' . $match[1] . ' ' . (\class_exists($match[2]) ? $match[2] : trim($match[2], '\\')), $content);
// 4. Fix namespace in relation annotation
$content = (string) preg_replace_callback('/(@ORM\\\\\w+\(targetEntity=")([^"]+)"/', static fn(array $match): string => $match[1] . '\\' . $namespace . '\\' . $match[2] . '"', $content);
// 5. Fix annotation type in setter
$content = (string) preg_replace_callback('/(function\sset\w+)\((\\\\\w+)\s/', static fn(array $match): string => $match[1] . '(' . (\class_exists($match[2]) ? $match[2] : trim($match[2], '\\')) . ' ', $content);
// 6. Add self typehint to setters
$content = (string) preg_replace_callback('/(public\sfunction\s\w+[^)]+)\)(\s*[^:](?:\n|[^}])+?return\s\$this;)/', static fn(array $match): string => $match[1] . '): self' . $match[2], $content);
FileSystem::write($path, $content);
}
}