Skip to content

Commit

Permalink
Add AfterChunk event for chunked exports (#4037)
Browse files Browse the repository at this point in the history
* Update AppendQueryToSheet.php

Add AfterChunk event for chunked exports

* Update AppendQueryToSheet.php

* Update AppendQueryToSheet.php

* Update AppendQueryToSheet.php

* Add test

* Fix style

* Fix style

* Cleanup
  • Loading branch information
sebestenyb authored Jan 16, 2024
1 parent 0cbbdb7 commit 104df8a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Jobs/AppendQueryToSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterChunk;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\HasEventBus;
use Maatwebsite\Excel\Jobs\Middleware\LocalizeJob;
use Maatwebsite\Excel\Writer;

class AppendQueryToSheet implements ShouldQueue
{
use Queueable, Dispatchable, ProxyFailures, InteractsWithQueue;
use Queueable, Dispatchable, ProxyFailures, InteractsWithQueue, HasEventBus;

/**
* @var TemporaryFile
Expand Down Expand Up @@ -88,6 +91,10 @@ public function middleware()
public function handle(Writer $writer)
{
(new LocalizeJob($this->sheetExport))->handle($this, function () use ($writer) {
if ($this->sheetExport instanceof WithEvents) {
$this->registerListeners($this->sheetExport->registerEvents());
}

$writer = $writer->reopen($this->temporaryFile, $this->writerType);

$sheet = $writer->getSheetByIndex($this->sheetIndex);
Expand All @@ -97,6 +104,9 @@ public function handle(Writer $writer)
$sheet->appendRows($query->get(), $this->sheetExport);

$writer->write($this->sheetExport, $this->temporaryFile, $this->writerType);

$this->raise(new AfterChunk($sheet, $this->sheetExport, ($this->page - 1) * $this->chunkSize));
$this->clearListeners();
});
}
}
22 changes: 22 additions & 0 deletions tests/Concerns/WithEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Maatwebsite\Excel\Tests\Concerns;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Events\AfterBatch;
use Maatwebsite\Excel\Events\AfterChunk;
Expand All @@ -17,7 +19,9 @@
use Maatwebsite\Excel\Tests\Data\Stubs\BeforeExportListener;
use Maatwebsite\Excel\Tests\Data\Stubs\CustomConcern;
use Maatwebsite\Excel\Tests\Data\Stubs\CustomSheetConcern;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
use Maatwebsite\Excel\Tests\Data\Stubs\ExportWithEvents;
use Maatwebsite\Excel\Tests\Data\Stubs\ExportWithEventsChunks;
use Maatwebsite\Excel\Tests\Data\Stubs\ImportWithEvents;
use Maatwebsite\Excel\Tests\Data\Stubs\ImportWithEventsChunksAndBatches;
use Maatwebsite\Excel\Tests\TestCase;
Expand All @@ -26,6 +30,8 @@

class WithEventsTest extends TestCase
{
use WithFaker;

/**
* @test
*/
Expand Down Expand Up @@ -63,6 +69,22 @@ public function export_events_get_called()
$this->assertEquals(4, $eventsTriggered);
}

/**
* @test
*/
public function export_chunked_events_get_called()
{
$this->loadLaravelMigrations(['--database' => 'testing']);
User::query()->create([
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => Str::random(10),
]);
$export = new ExportWithEventsChunks();
$export->queue('filename.xlsx');
}

/**
* @test
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/Data/Stubs/ExportWithEventsChunks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Maatwebsite\Excel\Tests\Data\Stubs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterChunk;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
use PHPUnit\Framework\Assert;

class ExportWithEventsChunks implements WithEvents, FromQuery, ShouldQueue, WithCustomChunkSize
{
use Exportable;

public function registerEvents(): array
{
return [
AfterChunk::class => function (AfterChunk $event) {
Assert::assertInstanceOf(ExportWithEventsChunks::class, $event->getConcernable());
},
];
}

public function query(): Builder
{
return User::query();
}

public function chunkSize(): int
{
return 1;
}
}

0 comments on commit 104df8a

Please sign in to comment.