1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Prateekkarki\Laragen\Generators; |
4
|
|
|
use Prateekkarki\Laragen\Models\Module; |
5
|
|
|
use Prateekkarki\Laragen\Models\FileSystem; |
6
|
|
|
|
7
|
|
|
class BaseGenerator |
8
|
|
|
{ |
9
|
|
|
protected $module; |
10
|
|
|
protected $fileSystem; |
11
|
|
|
|
12
|
|
|
public function __construct(Module $module) |
13
|
|
|
{ |
14
|
|
|
$this->setModule($module); |
15
|
|
|
$this->fileSystem = new FileSystem(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getModule() |
19
|
|
|
{ |
20
|
|
|
return $this->module; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setModule(Module $module) |
24
|
|
|
{ |
25
|
|
|
$this->module = $module; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getStub($type) |
29
|
|
|
{ |
30
|
|
|
return $this->sanitize(file_get_contents(__DIR__."/../resources/stubs/".$type.".stub")); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function sanitize($string) |
34
|
|
|
{ |
35
|
|
|
return str_replace("\r", '', $string); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getPath($path) |
39
|
|
|
{ |
40
|
|
|
$dir = base_path($path); |
41
|
|
|
|
42
|
|
|
if (!is_dir($dir)) |
43
|
|
|
$this->fileSystem->mkdir($dir, 0755); |
44
|
|
|
|
45
|
|
|
return $dir; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function moduleToModelName($moduleName) |
49
|
|
|
{ |
50
|
|
|
return ucfirst(camel_case(str_singular($moduleName))); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function initializeFile($fullFilePath, $stub, $initializeWithText = false) { |
54
|
|
|
if(file_exists($fullFilePath)){ |
55
|
|
|
$this->fileSystem->remove($fullFilePath); |
56
|
|
|
} |
57
|
|
|
$seederTemplate = ($initializeWithText===false) ? $this->buildTemplate($stub) : $initializeWithText; |
58
|
|
|
$this->fileSystem->dumpFile($fullFilePath, $seederTemplate); |
59
|
|
|
return $fullFilePath; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function initializeFiles($fileMaps = []) { |
63
|
|
|
foreach ($fileMaps as $file => $stub) { |
64
|
|
|
$this->initializeFile($file, $stub); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function buildTemplate($stub, $replacements = []) |
69
|
|
|
{ |
70
|
|
|
return str_replace(array_keys($replacements), array_values($replacements), $this->getStub($stub)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function updateFile($file, $replacements) |
74
|
|
|
{ |
75
|
|
|
return str_replace(array_keys($replacements), array_values($replacements), file_get_contents($file)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function insertIntoFile($file_path, $insert_marker, $text, $after = true) { |
79
|
|
|
$contents = str_replace("\r",'', file_get_contents($file_path)); |
80
|
|
|
$new_contents = ($after) ? str_replace($insert_marker, $insert_marker.$text, $contents) : str_replace($insert_marker, $text.$insert_marker, $contents); |
81
|
|
|
$this->fileSystem->dumpFile($file_path, $new_contents); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
public function getTabs($number) |
86
|
|
|
{ |
87
|
|
|
$schema = ""; |
88
|
|
|
for ($i = 0; $i < $number; $i++) { |
89
|
|
|
$schema .= " "; |
90
|
|
|
} |
91
|
|
|
return $schema; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|