|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OfflineAgency\MongoAutoSync\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use OfflineAgency\MongoAutoSync\Http\Models\MDModel; |
|
9
|
|
|
|
|
10
|
|
|
class GenerateModelDocumentation extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'model-doc:generate {collection_name}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Generate the documentation of the given Model. The doc is useful for autocomplete suggestion'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new command instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return void |null |
|
38
|
|
|
* |
|
39
|
|
|
* @throws Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function handle() |
|
42
|
|
|
{ |
|
43
|
|
|
$collection_name = $this->argument('collection_name'); |
|
44
|
|
|
|
|
45
|
|
|
$modelPath = $this->getModelPathByName($collection_name); |
|
46
|
|
|
|
|
47
|
|
|
$model = $this->getModel($modelPath); |
|
48
|
|
|
|
|
49
|
|
|
$items = $model->getItems(); |
|
50
|
|
|
$relations = $model->getMongoRelation(); |
|
51
|
|
|
|
|
52
|
|
|
$output = "\n\n\n\n/**\n*\n* Plain Fields\n* \n"; |
|
53
|
|
|
$output .= "* @property string \$id\n"; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($items as $key => $item) { |
|
56
|
|
|
if (isML($item)) { |
|
57
|
|
|
$output .= '* @property array $'.$key."\n"; |
|
58
|
|
|
} else { |
|
59
|
|
|
$output .= '* @property string $'.$key."\n"; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$output .= "*\n*\n*"; |
|
64
|
|
|
|
|
65
|
|
|
if (count($relations) > 0) { |
|
66
|
|
|
$output .= " Relationships\n*\n"; |
|
67
|
|
|
foreach ($relations as $key => $relation) { |
|
68
|
|
|
$modelTarget = str_replace("App\Models\\", '', $relation['model']); |
|
69
|
|
|
|
|
70
|
|
|
$output .= '* @property '.$modelTarget.' $'.$key."\n"; |
|
71
|
|
|
} |
|
72
|
|
|
$output .= "*\n**/ \n\n\n\n\n"; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->info($output); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $collection_name |
|
80
|
|
|
* @return string |
|
81
|
|
|
* |
|
82
|
|
|
* @throws Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getModelPathByName($collection_name) |
|
85
|
|
|
{ |
|
86
|
|
|
$path = config('laravel-mongo-auto-sync.model_path'); |
|
87
|
|
|
|
|
88
|
|
|
return $this->checkOaModels($path, $collection_name); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $path |
|
93
|
|
|
* @param $collection_name |
|
94
|
|
|
* @return string |
|
95
|
|
|
* |
|
96
|
|
|
* @throws Exception |
|
97
|
|
|
*/ |
|
98
|
|
|
public function checkOaModels($path, $collection_name) |
|
99
|
|
|
{ |
|
100
|
|
|
$out = ''; |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
|
|
$results = scandir($path); |
|
104
|
|
|
} catch (Exception $e) { |
|
105
|
|
|
throw new Exception('Error directory '.config('laravel-mongo-auto-sync.model_path').' not found'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
foreach ($results as $result) { |
|
109
|
|
|
if ($result === '.' or $result === '..') { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
$filename = $path.'/'.$result; |
|
113
|
|
|
if (is_dir($filename)) { |
|
114
|
|
|
$out = $this->checkOaModels($filename, $collection_name); |
|
115
|
|
|
} elseif (strtolower(substr($result, 0, -4)) == strtolower($collection_name)) { |
|
116
|
|
|
return config('laravel-mongo-auto-sync.model_namespace').'\\'.substr($result, 0, -4); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
foreach (config('laravel-mongo-auto-sync.other_models') as $key => $values) { |
|
120
|
|
|
if (strtolower($collection_name) == $key) { |
|
121
|
|
|
return $values['model_namespace'].'\\'.Str::ucfirst($key); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $out; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $modelPath |
|
130
|
|
|
* @return MDModel |
|
131
|
|
|
* |
|
132
|
|
|
* @throws Exception |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getModel(string $modelPath) |
|
135
|
|
|
{ |
|
136
|
|
|
if (class_exists($modelPath)) { |
|
137
|
|
|
return new $modelPath; |
|
138
|
|
|
} else { |
|
139
|
|
|
throw new Exception('Error '.$this->argument('collection_name').' Model not found'); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|