|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
use League\Flysystem\Filesystem; |
|
8
|
|
|
use League\Flysystem\Adapter\Local; |
|
9
|
|
|
use App\Account; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractExportCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The name and signature of the console command. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $signature = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The export folder location. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $exportFolder = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The export file name. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $exportFileName = ''; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The export extension file. |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $exportFileExt = '.txt'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The export storage disk name. |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $storageDisk = 'export'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The export storage location. |
|
59
|
|
|
* |
|
60
|
|
|
* @var Illuminate\Contracts\Filesystem\Filesystem |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $storage; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create a new command instance. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct() |
|
70
|
|
|
{ |
|
71
|
|
|
parent::__construct(); |
|
72
|
|
|
$this->storage = Storage::disk($this->storageDisk); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Execute the console command. |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract public function handle(); |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Export filtered accounts into target file. |
|
85
|
|
|
* |
|
86
|
|
|
* @param App\Account $accounts |
|
87
|
|
|
* @param string $filename |
|
88
|
|
|
* @param bool $password |
|
89
|
|
|
* @param bool $flagCI |
|
90
|
|
|
* |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
|
|
final protected function exportAccounts($accounts, $filename, $password = false, $flagCI = false) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->storage->put($filename, ''); |
|
96
|
|
|
|
|
97
|
|
|
$count = count($accounts); |
|
98
|
|
|
|
|
99
|
|
|
if ($flagCI === false) { |
|
100
|
|
|
$bar = $this->output->createProgressBar($count); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
foreach ($accounts as $account) { |
|
104
|
|
|
$record = "{$account->netlogin}"; |
|
105
|
|
|
$record .= $password ? ":{$account->netpass}" : ''; |
|
106
|
|
|
$this->storage->prepend($filename, $record); |
|
107
|
|
|
if (isset($bar)) { |
|
108
|
|
|
$bar->advance(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (isset($bar)) { |
|
113
|
|
|
$bar->finish(); |
|
114
|
|
|
$this->info("\n"); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $count; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Retrieve enabled accounts. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $filter |
|
124
|
|
|
* |
|
125
|
|
|
* @return App\Account |
|
126
|
|
|
*/ |
|
127
|
|
|
final protected function fecthAccounts($filter=null) |
|
128
|
|
|
{ |
|
129
|
|
|
$query = Account::where('status', Account::ACCOUNT_ENABLE); |
|
130
|
|
|
|
|
131
|
|
|
if(is_null($filter) === false && is_array($filter)) { |
|
132
|
|
|
$query = $query->where($filter); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $query->orderBy('netlogin', 'desc')->get(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
final protected function exportToLocation($filename) |
|
139
|
|
|
{ |
|
140
|
|
|
$source = $this->storage->path($filename); |
|
141
|
|
|
|
|
142
|
|
|
if($this->argument('output')) { |
|
143
|
|
|
$target = $this->argument('output') . '/' . $filename; |
|
144
|
|
|
|
|
145
|
|
|
$fs = new Filesystem(new Local('/')); |
|
146
|
|
|
|
|
147
|
|
|
if ($fs->has($target) ) { |
|
148
|
|
|
if ($this->option('ci')) { |
|
149
|
|
|
$fs->delete($target); |
|
150
|
|
|
} else { |
|
151
|
|
|
if ($this->confirm("Overwrite existing '$target' ?")) { |
|
152
|
|
|
$fs->delete($target); |
|
153
|
|
|
} else { |
|
154
|
|
|
throw new \Exception(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$fs->rename($source, $target); |
|
160
|
|
|
|
|
161
|
|
|
} else { |
|
162
|
|
|
$target = $source; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $target; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.