-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathTemplater.php
More file actions
52 lines (44 loc) · 1.6 KB
/
Templater.php
File metadata and controls
52 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php namespace DBDiff;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\Filesystem\Filesystem;
class Templater {
function __construct($params, $up, $down) {
$this->params = $params;
$this->up = $up;
$this->down = $down;
}
public function output() {
$content = $this->getComments();
$content .= $this->getContent();
if (is_null($this->params->output)) {
Logger::info("Writing migration file to ".getcwd()."/migration.sql");
file_put_contents('migration.sql', $content);
} else {
Logger::info("Writing migration file to ".$this->params->output);
return file_put_contents($this->params->output, $content);
}
}
private function getComments() {
if (!$this->params->nocomments) {
return "# Generated by DBDiff\n# On ".date('m/d/Y h:i:s a', time())."\n\n";
}
return "";
}
private function getContent() {
$compiler = new BladeCompiler(new Filesystem, ".");
$template = $this->getTemplate();
$compiled = $compiler->compileString(' ?>'.$template);
$up = trim($this->up, "\n");
$down = trim($this->down, "\n");
ob_start();
eval($compiled);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
private function getTemplate() {
if (file_exists($this->params->template))
return file_get_contents($this->params->template);
return "#---------- UP ----------\n{!! \$up !!}\n#---------- DOWN ----------\n{!! \$down !!}";
}
}