From d8060523487cb116b8e7d938c19df6bf2ce9dd25 Mon Sep 17 00:00:00 2001 From: Patrick Brouwers Date: Fri, 22 Feb 2019 17:15:29 +0000 Subject: [PATCH] Apply fixes from StyleCI [ci skip] [skip ci] --- config/excel.php | 53 +++++++++++++++++------------- src/ChunkReader.php | 2 -- src/Excel.php | 11 +++++-- src/ExcelServiceProvider.php | 5 ++- src/Exporter.php | 5 +-- src/Factories/ReaderFactory.php | 10 +++--- src/Factories/WriterFactory.php | 10 +++--- src/Fakes/ExcelFake.php | 7 ++-- src/Files/Disk.php | 53 +++++++----------------------- src/Files/RemoteTemporaryFile.php | 4 +-- src/Files/TemporaryFile.php | 4 +-- src/Files/TemporaryFileFactory.php | 2 +- src/Helpers/FileTypeDetector.php | 4 +-- src/Jobs/ReadChunk.php | 5 ++- src/Reader.php | 7 +--- src/Sheet.php | 3 +- src/Writer.php | 8 ++--- tests/ExcelTest.php | 10 +++--- tests/QueuedExportTest.php | 10 +++--- tests/QueuedImportTest.php | 8 ++--- 20 files changed, 93 insertions(+), 128 deletions(-) diff --git a/config/excel.php b/config/excel.php index 16728b7c1..f3cd88152 100644 --- a/config/excel.php +++ b/config/excel.php @@ -3,28 +3,6 @@ use Maatwebsite\Excel\Excel; return [ - /* - |-------------------------------------------------------------------------- - | Temporary path - |-------------------------------------------------------------------------- - | - | When exporting files, we use a temporary file, before storing - | or downloading. Here you can customize that path. - | - */ - 'temp_path' => storage_path('framework/laravel-excel'), - - /* - |-------------------------------------------------------------------------- - | Remote temporary disk - |-------------------------------------------------------------------------- - | - | If not null, then temporary files for queued imports and exports will be stored - | at and retrieved from the specified filesystem disk, in addition to - | the configured temp_path on the local server. - | - */ - 'remote_temp_disk' => null, 'exports' => [ @@ -138,4 +116,35 @@ */ 'pdf' => Excel::DOMPDF, ], + + 'temporary_files' => [ + + /* + |-------------------------------------------------------------------------- + | Local Temporary Path + |-------------------------------------------------------------------------- + | + | When exporting and importing files, we use a temporary file, before + | storing reading or downloading. Here you can customize that path. + | + */ + 'local_path' => sys_get_temp_dir(), + + /* + |-------------------------------------------------------------------------- + | Remote Temporary Disk + |-------------------------------------------------------------------------- + | + | When dealing with a multi server setup with queues in which you + | cannot rely on having a shared local temporary path, you might + | want to store the temporary file on a shared disk. During the + | queue executing, we'll retrieve the temporary file from that + | location instead. When left to null, it will always use + | the local path. This setting only has effect when using + | in conjunction with queued imports and exports. + | + */ + 'remote_disk' => null, + + ], ]; diff --git a/src/ChunkReader.php b/src/ChunkReader.php index 5345fd8cf..e85a75add 100644 --- a/src/ChunkReader.php +++ b/src/ChunkReader.php @@ -5,12 +5,10 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Jobs\ReadChunk; use Maatwebsite\Excel\Jobs\QueueImport; -use Illuminate\Contracts\Bus\Dispatcher; use Maatwebsite\Excel\Concerns\WithLimit; use Maatwebsite\Excel\Files\TemporaryFile; use Illuminate\Contracts\Queue\ShouldQueue; use PhpOffice\PhpSpreadsheet\Reader\IReader; -use Maatwebsite\Excel\Helpers\FilePathHelper; use Maatwebsite\Excel\Concerns\WithProgressBar; use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithMultipleSheets; diff --git a/src/Excel.php b/src/Excel.php index ef7f0df61..03bc0bd6c 100644 --- a/src/Excel.php +++ b/src/Excel.php @@ -4,9 +4,9 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Files\Filesystem; +use Maatwebsite\Excel\Files\TemporaryFile; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\PendingDispatch; -use Maatwebsite\Excel\Files\TemporaryFile; use Maatwebsite\Excel\Helpers\FileTypeDetector; class Excel implements Exporter, Importer @@ -121,9 +121,14 @@ public function queue($export, string $filePath, string $diskName = null, string /** * {@inheritdoc} */ - public function raw($export, string $fileName, string $writerType = null) + public function raw($export, string $writerType) { - return $this->export($export, $fileName, $writerType)->contents(); + $temporaryFile = $this->writer->export($export, $writerType); + + $contents = $temporaryFile->contents(); + $temporaryFile->delete(); + + return $contents; } /** diff --git a/src/ExcelServiceProvider.php b/src/ExcelServiceProvider.php index 87bf2c5ab..7e9ba8c63 100644 --- a/src/ExcelServiceProvider.php +++ b/src/ExcelServiceProvider.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; use Maatwebsite\Excel\Files\Filesystem; -use Maatwebsite\Excel\Helpers\FilePathHelper; use Maatwebsite\Excel\Mixins\StoreCollection; use Maatwebsite\Excel\Console\ExportMakeCommand; use Maatwebsite\Excel\Console\ImportMakeCommand; @@ -44,8 +43,8 @@ public function register() $this->app->bind(TemporaryFileFactory::class, function () { return new TemporaryFileFactory( $this->app->make(Filesystem::class), - config('excel.temp_path', config('excel.exports.temp_path', storage_path('framework/laravel-excel'))), - config('excel.remote_temp_disk') + config('excel.temporary_files.local_path', config('excel.exports.temp_path', storage_path('framework/laravel-excel'))), + config('excel.temporary_files.remote_disk') ); }); diff --git a/src/Exporter.php b/src/Exporter.php index 5df8484ac..b69b4b605 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -44,12 +44,9 @@ public function queue($export, string $filePath, string $disk = null, string $wr /** * @param object $export - * @param string $filePath * @param string $writerType * - * @throws \PhpOffice\PhpSpreadsheet\Exception - * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @return string */ - public function raw($export, string $filePath, string $writerType = null); + public function raw($export, string $writerType); } diff --git a/src/Factories/ReaderFactory.php b/src/Factories/ReaderFactory.php index 6889a3823..0ed8ecf12 100644 --- a/src/Factories/ReaderFactory.php +++ b/src/Factories/ReaderFactory.php @@ -2,14 +2,14 @@ namespace Maatwebsite\Excel\Factories; -use Maatwebsite\Excel\Concerns\MapsCsvSettings; -use Maatwebsite\Excel\Concerns\WithCustomCsvSettings; -use Maatwebsite\Excel\Exceptions\NoTypeDetectedException; -use Maatwebsite\Excel\Files\TemporaryFile; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Csv; -use PhpOffice\PhpSpreadsheet\Reader\Exception; +use Maatwebsite\Excel\Files\TemporaryFile; use PhpOffice\PhpSpreadsheet\Reader\IReader; +use PhpOffice\PhpSpreadsheet\Reader\Exception; +use Maatwebsite\Excel\Concerns\MapsCsvSettings; +use Maatwebsite\Excel\Concerns\WithCustomCsvSettings; +use Maatwebsite\Excel\Exceptions\NoTypeDetectedException; class ReaderFactory { diff --git a/src/Factories/WriterFactory.php b/src/Factories/WriterFactory.php index 702cf47ca..1f7b245c7 100644 --- a/src/Factories/WriterFactory.php +++ b/src/Factories/WriterFactory.php @@ -2,14 +2,14 @@ namespace Maatwebsite\Excel\Factories; -use Maatwebsite\Excel\Concerns\MapsCsvSettings; -use Maatwebsite\Excel\Concerns\WithCharts; -use Maatwebsite\Excel\Concerns\WithCustomCsvSettings; -use Maatwebsite\Excel\Concerns\WithPreCalculateFormulas; use PhpOffice\PhpSpreadsheet\IOFactory; -use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Csv; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use Maatwebsite\Excel\Concerns\WithCharts; use PhpOffice\PhpSpreadsheet\Writer\IWriter; +use Maatwebsite\Excel\Concerns\MapsCsvSettings; +use Maatwebsite\Excel\Concerns\WithCustomCsvSettings; +use Maatwebsite\Excel\Concerns\WithPreCalculateFormulas; class WriterFactory { diff --git a/src/Fakes/ExcelFake.php b/src/Fakes/ExcelFake.php index b78216264..505d8e96d 100644 --- a/src/Fakes/ExcelFake.php +++ b/src/Fakes/ExcelFake.php @@ -82,16 +82,13 @@ public function handle() /** * @param object $export - * @param string $filePath * @param string $writerType * - * @throws \PhpOffice\PhpSpreadsheet\Exception - * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @return string */ - public function raw($export, string $filePath, string $writerType = null) + public function raw($export, string $writerType) { - return ''; + return 'RAW-CONTENTS'; } /** diff --git a/src/Files/Disk.php b/src/Files/Disk.php index 66bfab519..bbec22c25 100644 --- a/src/Files/Disk.php +++ b/src/Files/Disk.php @@ -39,6 +39,17 @@ public function __construct(Filesystem $disk, string $name = null, array $diskOp $this->diskOptions = $diskOptions; } + /** + * @param string $name + * @param array $arguments + * + * @return mixed + */ + public function __call($name, $arguments) + { + return $this->disk->{$name}(...$arguments); + } + /** * @param string $destination * @param string|resource $contents @@ -73,29 +84,6 @@ public function copy(TemporaryFile $source, string $destination): bool return $success; } - /** - * @param string $fileName - * - * @return LocalTemporaryFile - */ - public function copyToLocalTempFolder(string $fileName): LocalTemporaryFile - { - $temporaryFile = $this->getTemporaryFileFactory()->makeLocal( - $fileName - ); - - $tmpStream = fopen($temporaryFile->getLocalPath(), 'wb+'); - - stream_copy_to_stream( - $this->disk->readStream($fileName), - $tmpStream - ); - - fclose($tmpStream); - - return $temporaryFile; - } - /** * @param string $filename */ @@ -103,23 +91,4 @@ public function touch(string $filename) { $this->disk->put($filename, '', $this->diskOptions); } - - /** - * @return TemporaryFileFactory - */ - private function getTemporaryFileFactory(): TemporaryFileFactory - { - return app(TemporaryFileFactory::class); - } - - /** - * @param string $name - * @param array $arguments - * - * @return mixed - */ - public function __call($name, $arguments) - { - return $this->disk->{$name}(...$arguments); - } } diff --git a/src/Files/RemoteTemporaryFile.php b/src/Files/RemoteTemporaryFile.php index 572820fa3..c02c500a7 100644 --- a/src/Files/RemoteTemporaryFile.php +++ b/src/Files/RemoteTemporaryFile.php @@ -62,7 +62,7 @@ public function delete(): bool /** * @return TemporaryFile */ - public function fresh(): TemporaryFile + public function sync(): TemporaryFile { if (!$this->localTemporaryFile->exists()) { touch($this->localTemporaryFile->getLocalPath()); @@ -79,7 +79,7 @@ public function fresh(): TemporaryFile /** * Store on remote disk. */ - public function sync() + public function updateRemote() { $this->disk->copy( $this->localTemporaryFile, diff --git a/src/Files/TemporaryFile.php b/src/Files/TemporaryFile.php index cc3d95632..9e638fa40 100644 --- a/src/Files/TemporaryFile.php +++ b/src/Files/TemporaryFile.php @@ -39,7 +39,7 @@ abstract public function contents(): string; /** * @return TemporaryFile */ - public function fresh(): TemporaryFile + public function sync(): TemporaryFile { return $this; } @@ -63,6 +63,6 @@ public function copyFrom($filePath, string $disk = null): TemporaryFile $this->put($readStream); fclose($readStream); - return $this->fresh(); + return $this->sync(); } } diff --git a/src/Files/TemporaryFileFactory.php b/src/Files/TemporaryFileFactory.php index b2d533b27..99b2d3276 100644 --- a/src/Files/TemporaryFileFactory.php +++ b/src/Files/TemporaryFileFactory.php @@ -70,7 +70,7 @@ private function makeRemote(): RemoteTemporaryFile return new RemoteTemporaryFile( $this->filesystem->disk($this->temporaryDisk), - $this->generateFilename(), + $filename, $this->makeLocal($filename) ); } diff --git a/src/Helpers/FileTypeDetector.php b/src/Helpers/FileTypeDetector.php index 91d7833ba..613c8fed6 100644 --- a/src/Helpers/FileTypeDetector.php +++ b/src/Helpers/FileTypeDetector.php @@ -2,8 +2,8 @@ namespace Maatwebsite\Excel\Helpers; -use Maatwebsite\Excel\Exceptions\NoTypeDetectedException; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Maatwebsite\Excel\Exceptions\NoTypeDetectedException; class FileTypeDetector { @@ -51,4 +51,4 @@ public static function detectStrict(string $filePath, string $type = null): stri return $type; } -} \ No newline at end of file +} diff --git a/src/Jobs/ReadChunk.php b/src/Jobs/ReadChunk.php index 49c830aab..d21ca826f 100644 --- a/src/Jobs/ReadChunk.php +++ b/src/Jobs/ReadChunk.php @@ -2,7 +2,6 @@ namespace Maatwebsite\Excel\Jobs; -use Illuminate\Foundation\Bus\Dispatchable; use Maatwebsite\Excel\Sheet; use Illuminate\Bus\Queueable; use Illuminate\Support\Facades\DB; @@ -16,7 +15,7 @@ class ReadChunk implements ShouldQueue { - use Queueable, Dispatchable; + use Queueable; /** * @var IReader @@ -90,7 +89,7 @@ public function handle() $this->reader->setReadEmptyCells(false); $spreadsheet = $this->reader->load( - $this->temporaryFile->fresh()->getLocalPath() + $this->temporaryFile->sync()->getLocalPath() ); $sheet = Sheet::byName( diff --git a/src/Reader.php b/src/Reader.php index e8e1fe545..f44dc6165 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -5,10 +5,7 @@ use InvalidArgumentException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; -use Maatwebsite\Excel\Files\TemporaryFileFactory; use PhpOffice\PhpSpreadsheet\Cell\Cell; -use PhpOffice\PhpSpreadsheet\IOFactory; -use PhpOffice\PhpSpreadsheet\Reader\Csv; use Maatwebsite\Excel\Events\AfterImport; use PhpOffice\PhpSpreadsheet\Spreadsheet; use Maatwebsite\Excel\Concerns\WithEvents; @@ -16,14 +13,12 @@ use Maatwebsite\Excel\Files\TemporaryFile; use Illuminate\Contracts\Queue\ShouldQueue; use PhpOffice\PhpSpreadsheet\Reader\IReader; -use Maatwebsite\Excel\Helpers\FilePathHelper; use Maatwebsite\Excel\Factories\ReaderFactory; use PhpOffice\PhpSpreadsheet\Reader\Exception; -use Maatwebsite\Excel\Concerns\MapsCsvSettings; use Maatwebsite\Excel\Concerns\WithChunkReading; +use Maatwebsite\Excel\Files\TemporaryFileFactory; use Maatwebsite\Excel\Concerns\SkipsUnknownSheets; use Maatwebsite\Excel\Concerns\WithMultipleSheets; -use Maatwebsite\Excel\Concerns\WithCustomCsvSettings; use Maatwebsite\Excel\Concerns\WithCustomValueBinder; use Maatwebsite\Excel\Concerns\WithCalculatedFormulas; use Symfony\Component\HttpFoundation\File\UploadedFile; diff --git a/src/Sheet.php b/src/Sheet.php index cebe88567..77b1483b5 100644 --- a/src/Sheet.php +++ b/src/Sheet.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToArray; use Maatwebsite\Excel\Concerns\ToModel; -use Maatwebsite\Excel\Files\TemporaryFileFactory; use PhpOffice\PhpSpreadsheet\IOFactory; use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Events\AfterSheet; @@ -28,12 +27,12 @@ use Maatwebsite\Excel\Concerns\WithDrawings; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Imports\ModelImporter; -use Maatwebsite\Excel\Helpers\FilePathHelper; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\WithMappedCells; use Maatwebsite\Excel\Concerns\WithProgressBar; use Maatwebsite\Excel\Concerns\WithChunkReading; +use Maatwebsite\Excel\Files\TemporaryFileFactory; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Maatwebsite\Excel\Imports\HeadingRowExtractor; use Maatwebsite\Excel\Concerns\WithCustomChunkSize; diff --git a/src/Writer.php b/src/Writer.php index 2bae79f78..c05a12b6a 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -2,8 +2,6 @@ namespace Maatwebsite\Excel; -use Maatwebsite\Excel\Factories\WriterFactory; -use Maatwebsite\Excel\Files\RemoteTemporaryFile; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\IOFactory; use Maatwebsite\Excel\Concerns\WithTitle; @@ -12,6 +10,8 @@ use Maatwebsite\Excel\Events\BeforeExport; use Maatwebsite\Excel\Files\TemporaryFile; use Maatwebsite\Excel\Events\BeforeWriting; +use Maatwebsite\Excel\Factories\WriterFactory; +use Maatwebsite\Excel\Files\RemoteTemporaryFile; use Maatwebsite\Excel\Files\TemporaryFileFactory; use Maatwebsite\Excel\Concerns\WithMultipleSheets; use Maatwebsite\Excel\Concerns\WithCustomValueBinder; @@ -108,7 +108,7 @@ public function open($export) public function reopen(TemporaryFile $tempFile, string $writerType) { $reader = IOFactory::createReader($writerType); - $this->spreadsheet = $reader->load($tempFile->fresh()->getLocalPath()); + $this->spreadsheet = $reader->load($tempFile->sync()->getLocalPath()); return $this; } @@ -141,7 +141,7 @@ public function write($export, TemporaryFile $temporaryFile, string $writerType) ); if ($temporaryFile instanceof RemoteTemporaryFile) { - $temporaryFile->sync(); + $temporaryFile->updateRemote(); } $this->spreadsheet->disconnectWorksheets(); diff --git a/tests/ExcelTest.php b/tests/ExcelTest.php index c89213f56..069cc4d36 100644 --- a/tests/ExcelTest.php +++ b/tests/ExcelTest.php @@ -119,7 +119,7 @@ public function can_get_raw_export_contents() { $export = new EmptyExport; - $response = $this->SUT->raw($export, 'filename.xlsx'); + $response = $this->SUT->raw($export, Excel::XLSX); $this->assertNotEmpty($response); } @@ -162,11 +162,11 @@ public function collection() public function getCsvSettings(): array { return [ - 'line_ending' => PHP_EOL, - 'enclosure' => '', - 'delimiter' => ';', + 'line_ending' => PHP_EOL, + 'enclosure' => '', + 'delimiter' => ';', 'include_separator_line' => true, - 'excel_compatibility' => false, + 'excel_compatibility' => false, ]; } }; diff --git a/tests/QueuedExportTest.php b/tests/QueuedExportTest.php index 0dc924ade..783fc8d67 100644 --- a/tests/QueuedExportTest.php +++ b/tests/QueuedExportTest.php @@ -4,11 +4,9 @@ use Maatwebsite\Excel\Excel; use Illuminate\Support\Facades\Queue; -use Maatwebsite\Excel\Jobs\AppendDataToSheet; -use Maatwebsite\Excel\Jobs\QueueExport; -use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Maatwebsite\Excel\Files\TemporaryFile; +use Maatwebsite\Excel\Jobs\AppendDataToSheet; use Maatwebsite\Excel\Files\RemoteTemporaryFile; use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExport; use Maatwebsite\Excel\Tests\Data\Stubs\ShouldQueueExport; @@ -46,13 +44,13 @@ public function can_queue_an_export_and_store_on_different_disk() */ public function can_queue_export_with_remote_temp_disk() { - config()->set('excel.remote_temp_disk', 'test'); + config()->set('excel.temporary_files.remote_disk', 'test'); // Delete the local temp file before each append job // to simulate using a shared remote disk, without - // having a depending on a local temp file. + // having a dependency on a local temp file. $jobs = 0; - Queue::before(function (JobProcessing $event) use(&$jobs) { + Queue::before(function (JobProcessing $event) use (&$jobs) { if ($event->job->resolveName() === AppendDataToSheet::class) { /** @var TemporaryFile $tempFile */ $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile'); diff --git a/tests/QueuedImportTest.php b/tests/QueuedImportTest.php index 42b2b5a5e..4f868479f 100644 --- a/tests/QueuedImportTest.php +++ b/tests/QueuedImportTest.php @@ -4,7 +4,6 @@ use Illuminate\Support\Facades\Queue; use Maatwebsite\Excel\Jobs\ReadChunk; -use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Maatwebsite\Excel\Concerns\Importable; use Maatwebsite\Excel\Files\TemporaryFile; @@ -59,10 +58,11 @@ public function can_queue_an_import() */ public function can_queue_import_with_remote_temp_disk() { - config()->set('excel.remote_temp_disk', 'test'); + config()->set('excel.temporary_files.remote_disk', 'test'); - // Delete the local temp file after the QueueExport job - // to simulate the following jobs using a different filesystem. + // Delete the local temp file before each read chunk job + // to simulate using a shared remote disk, without + // having a dependency on a local temp file. Queue::before(function (JobProcessing $event) { if ($event->job->resolveName() === ReadChunk::class) { /** @var TemporaryFile $tempFile */