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\Database; |
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 DatabaseGenerator extends Generator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $outputPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Twig_Environment |
30
|
|
|
*/ |
31
|
|
|
protected $twig; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $outputPath |
36
|
|
|
* @param array $templateDirs |
37
|
|
|
*/ |
38
|
|
|
public function __construct($outputPath, array $templateDirs = []) |
39
|
|
|
{ |
40
|
|
|
$this->outputPath = $outputPath; |
41
|
|
|
|
42
|
|
|
$loader = new \Twig_Loader_Filesystem(array_merge($templateDirs, [ |
43
|
|
|
__DIR__ . '/../Resources/Skeletons/Database' |
44
|
|
|
])); |
45
|
|
|
$loader->addPath(__DIR__ . '/../Resources/Skeletons/Database/Driver/SQLite', 'sqlite'); |
46
|
|
|
|
47
|
|
|
$this->twig = new \Twig_Environment($loader, [ |
48
|
|
|
'cache' => false, |
49
|
|
|
'strict_variables' => true |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Schema $schema |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function generate(Schema $schema) |
60
|
|
|
{ |
61
|
|
|
$this->createDirectory($this->outputPath); |
62
|
|
|
|
63
|
|
|
// Allow overriding template for a given driver |
64
|
|
|
$driver = Rocket::getConnectionDriver($schema->connection); |
65
|
|
|
|
66
|
|
|
$template = $this->twig->resolveTemplate(['@' . $driver . '/schema.sql.twig', 'schema.sql.twig'])->render([ |
67
|
|
|
'schema' => $schema, |
68
|
|
|
'driver' => $driver |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
file_put_contents($this->outputPath . DIRECTORY_SEPARATOR . $schema->database . '.sql', $template); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|