-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel Scout support #4032
Merged
Merged
Laravel Scout support #4032
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ea0a1fc
Laravel Scout support
Cellard b7d1a1b
Update run-tests.yml
Cellard 5e0e5e3
Fix run-tests.yml
Cellard 956dfc5
Add matrix.scout to run-tests.yml
Cellard dcccb33
Fix matrix.scout to run-tests.yml
Cellard 4476ea5
Scout 7,8 doesn't provides DatabaseEngine
Cellard baebe4d
Skip Scout test for L5-8
Cellard a203fc7
Fix skip Scout test for L5-8
Cellard 3416365
Code Style
Cellard 258c2ed
Code Style by StyleCI
Cellard d76aab1
Meet requested changes
Cellard 6dc8c4c
Code style
Cellard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Laravel\Scout\Builder as ScoutBuilder; | ||
use Maatwebsite\Excel\Concerns\FromQuery; | ||
use Maatwebsite\Excel\Files\TemporaryFile; | ||
use Maatwebsite\Excel\Jobs\Middleware\LocalizeJob; | ||
use Maatwebsite\Excel\Writer; | ||
|
||
class AppendPaginatedToSheet implements ShouldQueue | ||
{ | ||
use Queueable, Dispatchable, ProxyFailures, InteractsWithQueue; | ||
|
||
/** | ||
* @var TemporaryFile | ||
*/ | ||
public $temporaryFile; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $writerType; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $sheetIndex; | ||
|
||
/** | ||
* @var FromQuery | ||
*/ | ||
public $sheetExport; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $page; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $perPage; | ||
|
||
/** | ||
* @param FromQuery $sheetExport | ||
* @param TemporaryFile $temporaryFile | ||
* @param string $writerType | ||
* @param int $sheetIndex | ||
* @param int $page | ||
* @param int $perPage | ||
*/ | ||
public function __construct( | ||
FromQuery $sheetExport, | ||
TemporaryFile $temporaryFile, | ||
string $writerType, | ||
int $sheetIndex, | ||
int $page, | ||
int $perPage | ||
) { | ||
$this->sheetExport = $sheetExport; | ||
$this->temporaryFile = $temporaryFile; | ||
$this->writerType = $writerType; | ||
$this->sheetIndex = $sheetIndex; | ||
$this->page = $page; | ||
$this->perPage = $perPage; | ||
} | ||
|
||
/** | ||
* Get the middleware the job should be dispatched through. | ||
* | ||
* @return array | ||
*/ | ||
public function middleware() | ||
{ | ||
return (method_exists($this->sheetExport, 'middleware')) ? $this->sheetExport->middleware() : []; | ||
} | ||
|
||
/** | ||
* @param Writer $writer | ||
* | ||
* @throws \PhpOffice\PhpSpreadsheet\Exception | ||
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception | ||
*/ | ||
public function handle(Writer $writer) | ||
{ | ||
(new LocalizeJob($this->sheetExport))->handle($this, function () use ($writer) { | ||
$writer = $writer->reopen($this->temporaryFile, $this->writerType); | ||
|
||
$sheet = $writer->getSheetByIndex($this->sheetIndex); | ||
|
||
$sheet->appendRows($this->chunk($this->sheetExport->query()), $this->sheetExport); | ||
|
||
$writer->write($this->sheetExport, $this->temporaryFile, $this->writerType); | ||
}); | ||
} | ||
|
||
/** | ||
* @param Builder|Relation|EloquentBuilder|ScoutBuilder $query | ||
*/ | ||
protected function chunk($query) | ||
{ | ||
if ($query instanceof \Laravel\Scout\Builder) { | ||
return $query->paginate($this->perPage, 'page', $this->page)->items(); | ||
} | ||
|
||
// Fallback | ||
return $query->forPage($this->page, $this->perPage)->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Tests\Data\Stubs; | ||
|
||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Database\Query\Builder; | ||
use Laravel\Scout\Builder as ScoutBuilder; | ||
use Maatwebsite\Excel\Concerns\Exportable; | ||
use Maatwebsite\Excel\Concerns\FromQuery; | ||
use Maatwebsite\Excel\Concerns\WithCustomChunkSize; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; | ||
|
||
class FromUsersScoutExport implements FromQuery, WithCustomChunkSize | ||
{ | ||
use Exportable; | ||
|
||
/** | ||
* @return Builder|EloquentBuilder|Relation|ScoutBuilder | ||
*/ | ||
public function query() | ||
{ | ||
return new ScoutBuilder(new User, ''); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function chunkSize(): int | ||
{ | ||
return 10; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Cellard this breaks for everyone not using Laravel scout but do use laravel Excel :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't break anything for me. I'm not using Laravel Scout, everything still works like expected.
instanceof
checks can run on non-existing classes.