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 | * @throws Exception |
||
39 | */ |
||
40 | public function handle() |
||
41 | { |
||
42 | $collection_name = $this->argument('collection_name'); |
||
43 | |||
44 | $modelPath = $this->getModelPathByName($collection_name); |
||
45 | |||
46 | $model = $this->getModel($modelPath); |
||
47 | |||
48 | $items = $model->getItems(); |
||
49 | $relations = $model->getMongoRelation(); |
||
50 | |||
51 | $output = "\n\n\n\n/**\n*\n* Plain Fields\n* \n"; |
||
52 | $output .= "* @property string \$id\n"; |
||
53 | |||
54 | foreach ($items as $key => $item) { |
||
55 | if (isML($item)) { |
||
56 | $output .= '* @property array $'.$key."\n"; |
||
57 | } else { |
||
58 | $output .= '* @property string $'.$key."\n"; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | $output .= "*\n*\n*"; |
||
63 | |||
64 | if (count($relations) > 0) { |
||
65 | $output .= " Relationships\n*\n"; |
||
66 | foreach ($relations as $key => $relation) { |
||
67 | $modelTarget = str_replace("App\Models\\", '', $relation['model']); |
||
68 | |||
69 | $output .= '* @property '.$modelTarget.' $'.$key."\n"; |
||
70 | } |
||
71 | $output .= "*\n**/ \n\n\n\n\n"; |
||
72 | } |
||
73 | |||
74 | $this->info($output); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $collection_name |
||
79 | * @return string |
||
80 | * @throws Exception |
||
81 | */ |
||
82 | public function getModelPathByName($collection_name) |
||
83 | { |
||
84 | $path = config('laravel-mongo-auto-sync.model_path'); |
||
85 | |||
86 | return $this->checkOaModels($path, $collection_name); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param $path |
||
91 | * @param $collection_name |
||
92 | * @return string |
||
93 | * @throws Exception |
||
94 | */ |
||
95 | public function checkOaModels($path, $collection_name) |
||
96 | { |
||
97 | $out = ''; |
||
98 | |||
99 | try { |
||
100 | $results = scandir($path); |
||
101 | } catch (Exception $e) { |
||
102 | throw new Exception('Error directory '.config('laravel-mongo-auto-sync.model_path').' not found'); |
||
103 | } |
||
104 | |||
105 | foreach ($results as $result) { |
||
106 | if ($result === '.' or $result === '..') { |
||
107 | continue; |
||
108 | } |
||
109 | $filename = $path.'/'.$result; |
||
110 | if (is_dir($filename)) { |
||
111 | $out = $this->checkOaModels($filename, $collection_name); |
||
112 | } elseif (strtolower(substr($result, 0, -4)) == strtolower($collection_name)) { |
||
113 | return config('laravel-mongo-auto-sync.model_namespace').'\\'.substr($result, 0, -4); |
||
114 | } |
||
115 | } |
||
116 | foreach (config('laravel-mongo-auto-sync.other_models') as $key => $values) { |
||
117 | if (strtolower($collection_name) == $key) { |
||
118 | return $values['model_namespace'].'\\'.Str::ucfirst($key); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $out; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param string $modelPath |
||
127 | * @return MDModel |
||
128 | * @throws Exception |
||
129 | */ |
||
130 | private function getModel(string $modelPath) |
||
131 | { |
||
132 | if (class_exists($modelPath)) { |
||
133 | return new $modelPath; |
||
134 | } else { |
||
135 | throw new Exception('Error '.$this->argument('collection_name').' Model not found'); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
136 | } |
||
137 | } |
||
138 | } |
||
139 |