Skip to content

Cake 5 Update #18

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CakePHP Excel plugin versions 0.4.0 and lower used now abandoned [PHPExcel](http
composer require robotusers/cakephp-excel
```

CakePHP 3.6+
CakePHP 5.0+

```php
//Application.php
Expand Down Expand Up @@ -193,9 +193,9 @@ public function initialize()
If you want to load data into your table you have to set a worksheet instance.

```php
use Cake\Filesystem\File;
use SplFileInfo;

$file = new File('path/to/file.xls');
$file = new SplFileInfo('path/to/file.xls');
$spreadsheet = $table->getManager()->getSpreadsheet($file); // \PhpOffice\PhpSpreadsheet\Spreadsheet instance
$worksheet = $spreadsheet->getActiveSheet(); // \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet instance

Expand All @@ -220,7 +220,7 @@ use Robotusers\Excel\Excel\Manager;
$table = TableRegistry::get('SomeTable');
$manager = new Manager();

$file = new File('file.xlsx');
$file = new SplFileInfo('file.xlsx');
$spreadsheet = $manager->getSpreadsheet($file);
$worksheet = $spreadsheet->getActiveSheet();

Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"php": "^7.2|^8.0",
"phpoffice/phpspreadsheet": "^1.1",
"cakephp/filesystem": "~4.0",
"cakephp/orm": "~4.0"
"php": ">=8.1",
"phpoffice/phpspreadsheet": "^2.0",
"cakephp/orm": "~5.0"
},
"require-dev": {
"cakephp/cakephp": "~4.0",
"cakephp/cakephp": "~5.0",
"cakephp/cakephp-codesniffer": "*",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.9"
Expand Down
22 changes: 11 additions & 11 deletions src/Excel/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace Robotusers\Excel\Excel;

use Cake\Datasource\EntityInterface;
use Cake\Filesystem\File;
use Cake\ORM\Table;
use DateTimeInterface;
use InvalidArgumentException;
Expand All @@ -43,6 +42,7 @@
use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use UnexpectedValueException;
use SplFileInfo;

/**
* Description of Manager
Expand Down Expand Up @@ -292,7 +292,7 @@ protected function setCellValue(Cell $cell, $value)
* @return Spreadsheet
* @deprecated 0.5.0 Use `getSpreadsheet()` instead.
*/
public function getExcel(File $file, array $options = [])
public function getExcel(SplFileInfo $file, array $options = [])
{
return $this->getSpreadsheet($file, $options);
}
Expand All @@ -302,11 +302,11 @@ public function getExcel(File $file, array $options = [])
* @param array $options
* @return Spreadsheet
*/
public function getSpreadsheet(File $file, array $options = [])
public function getSpreadsheet(SplFileInfo $file, array $options = [])
{
$reader = $this->getReader($file, $options);

return $reader->load($file->pwd());
return $reader->load($file->getRealPath());
}

/**
Expand All @@ -316,14 +316,14 @@ public function getSpreadsheet(File $file, array $options = [])
* @return IReader
* @throws InvalidArgumentException
*/
public function getReader(File $file, array $options = [])
public function getReader(SplFileInfo $file, array $options = [])
{
if (!$file->exists()) {
$message = sprintf('File %s does not exist.', $file->name());
if (!$file->getSize()) {
$message = sprintf('File %s does not exist.', $file->getBasename());
throw new InvalidArgumentException($message);
}

$reader = IOFactory::createReaderForFile($file->pwd());
$reader = IOFactory::createReaderForFile($file->getRealPath());

if ($reader instanceof CsvReader) {
if (isset($options['delimiter'])) {
Expand Down Expand Up @@ -354,14 +354,14 @@ public function getReader(File $file, array $options = [])
public function getWriter(Spreadsheet $excel, File $file, array $options = [])
{
if (!$file->exists()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be renamed to getSize() as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, missed that.

$message = sprintf('File %s does not exist.', $file->name());
$message = sprintf('File %s does not exist.', $file->getBasename());
throw new InvalidArgumentException($message);
}

if (isset($options['writerType'])) {
$type = $options['writerType'];
} else {
$type = IOFactory::identify($file->pwd());
$type = IOFactory::identify($file->getRealPath());
}
$writer = IOFactory::createWriter($excel, $type);

Expand Down Expand Up @@ -393,7 +393,7 @@ public function getWriter(Spreadsheet $excel, File $file, array $options = [])
public function save(Spreadsheet $excel, File $file, array $options = [])
{
$writer = $this->getWriter($excel, $file, $options);
$writer->save($file->pwd());
$writer->save($file->getRealPath());

return $file;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Model/Behavior/ExcelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace Robotusers\Excel\Model\Behavior;

use Cake\Datasource\EntityInterface;
use Cake\Filesystem\File;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use InvalidArgumentException;
Expand All @@ -35,6 +34,7 @@
use Robotusers\Excel\Excel\Manager;
use Robotusers\Excel\Traits\DiscoverWorksheetTrait;
use RuntimeException;
use SplFileInfo;

/**
* Description of ExcelBehavior
Expand Down Expand Up @@ -68,7 +68,7 @@ class ExcelBehavior extends Behavior
*
* @var array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'startRow' => 1,
'endRow' => null,
'startColumn' => 'A',
Expand Down Expand Up @@ -164,7 +164,7 @@ public function writeSpreadsheet(array $options = [])
]);
$writer = $manager->getWriter($worksheet->getParent(), $file, $options);

$writer->save($file->pwd());
$writer->save($file->getRealPath());

return $file;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ public function getFile()
* @param File $file
* @return Table
*/
public function setFile(File $file)
public function setFile(SplFileInfo $file)
{
$this->file = $file;

Expand Down
16 changes: 9 additions & 7 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
use Cake\Database\Connection;
use Cake\Datasource\ConnectionInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\File;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Utility\Inflector;
use Cake\Utility\Security;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Robotusers\Excel\Database\Factory;
use Robotusers\Excel\Excel\Manager;
use Robotusers\Excel\Model\Sheet;
use Robotusers\Excel\Traits\DiscoverWorksheetTrait;
use SplFileInfo;

/**
* Description of SheetLoader
Expand Down Expand Up @@ -92,27 +93,28 @@ public function __construct(Manager $manager, Factory $factory)

/**
*
* @param string|File $file
* @param string|SplFileInfo $file
* @param string $sheet
* @param array $options
* @param array $locatorOptions
* @return Sheet
*/
public function get($file, $sheet = null, array $options = [], array $locatorOptions = [])
{
if (!$file instanceof File) {
$file = new File($file);
if (!$file instanceof SplFileInfo) {
$file = new SplFileInfo($file);
}
if (is_array($sheet)) {
$options = $sheet;
$sheet = null;
}

$reader = $this->manager->getReader($file, $options);
$excel = $reader->load($file->pwd());
$excel = $reader->load($file->getRealPath());
$worksheet = $this->discoverWorksheet($excel, $sheet);

$hash = $file->md5();
// $hash = $file->md5();
$hash = Security::hash($file->getRealPath(),'md5');
$sheetId = $excel->getIndex($worksheet);

if (!isset($this->sheets[$hash][$sheetId])) {
Expand All @@ -134,7 +136,7 @@ public function get($file, $sheet = null, array $options = [], array $locatorOpt
* @param array $options
* @return Sheet
*/
protected function loadSheet(File $file, Worksheet $worksheet, array $options)
protected function loadSheet(SplFileInfo $file, Worksheet $worksheet, array $options)
{
$schema = $this->factory->createSchema($worksheet, $options['excel']);
$connection = $this->getConnection();
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
namespace Robotusers\Excel\Test;

use Cake\Filesystem\File;
use Cake\TestSuite\TestCase as CakeTestCase;
use InvalidArgumentException;
use SplFileInfo;

/**
* Description of TestCase
Expand Down Expand Up @@ -66,8 +66,8 @@ protected function loadWorksheet($filename, $sheetIndex = 0)
{
$file = $this->getFile($filename);

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file->pwd());
$excel = $reader->load($file->pwd());
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file->getRealPath());
$excel = $reader->load($file->getRealPath());

return $excel->getSheet($sheetIndex);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/Excel/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

use Cake\Chronos\Chronos;
use Cake\Chronos\Date;
use Cake\Filesystem\File;
use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
use InvalidArgumentException;
Expand All @@ -40,6 +39,7 @@
use Robotusers\Excel\Excel\Manager;
use Robotusers\Excel\Test\TestCase;
use UnexpectedValueException;
use SplFileInfo;

/**
* Description of ManagerTest
Expand Down Expand Up @@ -68,10 +68,10 @@ public function testGetReaderMissingFile()
$this->expectExceptionMessage('File foo does not exist.');

$manager = new Manager();
$file = $this->createMock(File::class);
$file->method('exists')
$file = $this->createMock(SplFileInfo::class);
$file->method('getSize')
->willReturn(false);
$file->method('name')
$file->method('getBasename')
->willReturn('foo');

$manager->getReader($file);
Expand All @@ -83,11 +83,11 @@ public function testGetWriterMissingFile()
$this->expectExceptionMessage('File foo does not exist.');

$manager = new Manager();
$file = $this->createMock(File::class);
$file = $this->createMock(SplFileInfo::class);
$excel = $this->createMock(Spreadsheet::class);
$file->method('exists')
$file->method('getSize')
->willReturn(false);
$file->method('name')
$file->method('getBasename')
->willReturn('foo');

$manager->getWriter($excel, $file);
Expand Down Expand Up @@ -763,7 +763,7 @@ public function testSaveAndCallbackWriter()

$writer->expects($this->once())
->method('save')
->with($file->pwd());
->with($file->getRealPath());

$manager = new Manager();

Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Model/Behavior/ExcelBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventList;
use Cake\Event\EventManager;
use Cake\Filesystem\File;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use InvalidArgumentException;
Expand All @@ -36,6 +35,7 @@
use Robotusers\Excel\Excel\Manager;
use Robotusers\Excel\Test\TestCase;
use RuntimeException;
use SplFileInfo;
use stdClass;

/**
Expand Down Expand Up @@ -150,7 +150,7 @@ public function testWorksheetName()
$excel = $this->createMock(Spreadsheet::class);
$manager = $this->createMock(Manager::class);
$worksheet = $this->createMock(Worksheet::class);
$file = $this->createMock(File::class);
$file = $this->createMock(SplFileInfo::class);

$table = $this->createTable([
'manager' => $manager
Expand Down Expand Up @@ -181,7 +181,7 @@ public function testWorksheetCodeName()
$excel = $this->createMock(Spreadsheet::class);
$manager = $this->createMock(Manager::class);
$worksheet = $this->createMock(Worksheet::class);
$file = $this->createMock(File::class);
$file = $this->createMock(SplFileInfo::class);

$table = $this->createTable([
'manager' => $manager
Expand Down Expand Up @@ -217,7 +217,7 @@ public function testWorksheetIndex()
$excel = $this->createMock(Spreadsheet::class);
$manager = $this->createMock(Manager::class);
$worksheet = $this->createMock(Worksheet::class);
$file = $this->createMock(File::class);
$file = $this->createMock(SplFileInfo::class);

$table = $this->createTable([
'manager' => $manager
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testWorksheetActive()
$excel = $this->createMock(Spreadsheet::class);
$manager = $this->createMock(Manager::class);
$worksheet = $this->createMock(Worksheet::class);
$file = $this->createMock(File::class);
$file = $this->createMock(SplFileInfo::class);

$table = $this->createTable([
'manager' => $manager
Expand Down