1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
error_reporting(E_ALL); |
4
|
|
|
ini_set('display_errors', 'on'); |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
|
8
|
|
|
require_once __DIR__ . '/../../htdocs/app/autoload.php'; |
9
|
|
|
|
10
|
|
|
function lowerCamelCase($string) |
11
|
|
|
{ |
12
|
|
|
$string = ucwords($string, '_'); |
13
|
|
|
$string[0] = strtolower($string[0]); |
14
|
|
|
$string = str_replace('_', '', $string); |
15
|
|
|
|
16
|
|
|
return $string; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function mapDataBaseTypesForPhpDoc($type) |
20
|
|
|
{ |
21
|
|
|
switch ($type) { |
22
|
|
|
case 'tinyint': |
23
|
|
|
case 'int': |
24
|
|
|
return 'int'; |
25
|
|
|
case 'varchar': |
26
|
|
|
case 'text': |
27
|
|
|
case 'mediumtext': |
28
|
|
|
case 'timestamp': |
29
|
|
|
return 'string'; |
30
|
|
|
case 'datetime': |
31
|
|
|
return 'DateTime'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $type; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @var Connection $connection */ |
38
|
|
|
$kernel = new AppKernel('dev', false); |
39
|
|
|
$kernel->boot(); |
40
|
|
|
$connection = $kernel::Container()->get(Connection::class); |
41
|
|
|
|
42
|
|
|
$tables = $connection->fetchAll( |
43
|
|
|
'SELECT DISTINCT(TABLE_NAME) |
44
|
|
|
FROM information_schema.COLUMNS |
45
|
|
|
WHERE TABLE_SCHEMA = :databaseName', |
46
|
|
|
[':databaseName' => 'opencaching'] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
foreach ($tables as $table) { |
50
|
|
|
$isNewMethod = false; |
51
|
|
|
$tableName = $table['TABLE_NAME']; |
52
|
|
|
$tableColumns = $connection |
53
|
|
|
->fetchAll( |
54
|
|
|
'SELECT * |
55
|
|
|
FROM information_schema.COLUMNS |
56
|
|
|
WHERE TABLE_SCHEMA = :databaseName |
57
|
|
|
AND TABLE_NAME = :tableName;', |
58
|
|
|
[ |
59
|
|
|
':databaseName' => 'opencaching', |
60
|
|
|
':tableName' => $tableName, |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$className = str_replace('_', '', ucwords($tableName, '_') . 'Entity'); |
65
|
|
|
|
66
|
|
|
if (strpos($className, 'Cache') === 0) { |
67
|
|
|
$className = str_replace('Cache', 'GeoCache', $className); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$class = new Nette\PhpGenerator\ClassType($className); |
71
|
|
|
$class->setExtends(Oc\Repository\AbstractEntity::class); |
72
|
|
|
|
73
|
|
|
foreach ($tableColumns as $column) { |
74
|
|
|
if ($isNewMethod === false && $column['COLUMN_KEY'] === 'PRI') { |
75
|
|
|
$isNewMethod = lowerCamelCase($column['COLUMN_NAME']); |
76
|
|
|
} |
77
|
|
|
$class->addProperty(lowerCamelCase($column['COLUMN_NAME'])) |
78
|
|
|
->setVisibility('public') |
79
|
|
|
->addComment('@var ' . mapDataBaseTypesForPhpDoc($column['DATA_TYPE'])); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($isNewMethod) { |
|
|
|
|
83
|
|
|
$class->addMethod('isNew') |
84
|
|
|
->addComment('@return bool') |
85
|
|
|
->setVisibility('public') |
86
|
|
|
->setBody('return $this->' . $isNewMethod . '=== null;'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
file_put_contents(__DIR__ . '/Entities/' . $className . '.php', "<?php \n\n" . $class->__toString(), LOCK_EX); |
90
|
|
|
} |
91
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: