|
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 DropCollection extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'drop:collection {collection_name}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Drop all elements of the collection given as input'; |
|
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
|
|
|
if (! is_null($model)) { |
|
50
|
|
|
$model = $model->all(); |
|
51
|
|
|
|
|
52
|
|
|
$count = $model->count(); |
|
53
|
|
|
$bar = $this->output->createProgressBar($count); |
|
54
|
|
|
|
|
55
|
|
|
if ($count > 0) { |
|
56
|
|
|
for ($i = 0; $i <= $count - 1; $i++) { |
|
57
|
|
|
$bar->advance(); |
|
58
|
|
|
$model[$i]->destroyWithSync(); |
|
59
|
|
|
$this->line($i + 1 .') Destroy item document with id #'.$model[$i]->getId()); |
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->warn('No record found on collection '.strtolower($collection_name)); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
|
|
$this->error('Error Model not found \n'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $collection_name |
|
71
|
|
|
* @return string |
|
72
|
|
|
* |
|
73
|
|
|
* @throws Exception |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getModelPathByName($collection_name) |
|
76
|
|
|
{ |
|
77
|
|
|
$path = config('laravel-mongo-auto-sync.model_path'); |
|
78
|
|
|
|
|
79
|
|
|
return $this->checkOaModels($path, $collection_name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $path |
|
84
|
|
|
* @param $collection_name |
|
85
|
|
|
* @return string |
|
86
|
|
|
* |
|
87
|
|
|
* @throws Exception |
|
88
|
|
|
*/ |
|
89
|
|
|
public function checkOaModels($path, $collection_name) |
|
90
|
|
|
{ |
|
91
|
|
|
$out = ''; |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
$results = scandir($path); |
|
95
|
|
|
} catch (Exception $e) { |
|
96
|
|
|
throw new Exception('Error directory '.config('laravel-mongo-auto-sync.model_path').' not found'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
foreach ($results as $result) { |
|
100
|
|
|
if ($result === '.' or $result === '..') { |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
$filename = $path.'/'.$result; |
|
104
|
|
|
if (is_dir($filename)) { |
|
105
|
|
|
$out = $this->checkOaModels($filename, $collection_name); |
|
106
|
|
|
} elseif (strtolower(substr($result, 0, -4)) == strtolower($collection_name)) { |
|
107
|
|
|
return config('laravel-mongo-auto-sync.model_namespace').'\\'.substr($result, 0, -4); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach (config('laravel-mongo-auto-sync.other_models') as $key => $values) { |
|
112
|
|
|
if (strtolower($collection_name) == $key) { |
|
113
|
|
|
return $values['model_namespace'].'\\'.Str::ucfirst($key); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $out; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $modelPath |
|
122
|
|
|
* @return MDModel |
|
123
|
|
|
* |
|
124
|
|
|
* @throws Exception |
|
125
|
|
|
*/ |
|
126
|
|
|
private function getModel(string $modelPath) |
|
127
|
|
|
{ |
|
128
|
|
|
if (class_exists($modelPath)) { |
|
129
|
|
|
return new $modelPath; |
|
130
|
|
|
} else { |
|
131
|
|
|
throw new Exception('Error '.$this->argument('collection_name').' Model not found'); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|