1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "RocketORM" package. |
5
|
|
|
* |
6
|
|
|
* https://github.com/RocketORM/ORM |
7
|
|
|
* |
8
|
|
|
* For the full license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Generator\Model\Object; |
13
|
|
|
|
14
|
|
|
use Rocket\ORM\Generator\Generator; |
15
|
|
|
use Rocket\ORM\Generator\Schema\Schema; |
16
|
|
|
use Rocket\ORM\Rocket; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Sylvain Lorinet <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ObjectGenerator extends Generator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $modelNamespace; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Twig_Environment |
30
|
|
|
*/ |
31
|
|
|
protected $twig; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $modelNamespace |
36
|
|
|
* @param array $templateDirs |
37
|
|
|
*/ |
38
|
|
|
public function __construct($modelNamespace = '', array $templateDirs = []) |
39
|
|
|
{ |
40
|
|
|
$this->modelNamespace = $modelNamespace; |
41
|
|
|
|
42
|
|
|
$loader = new \Twig_Loader_Filesystem(array_merge($templateDirs, [ |
43
|
|
|
__DIR__ . '/../../Resources/Skeletons/Model/Object' |
44
|
|
|
])); |
45
|
|
|
$loader->addPath(__DIR__ . '/../../Resources/Skeletons/Model/Object/Driver/SQLite', 'sqlite'); |
46
|
|
|
|
47
|
|
|
$this->twig = new \Twig_Environment($loader, [ |
48
|
|
|
'cache' => false, |
49
|
|
|
'strict_variables' => true |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$this->twig->addFilter(new \Twig_SimpleFilter('type', function ($variable, $type) { |
53
|
|
|
switch ($type) { |
54
|
|
|
case 'string': return is_string($variable); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new \LogicException('Unknown type "' . $type . '" for the Twig filter "type"'); |
58
|
|
|
})); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Schema $schema |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function generate(Schema $schema) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
// First generate base |
69
|
|
|
$this->generateBase($schema); |
70
|
|
|
|
71
|
|
|
$outputDirectory = $schema->absoluteDirectory; |
72
|
|
|
$this->createDirectory($outputDirectory); |
73
|
|
|
|
74
|
|
|
foreach ($schema->getTables() as $table) { |
75
|
|
|
$outputFile = $outputDirectory . DIRECTORY_SEPARATOR . $table->phpName . '.php'; |
76
|
|
|
|
77
|
|
|
// Check if file already exists, do not override the file |
78
|
|
|
if (is_file($outputFile)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$template = $this->twig->render('object.php.twig', [ |
83
|
|
|
'table' => $table |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
file_put_contents($outputFile, $template); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Schema $schema |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
protected function generateBase(Schema $schema) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$outputDirectory = $schema->absoluteDirectory . DIRECTORY_SEPARATOR . 'Base'; |
96
|
|
|
$this->createDirectory($outputDirectory); |
97
|
|
|
|
98
|
|
|
// Allow overriding template for a given driver |
99
|
|
|
$driver = Rocket::getConnectionDriver($schema->connection); |
100
|
|
|
|
101
|
|
|
foreach ($schema->getTables() as $table) { |
102
|
|
|
$template = $this->twig->resolveTemplate([ |
103
|
|
|
'@' . $driver . '/base_object.php.twig', |
104
|
|
|
'base_object.php.twig' |
105
|
|
|
])->render([ |
106
|
|
|
'table' => $table, |
107
|
|
|
'driver' => $driver |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
file_put_contents($outputDirectory . DIRECTORY_SEPARATOR . 'Base' . $table->phpName . '.php', $template); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.