|
1
|
|
|
<?php |
|
2
|
|
|
namespace Prateekkarki\Laragen\Models; |
|
3
|
|
|
use Prateekkarki\Laragen\Models\Module; |
|
4
|
|
|
|
|
5
|
|
|
class LaragenOptions |
|
6
|
|
|
{ |
|
7
|
|
|
protected $modules; |
|
8
|
|
|
protected $options; |
|
9
|
|
|
|
|
10
|
|
|
private static $instance; |
|
11
|
|
|
|
|
12
|
|
|
private function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->options = config('laragen.options'); |
|
15
|
|
|
|
|
16
|
|
|
$this->modules = []; |
|
17
|
|
|
foreach ($this->getValidatedModules() as $moduleName => $moduleData) { |
|
18
|
|
|
$this->modules = array_merge($this->modules, $this->getModulesRecursive($moduleName, $moduleData)); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function getValidatedModules() |
|
23
|
|
|
{ |
|
24
|
|
|
// Validate laragen.modules |
|
25
|
|
|
return config('laragen.modules'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function getInstance() |
|
29
|
|
|
{ |
|
30
|
|
|
if (!isset(self::$instance)) { |
|
31
|
|
|
self::$instance = new LaragenOptions(); |
|
32
|
|
|
} |
|
33
|
|
|
return self::$instance; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getOptions() { |
|
37
|
|
|
return $this->options; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getOption($option) { |
|
41
|
|
|
return $this->options[$option] ?? null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getModules() { |
|
45
|
|
|
return $this->modules; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getModule($name) { |
|
49
|
|
|
return isset($this->modules[$name]) ? $this->modules[$name] : false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getModulesRecursive($moduleName, $moduleData) { |
|
53
|
|
|
$modules = []; |
|
54
|
|
|
$module = new Module($moduleName, $moduleData); |
|
55
|
|
|
$childColumns = $module->getFilteredColumns(['isMultipleType', 'hasMultipleFiles']); |
|
56
|
|
|
$modules[$moduleName] = $module; |
|
57
|
|
|
foreach ($childColumns as $childColumn) { |
|
58
|
|
|
$modules = array_merge($modules, $this->getModulesRecursive($childColumn->getPivotTable(), $childColumn->getLaragenColumns())); |
|
59
|
|
|
} |
|
60
|
|
|
return $modules; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getGenerators() { |
|
64
|
|
|
return $this->configToGenerators($this->options['items_to_generate']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function generatorExists($str) { |
|
68
|
|
|
foreach ($this->getGenerators() as $generator) { |
|
69
|
|
|
if (substr($generator, -strlen($str)) === $str) { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function configToGenerators($array) { |
|
77
|
|
|
$generators = []; |
|
78
|
|
|
foreach ($array as $ns => $items) { |
|
79
|
|
|
foreach ($items as $item) { |
|
80
|
|
|
$generators[] = "\\Prateekkarki\\Laragen\\Generators\\".$ns."\\".$item; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $generators; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|