|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PrismX\Generators\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractGenerator |
|
9
|
|
|
{ |
|
10
|
|
|
const INDENT = ' '; |
|
11
|
|
|
|
|
12
|
|
|
/* @var \PrismX\Generators\Support\Model */ |
|
13
|
|
|
protected $model; |
|
14
|
|
|
|
|
15
|
|
|
protected $stub; |
|
16
|
|
|
|
|
17
|
|
|
abstract protected function getpath(): string; |
|
18
|
|
|
|
|
19
|
|
|
abstract public function populateStub(): string; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Model $model) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->model = $model; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setCache($key, $contents) |
|
27
|
|
|
{ |
|
28
|
|
|
Storage::put('generators/cache/'.md5($key), $contents); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getCache($key) |
|
32
|
|
|
{ |
|
33
|
|
|
$file = 'generators/cache/'.md5($key); |
|
34
|
|
|
|
|
35
|
|
|
return Storage::exists($file) ? Storage::get($file) : null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function run() |
|
39
|
|
|
{ |
|
40
|
|
|
$parsedStub = $this->populateStub(); |
|
41
|
|
|
$cached = $this->getCache($this->getPath()); |
|
42
|
|
|
$currentFile = null; |
|
43
|
|
|
|
|
44
|
|
|
if (File::exists($this->getpath())) { |
|
45
|
|
|
$currentFile = File::get($this->getPath()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($currentFile === $parsedStub || $cached === $parsedStub) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (! $cached || ! File::exists($this->getpath())) { |
|
53
|
|
|
File::put($this->getPath(), $parsedStub); |
|
54
|
|
|
$this->setCache($this->getPath(), $parsedStub); |
|
55
|
|
|
|
|
56
|
|
|
return "{$this->getType()} created successfully. <comment>[{$this->getPath()}]</comment>"; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($currentFile === $cached) { |
|
60
|
|
|
File::put($this->getPath(), $parsedStub); |
|
61
|
|
|
$this->setCache($this->getPath(), $parsedStub); |
|
62
|
|
|
|
|
63
|
|
|
return "{$this->getType()} updated successfully. <comment>[{$this->getPath()}]</comment>"; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return "<fg=black;bg=yellow> {$this->getType()} was manually changed. </> <comment>[skipped]</comment>"; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getType() |
|
70
|
|
|
{ |
|
71
|
|
|
$class = (new \ReflectionClass($this))->getShortName(); |
|
72
|
|
|
|
|
73
|
|
|
return str_replace('Generator', '', $class); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|