Skip to content
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

1793 kill liuggio #144

Merged
merged 7 commits into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": ">=5.3.3",
"friendsofsymfony/user-bundle": "2.0.*|2.1.*",
"liuggio/excelbundle": "~2.0",
"phpoffice/phpspreadsheet": "^1.4",
"symfony/config": "^2.7|^3.0|^4.0",
"symfony/ldap": "^3.0|^4.0"
},
Expand Down
65 changes: 25 additions & 40 deletions src/FileFormat/ExcelConverter.php
Expand Up @@ -2,40 +2,18 @@

namespace CubeTools\CubeCommonBundle\FileFormat;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Helper for exporting html to excel file.
*/
class ExcelConverter
{
private $excelSrvc;

/**
* Create service.
*
* @param \Luiggio\ExcelBundle\Factory $excelService
*/
public function setExcelService($excelService)
{
$this->excelSrvc = $excelService;
}

/**
*
* @return \Luiggio\ExcelBundle\Factory
*/
public function getExcelService()
{
if (!isset($this->excelSrvc)) {
$this->excelSrvc = new \Liuggio\ExcelBundle\Factory();
}

return $this->excelSrvc;
}

/**
* Convert html to excel file.
*
Expand Down Expand Up @@ -94,40 +72,47 @@ public function fromHtml($html, $selector = null)

$tmpFile = $this->getTempHtmlFile($htmlStr); // as temporary file because it must have a filename

return $this->getExcelService()->createPHPExcelObject($tmpFile['path']);
return IOFactory::load($tmpFile['path']);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

// tmpfile is deleted automatically
}

/**
* Create response with excel download.
*
* @param \PHPExcel $xlObj Excel object to create the download from
* @param string $filename filename to give to the download
* @param string $format format of write (like Excel2007)
* @param string $contentType mime content type
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet Excel object to create the download from
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need for the namespace. The class has a use statement, and this is used in phpdoc as well.

* @param string $filename filename to give to the download
* @param string $format format of write (like Excel2007)
* @param string $contentType mime content type
*
* @return \Symfony\Component\HtmlFoundation\Response
*/
public function createResponse(\PHPExcel $xlObj, $filename, $format, $contentType)
public function createResponse(Spreadsheet $spreadsheet, $filename, $format, $contentType)
{
return self::createExcelResponse($this->getExcelService(), $xlObj, $filename, $format, $contentType);
return self::createExcelResponse($spreadsheet, $filename, $format, $contentType);
}

/**
* Create response with excel download.
*
* @param Luiggio\ExcelBundle\Factory $excelSrvc
* @param \PHPExcel $xlObj Excel object to create the download from
* @param string $filename filename to give to the download
* @param string $format format of write (like Excel2007)
* @param string $contentType mime content type
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet Excel object to create the download from
* @param string $filename filename to give to the download
* @param string $writerType format of write (like Excel2007)
* @param string $contentType mime content type
*
* @return \Symfony\Component\HtmlFoundation\Response
*/
public static function createExcelResponse($excelSrvc, \PHPExcel $xlObj, $filename, $format, $contentType)
public static function createExcelResponse(Spreadsheet $spreadsheet, $filename, $writerType, $contentType)
{
$xlWr = $excelSrvc->createWriter($xlObj, $format);
$response = $excelSrvc->createStreamedResponse($xlWr);
$writer = IOFactory::createWriter($spreadsheet, $writerType);

$response = new StreamedResponse(
function () use ($writer) {
$writer->save('php://output');
},
200,
[]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. The headers are set later. So it is fine. We could skip this arg entirely ([] is the default).

);

$headers = $response->headers;
$headers->set('Content-Type', $contentType.'; charset=utf-8');
$headers->set('Content-Disposition', $headers->makeDisposition(
Expand Down
104 changes: 0 additions & 104 deletions src/FileFormat/ExcelReader.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Resources/config/services.yml
Expand Up @@ -48,9 +48,6 @@ services:
cube_common.excel.converter:
class: CubeTools\CubeCommonBundle\FileFormat\ExcelConverter
CubeTools\CubeCommonBundle\FileFormat\ExcelConverter: '@cube_common.excel.converter'
cube_common.excel.reader:
class: CubeTools\CubeCommonBundle\FileFormat\ExcelReader
CubeTools\CubeCommonBundle\FileFormat\ExcelReader: '@cube_common.excel.reader'
CubeTools\CubeCommonBundle\DataHandling\Logs\DataDogAudit:
class: CubeTools\CubeCommonBundle\DataHandling\Logs\DataDogAudit
arguments:
Expand Down
10 changes: 6 additions & 4 deletions tests/FileFormat/ExcelConverterTest.php
Expand Up @@ -16,14 +16,16 @@ public function testExportAll($data)
{
$h2e = $this->getService();
$xlo = $h2e->fromHtml($data);
$this->assertInstanceOf('\PHPExcel', $xlo);
$this->assertInstanceOf('\PhpOffice\PhpSpreadsheet\Spreadsheet', $xlo);
}

/**
* @expectedException \InvalidArgumentException
Copy link
Contributor

Choose a reason for hiding this comment

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

*/
public function testInvalidArg()
{
$h2e = $this->getService();
$data = new self();
$this->expectException(\InvalidArgumentException::class);
$h2e->fromHtml($data);
}

Expand All @@ -43,7 +45,7 @@ public function testExportPart($data)
// CssSelector not installed, but enough code checked
return;
}
$this->assertInstanceOf('\PHPExcel', $xlo);
$this->assertInstanceOf('\PhpOffice\PhpSpreadsheet\Spreadsheet', $xlo);
}

/**
Expand All @@ -52,7 +54,7 @@ public function testExportPart($data)
public function testCreateResponse()
{
$fileName = 'anyName.xlsx';
$format = 'Excel2007';
$format = 'Xlsx';
$contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

$h2e = $this->getService();
Expand Down
76 changes: 0 additions & 76 deletions tests/FileFormat/ExcelReaderTest.php

This file was deleted.