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\TableMap; |
13
|
|
|
|
14
|
|
|
use Rocket\ORM\Generator\Generator; |
15
|
|
|
use Rocket\ORM\Generator\Schema\Schema; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Sylvain Lorinet <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class TableMapGenerator extends Generator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $modelNamespace; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Twig_Environment |
29
|
|
|
*/ |
30
|
|
|
protected $twig; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $modelNamespace |
35
|
|
|
* @param array $templateDirs |
36
|
|
|
*/ |
37
|
|
|
public function __construct($modelNamespace = '\\Rocket\\ORM\\Model\\Map\\TableMap', array $templateDirs = []) |
38
|
|
|
{ |
39
|
|
|
$class = new \ReflectionClass($modelNamespace); |
40
|
|
|
if (!$class->implementsInterface('\\Rocket\\ORM\\Model\\Map\\TableMapInterface')) { |
41
|
|
|
throw new \InvalidArgumentException('The table map model must implement Rocket\ORM\Model\Map\TableMapInterface'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->modelNamespace = $modelNamespace; |
45
|
|
|
$this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array_merge($templateDirs, [__DIR__ . '/../../Resources/Skeletons/Model/Map'])), [ |
46
|
|
|
'cache' => false, |
47
|
|
|
'strict_variables' => true |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Schema $schema |
53
|
|
|
*/ |
54
|
|
|
public function generate(Schema $schema) |
55
|
|
|
{ |
56
|
|
|
$outputDirectory = $schema->absoluteDirectory . DIRECTORY_SEPARATOR . 'TableMap'; |
57
|
|
|
$this->createDirectory($outputDirectory); |
58
|
|
|
|
59
|
|
|
foreach ($schema->getTables() as $table) { |
60
|
|
|
$template = $this->twig->render('table_map.php.twig', [ |
61
|
|
|
'table' => $table |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
file_put_contents($outputDirectory . DIRECTORY_SEPARATOR . $table->phpName . 'TableMap.php', $template); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|