forked from SpartnerNL/Laravel-Excel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueuedWriter.php
214 lines (189 loc) · 6.16 KB
/
QueuedWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Maatwebsite\Excel;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Concerns\WithCustomQuerySize;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\Files\TemporaryFileFactory;
use Maatwebsite\Excel\Jobs\AppendDataToSheet;
use Maatwebsite\Excel\Jobs\AppendQueryToSheet;
use Maatwebsite\Excel\Jobs\AppendViewToSheet;
use Maatwebsite\Excel\Jobs\CloseSheet;
use Maatwebsite\Excel\Jobs\QueueExport;
use Maatwebsite\Excel\Jobs\StoreQueuedExport;
use Traversable;
class QueuedWriter
{
/**
* @var Writer
*/
protected $writer;
/**
* @var int
*/
protected $chunkSize;
/**
* @var TemporaryFileFactory
*/
protected $temporaryFileFactory;
/**
* @param Writer $writer
* @param TemporaryFileFactory $temporaryFileFactory
*/
public function __construct(Writer $writer, TemporaryFileFactory $temporaryFileFactory)
{
$this->writer = $writer;
$this->chunkSize = config('excel.exports.chunk_size', 1000);
$this->temporaryFileFactory = $temporaryFileFactory;
}
/**
* @param object $export
* @param string $filePath
* @param string $disk
* @param string|null $writerType
* @param array|string $diskOptions
*
* @return \Illuminate\Foundation\Bus\PendingDispatch
*/
public function store($export, string $filePath, string $disk = null, string $writerType = null, $diskOptions = [])
{
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
$temporaryFile = $this->temporaryFileFactory->make($extension);
$jobs = $this->buildExportJobs($export, $temporaryFile, $writerType);
$jobs->push(new StoreQueuedExport(
$temporaryFile,
$filePath,
$disk,
$diskOptions
));
return new PendingDispatch(
(new QueueExport($export, $temporaryFile, $writerType))->chain($jobs->toArray())
);
}
/**
* @param object $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
*
* @return Collection
*/
private function buildExportJobs($export, TemporaryFile $temporaryFile, string $writerType): Collection
{
$sheetExports = [$export];
if ($export instanceof WithMultipleSheets) {
$sheetExports = $export->sheets();
}
$jobs = new Collection;
foreach ($sheetExports as $sheetIndex => $sheetExport) {
if ($sheetExport instanceof FromCollection) {
$jobs = $jobs->merge($this->exportCollection($sheetExport, $temporaryFile, $writerType, $sheetIndex));
} elseif ($sheetExport instanceof FromQuery) {
$jobs = $jobs->merge($this->exportQuery($sheetExport, $temporaryFile, $writerType, $sheetIndex));
} elseif ($sheetExport instanceof FromView) {
$jobs = $jobs->merge($this->exportView($sheetExport, $temporaryFile, $writerType, $sheetIndex));
}
$jobs->push(new CloseSheet($sheetExport, $temporaryFile, $writerType, $sheetIndex));
}
return $jobs;
}
/**
* @param FromCollection $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
* @param int $sheetIndex
*
* @return Collection
*/
private function exportCollection(
FromCollection $export,
TemporaryFile $temporaryFile,
string $writerType,
int $sheetIndex
): Collection {
return $export
->collection()
->chunk($this->getChunkSize($export))
->map(function ($rows) use ($writerType, $temporaryFile, $sheetIndex, $export) {
if ($rows instanceof Traversable) {
$rows = iterator_to_array($rows);
}
return new AppendDataToSheet(
$export,
$temporaryFile,
$writerType,
$sheetIndex,
$rows
);
});
}
/**
* @param FromQuery $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
* @param int $sheetIndex
*
* @return Collection
*/
private function exportQuery(
FromQuery $export,
TemporaryFile $temporaryFile,
string $writerType,
int $sheetIndex
): Collection {
$query = $export->query();
$count = $export instanceof WithCustomQuerySize ? $export->querySize() : $query->count();
$spins = ceil($count / $this->getChunkSize($export));
$jobs = new Collection();
for ($page = 1; $page <= $spins; $page++) {
$jobs->push(new AppendQueryToSheet(
$export,
$temporaryFile,
$writerType,
$sheetIndex,
$page,
$this->getChunkSize($export)
));
}
return $jobs;
}
/**
* @param FromView $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
* @param int $sheetIndex
*
* @return Collection
*/
private function exportView(
FromView $export,
TemporaryFile $temporaryFile,
string $writerType,
int $sheetIndex
): Collection {
$jobs = new Collection();
$jobs->push(new AppendViewToSheet(
$export,
$temporaryFile,
$writerType,
$sheetIndex
));
return $jobs;
}
/**
* @param object|WithCustomChunkSize $export
*
* @return int
*/
private function getChunkSize($export): int
{
if ($export instanceof WithCustomChunkSize) {
return $export->chunkSize();
}
return $this->chunkSize;
}
}