diff --git a/.travis.yml b/.travis.yml index 2c8bbfd..eea6797 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: php php: - - 5.4 - - 5.5 - 5.6 + - 7.0 + - 7.1 env: global: @@ -13,10 +13,10 @@ matrix: fast_finish: true include: - - php: 5.4 + - php: 5.6 env: PHPCS=1 DEFAULT=0 - - php: 5.4 + - php: 5.6 env: COVERALLS=1 DEFAULT=0 install: diff --git a/README.md b/README.md index 7129194..a3614e5 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ A plugin to generate Excel files with CakePHP. ## Requirements -* CakePHP 3.x -* PHP 5.4.16 or greater +* CakePHP 3.6 +* PHP 5.6.0 or greater * Patience ## Installation @@ -60,7 +60,7 @@ Finally, you can link to the current page with the .xlsx extension. This assumes $this->Html->link('Excel file', ['_ext' => 'xlsx']); ``` -Inside your view file you will have access to the PHPExcel library with `$this->PhpExcel`. Please see the [PHPExcel](https://github.com/PHPOffice/PHPExcel) documentation for a guide on how to use PHPExcel. +Inside your view file you will have access to the PhpSpreadsheet library with `$this->Spreadsheet`. Please see the [PhpSpreadsheet](https://github.com/PHPOffice/phpspreadsheet) documentation for a guide on how to use PhpSpreadsheet. ## License diff --git a/composer.json b/composer.json index 9538d81..41caba4 100644 --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ } ], "require": { - "php": ">=5.4.16", - "cakephp/cakephp": "~3.1", - "phpoffice/phpexcel": "~1.8" + "php": ">=5.6.0", + "cakephp/cakephp": "^3.6", + "phpoffice/phpspreadsheet": "^1.2" }, "require-dev": { - "phpunit/phpunit": "4.1.*" + "phpunit/phpunit": "^5.7.14|^6.0" }, "autoload": { "psr-4": { diff --git a/config/bootstrap.php b/config/bootstrap.php index e3e9c63..d3e39c4 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -1,16 +1,17 @@ on('Controller.initialize', function (Event $event) { - $controller = $event->subject(); + $controller = $event->getSubject(); if ($controller->components()->has('RequestHandler')) { - $controller->RequestHandler->config('viewClassMap.xlsx', 'CakeExcel.Excel'); + $controller->RequestHandler->setConfig('viewClassMap.xlsx', 'CakeExcel.Excel'); } }); -Request::addDetector('xlsx', [ +ServerRequest::addDetector('xlsx', [ 'accept' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 'param' => '_ext', 'value' => 'xlsx' diff --git a/src/View/ExcelView.php b/src/View/ExcelView.php index 5e59966..c136f5e 100644 --- a/src/View/ExcelView.php +++ b/src/View/ExcelView.php @@ -1,14 +1,13 @@ subDir = null; $this->layoutPath = null; - $response->type('html'); + $this->response = $this->response->withType('html'); return; } if ($response && $response instanceof Response) { - $response->type('xlsx'); + $this->response = $this->response->withType('xlsx'); + } + + $this->Spreadsheet = new Spreadsheet(); + } + + /** + * Magic accessor for helpers. Backward compatibility for PHPExcel property + * + * @param string $name Name of the attribute to get. + * + * @return mixed + */ + public function __get($name) + { + if ($name === 'PhpExcel') { + return $this->Spreadsheet; } - $this->PhpExcel = new PHPExcel(); + return parent::__get($name); } /** * Render method * - * @param string $view The view being rendered. - * @param string $layout The layout being rendered. - * @return string The rendered view. + * @param string|false|null $view Name of view file to use + * @param string|null $layout Layout to use. + * @return string|null Rendered content or null if content already rendered and returned earlier. + * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ public function render($view = null, $layout = null) { $content = parent::render($view, $layout); - if ($this->response->type() == 'text/html') { + if ($this->response->getType() == 'text/html') { return $content; } $this->Blocks->set('content', $this->output()); - $this->response->download($this->getFilename()); + $this->response = $this->response->withDownload($this->getFilename()); return $this->Blocks->get('content'); } @@ -95,17 +112,13 @@ public function render($view = null, $layout = null) * Generates the binary excel data * * @return string - * @throws CakeException If the excel writer does not exist + * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ protected function output() { ob_start(); - $writer = PHPExcel_IOFactory::createWriter($this->PhpExcel, 'Excel2007'); - - if (!isset($writer)) { - throw new Exception('Excel writer not found'); - } + $writer = new Xlsx($this->Spreadsheet); $writer->setPreCalculateFormulas(false); $writer->setIncludeCharts(true); @@ -127,6 +140,6 @@ public function getFilename() return $this->viewVars['_filename'] . '.xlsx'; } - return Inflector::slug(str_replace('.xlsx', '', $this->request->url)) . '.xlsx'; + return Text::slug(str_replace('.xlsx', '', $this->request->getRequestTarget())) . '.xlsx'; } } diff --git a/tests/TestCase/View/ExcelViewTest.php b/tests/TestCase/View/ExcelViewTest.php index 7ece6c7..f186c4c 100644 --- a/tests/TestCase/View/ExcelViewTest.php +++ b/tests/TestCase/View/ExcelViewTest.php @@ -1,11 +1,11 @@ request = new Request([ + $this->request = new ServerRequest([ 'params' => [ 'plugin' => null, 'controller' => 'posts', @@ -48,14 +53,16 @@ public function tearDown() */ public function testConstruct() { - $result = $this->View->response->type(); + $result = $this->View->response->getType(); $this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $result); - $this->assertTrue($this->View->PhpExcel instanceof PHPExcel); + $this->assertTrue($this->View->Spreadsheet instanceof Spreadsheet); + $this->assertSame($this->View->PhpExcel, $this->View->Spreadsheet); } public function testRender() { - $this->View->name = $this->View->viewPath = 'Posts'; + $this->View->setTemplatePath('Posts'); + $this->View->name = 'Posts'; $output = $this->View->render('index'); $this->assertSame('504b030414', bin2hex(substr($output, 0, 5))); diff --git a/tests/test_app/TestApp/Template/Posts/xlsx/index.ctp b/tests/test_app/TestApp/Template/Posts/xlsx/index.ctp index 49769ef..9edac89 100644 --- a/tests/test_app/TestApp/Template/Posts/xlsx/index.ctp +++ b/tests/test_app/TestApp/Template/Posts/xlsx/index.ctp @@ -1,3 +1,3 @@ PhpExcel->setActiveSheetIndex(0); +$worksheet = $this->Spreadsheet->setActiveSheetIndex(0); $worksheet->setCellValueByColumnAndRow(0, 0, 'Test string');