/
ChunkedReadJob.php
114 lines (94 loc) 路 2.51 KB
/
ChunkedReadJob.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
<?php
namespace Maatwebsite\Excel\Readers;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Filters\ChunkReadFilter;
use SuperClosure\Serializer;
class ChunkedReadJob implements SelfHandling, ShouldQueue
{
/**
* @var int
*/
private $startRow;
/**
* @var callable
*/
private $callback;
/**
* @var
*/
private $chunkSize;
/**
* @var
*/
private $startIndex;
/**
* @var
*/
private $file;
/**
* @var null
*/
private $sheets;
/**
* @var bool
*/
private $shouldQueue;
/**
* ChunkedReadJob constructor.
*
* @param $file
* @param null $sheets
* @param int $startRow
* @param $startIndex
* @param $chunkSize
* @param callable $callback
* @param bool $shouldQueue
*/
public function __construct(
$file,
$sheets = null,
$startRow,
$startIndex,
$chunkSize,
callable $callback,
$shouldQueue = true
) {
$this->startRow = $startRow;
$this->chunkSize = $chunkSize;
$this->startIndex = $startIndex;
$this->file = $file;
$this->callback = $shouldQueue ? (new Serializer)->serialize($callback) : $callback;
$this->sheets = $sheets;
$this->shouldQueue = $shouldQueue;
}
/***
* Handle the read job
*/
public function handle()
{
$reader = app('excel.reader');
$reader->injectExcel(app('phpexcel'));
$reader->_init($this->file);
$filter = new ChunkReadFilter();
$reader->reader->setLoadSheetsOnly($this->sheets);
$reader->reader->setReadFilter($filter);
$reader->reader->setReadDataOnly(true);
// Set the rows for the chunking
$filter->setRows($this->startRow, $this->chunkSize);
// Load file with chunk filter enabled
$reader->excel = $reader->reader->load($this->file);
// Slice the results
$results = $reader->get()->slice($this->startIndex, $this->chunkSize);
$callback = $this->shouldQueue ? (new Serializer)->unserialize($this->callback) : $this->callback;
// Do a callback
if (is_callable($callback)) {
$break = call_user_func($callback, $results);
}
$reader->_reset();
unset($reader, $results);
if ($break) {
return true;
}
}
}