|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Generate | Generate.php |
|
4
|
|
|
* @package ORM\Console |
|
5
|
|
|
* @author Florian Knapp <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Faulancer\Console\Handler; |
|
8
|
|
|
|
|
9
|
|
|
use Faulancer\Console\ArgumentParser; |
|
10
|
|
|
use Faulancer\Console\ConsoleInterface; |
|
11
|
|
|
use Faulancer\Console\Helper\DbTables; |
|
12
|
|
|
use Faulancer\Console\Helper\TableRelationResolver; |
|
13
|
|
|
use Faulancer\Console\Output; |
|
14
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
|
15
|
|
|
use ORM\DbConfig; |
|
16
|
|
|
use ORM\EntityManager; |
|
17
|
|
|
use Faulancer\Service\Config; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\Tests\Service; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Generate |
|
22
|
|
|
*/ |
|
23
|
|
|
class Generate implements ConsoleInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $mysqlTypeMapping = [ |
|
28
|
|
|
'/@property text/' => '@property string', |
|
29
|
|
|
'/@property int\((\d+)\)/' => '@property integer', |
|
30
|
|
|
'/@property datetime/' => '@property string', |
|
31
|
|
|
'/@property timestamp/' => '@property string', |
|
32
|
|
|
'/varchar\((\d+)\)/' => 'string' |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** @var Config */ |
|
36
|
|
|
protected $config; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ArgumentParser */ |
|
39
|
|
|
protected $args; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generate constructor. |
|
43
|
|
|
* @param Config $config |
|
44
|
|
|
* @param ArgumentParser $args |
|
45
|
|
|
* @codeCoverageIgnore |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Config $config, ArgumentParser $args) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->config = $config; |
|
50
|
|
|
$this->args = $args; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws \Exception |
|
55
|
|
|
* @throws \ORM\Exceptions\NoConnection |
|
56
|
|
|
* @codeCoverageIgnore |
|
57
|
|
|
*/ |
|
58
|
|
|
public function entitiesAction() |
|
59
|
|
|
{ |
|
60
|
|
|
$creationCount = 0; |
|
61
|
|
|
$entitiesRoot = $this->config->get('projectRoot') . '/src/Entity'; |
|
62
|
|
|
|
|
63
|
|
|
if (!is_dir($entitiesRoot)) { |
|
64
|
|
|
mkdir($entitiesRoot); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$result = DbTables::getTableData($this->config); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($result as $table => $fields) { |
|
70
|
|
|
|
|
71
|
|
|
$properties = []; |
|
72
|
|
|
$namespace = $this->config->get('namespacePrefix') . '\\Entity'; |
|
73
|
|
|
$className = ucfirst($table) . 'Entity'; |
|
74
|
|
|
|
|
75
|
|
|
Output::writeLine('Process table "' . $table . '"', 'warning'); |
|
76
|
|
|
|
|
77
|
|
|
$targetFile = $entitiesRoot . '/' . $className . '.php'; |
|
78
|
|
|
|
|
79
|
|
|
if (file_exists($targetFile)) { |
|
80
|
|
|
Output::writeLine('Entity "' . $className . '" already exists... skipping.', 'error'); |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$properties[] = '/**'; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($fields as $field) { |
|
87
|
|
|
|
|
88
|
|
|
$properties[] = ' * @property ' . $field['Type'] . ' $' . $field['Field']; |
|
89
|
|
|
Output::writeLine('Create property ' . $field['Field']); |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$properties[] = ' */'; |
|
94
|
|
|
|
|
95
|
|
|
$props = implode(PHP_EOL, $properties); |
|
96
|
|
|
$props = preg_replace( |
|
97
|
|
|
array_keys($this->mysqlTypeMapping), |
|
98
|
|
|
array_values($this->mysqlTypeMapping), |
|
99
|
|
|
$props |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$fixture = str_replace( |
|
103
|
|
|
['{$namespace}', '{$props}', '{$table}', '{$className}'], |
|
104
|
|
|
[$namespace, $props, $table, $className], |
|
105
|
|
|
file_get_contents(__DIR__ . '/../Fixture/Entity.fixture') |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
file_put_contents($entitiesRoot . '/' . $className . '.php', $fixture); |
|
110
|
|
|
|
|
111
|
|
|
Output::writeLine('Entity for table "' . $table . '" successfully generated', 'success'); |
|
112
|
|
|
|
|
113
|
|
|
$creationCount++; |
|
114
|
|
|
|
|
115
|
|
|
Output::writeEmptyLine(); |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
Output::writeLine($creationCount . ' entities generated', 'success'); |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |