Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Release Candidate

  • Loading branch information...
commit f1924d8abb21f15121810f8d04549b88b77cc241 1 parent 933bdd5
@liuggio authored
View
2  .travis.yml
@@ -11,5 +11,5 @@ before_script:
- composer install --dev
script:
- - php php-cs-fixer.phar fix -v --dry-run .; EXIT 0;
+ - php php-cs-fixer.phar fix --dry-run --verbose --diff . || true;
- bin/phpunit
View
5 DependencyInjection/Configuration.php
@@ -18,11 +18,8 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
- $rootNode = $treeBuilder->root('liuggio_excel');
+ $treeBuilder->root('liuggio_excel');
- // Here you should define the parameters that are allowed to
- // configure your bundle. See the documentation linked above for
- // more information on that topic.
return $treeBuilder;
}
}
View
72 Factory.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Liuggio\ExcelBundle;
+
+use Symfony\Component\HttpFoundation\StreamedResponse;
+
+/**
+ * Factory for PHPExcel objects, StreamedResponse, and PHPExcel_Writer_IWriter.
+ *
+ * @package Liuggio\ExcelBundle
+ */
+class Factory
+{
+ private $phpExcelIO;
+
+ public function __construct($phpExcelIO = '\PHPExcel_IOFactory')
+ {
+ $this->phpExcelIO = $phpExcelIO;
+ }
+ /**
+ * Creates an empty PHPExcel Object if the filename is empty, otherwise loads the file into the object.
+ *
+ * @param string $filename
+ *
+ * @return \PHPExcel
+ */
+ public function createPHPExcelObject($filename = null)
+ {
+ if (null == $filename) {
+ $phpExcelObject = new \PHPExcel();
+
+ return $phpExcelObject;
+ }
+
+ return call_user_func(array($this->phpExcelIO, 'load'), array($filename));
+ }
+
+ /**
+ * Create a writer given the PHPExcelObject and the type,
+ * the type coul be one of PHPExcel_IOFactory::$_autoResolveClasses
+ *
+ * @param \PHPExcel $phpExcelObject
+ * @param string $type
+ *
+ *
+ * @return \PHPExcel_Writer_IWriter
+ */
+ public function createWriter(\PHPExcel $phpExcelObject, $type = 'Excel5')
+ {
+ return call_user_func(array($this->phpExcelIO, 'createWriter'), $phpExcelObject, $type);
+ }
+
+ /**
+ * Stream the file as Response.
+ *
+ * @param \PHPExcel_Writer_IWriter $writer
+ * @param int $status
+ * @param array $headers
+ *
+ * @return StreamedResponse
+ */
+ public function createStreamedResponse(\PHPExcel_Writer_IWriter $writer, $status = 200, $headers = array())
+ {
+ return new StreamedResponse(
+ function () use ($writer) {
+ $writer->save('php://output');
+ },
+ $status,
+ $headers
+ );
+ }
+}
View
147 README.md
@@ -8,7 +8,21 @@ Symfony2 Excel bundle
This bundle permits you to create an easily modifiable excel object.
-You should know that csv is faster so I encourage you to use the built-in function for csv: http://php.net/manual-lookup.php?pattern=csv&lang=en&scope=quickref
+## Version 2
+
+This is the **shiny** new version.
+There is a big BC with the 1.* version, but **unit tests**, **functional tests**, and **the new factory** is very simple to use.
+
+### Version 1.*
+
+If you have installed an old version, and you are happy to use it, you could find documentation and files
+in the [tag v1.0.6](https://github.com/liuggio/ExcelBundle/releases/tag/v1.0.6),
+[browse the code](https://github.com/liuggio/ExcelBundle/tree/cf0ecbeea411d7c3bdc8abab14c3407afdf530c4).
+
+### Things to know:
+
+CSV is faster so if you have to create simple xls file,
+I encourage you to use the built-in function for csv: http://php.net/manual-lookup.php?pattern=csv&lang=en&scope=quickref
## Installation
@@ -16,7 +30,7 @@ You should know that csv is faster so I encourage you to use the built-in functi
``` yml
"require" : {
- "liuggio/excelbundle": "2.0.x-dev",
+ "liuggio/excelbundle": "~2.0",
}
```
@@ -29,30 +43,57 @@ You should know that csv is faster so I encourage you to use the built-in functi
);
```
-## Services
+## TL;DR
+
+- Create an empty object:
+
+``` php
+$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
+```
-The list of the services are listed in `/Resources/config/services.yml`.
+- Create an object from a file:
``` php
-// create MS Excel5
-$this->get('xls.excel5');
-// create MS Excel 2007
-$this->get('xls.excel2007');
-// read file
-$excelService = $this->get('xls.excel5')->load($filename);
+$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('file.xls');
+```
+
+- Create a Excel5 and write to a file given the object:
+
+```php
+$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
+$writer->save('file.xls');
```
+- Create a Excel5 and create a StreamedResponse:
+
+```php
+$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
+$writer->save('file.xls');
+```
+## Not Only 'Excel5'
+
+The list of the types are:
+
+1. 'Excel5'
+2. 'Excel2007'
+3. 'Excel2003XML'
+4. 'OOCalc'
+5. 'SYLK'
+6. 'Gnumeric'
+7. 'HTML'
+8. 'CSV'
+
## Example
### Fake Controller
-The best place to start is the fake Controller at `Tests/app/Controller/FakeController.php`, this is a working example.
+The best place to start is the fake Controller at `Tests/app/Controller/FakeController.php`, that is a working example.
### More example
-A lot of great example are in the official PHPExcel repository https://github.com/PHPOffice/PHPExcel/tree/develop/Examples
+You could find a lot of examples in the official PHPExcel repository https://github.com/PHPOffice/PHPExcel/tree/develop/Examples
-### For lazy :)
+### For lazy devs
``` php
namespace YOURNAME\YOURBUNDLE\Controller;
@@ -65,32 +106,29 @@ class DefaultController extends Controller
public function indexAction($name)
{
// ask the service for a Excel5
- $excelService = $this->get('xls.excel5');
- // or $this->get('xls.excel5')->load($filename);
- // or create your own is easy just modify services.yml
-
- // create the object see http://phpexcel.codeplex.com documentation
- $excelService->excelObj->getProperties()->setCreator("liuggio")
- ->setLastModifiedBy("Giulio De Donato")
- ->setTitle("Office 2005 XLSX Test Document")
- ->setSubject("Office 2005 XLSX Test Document")
- ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
- ->setKeywords("office 2005 openxml php")
- ->setCategory("Test result file");
- $excelService->excelObj->setActiveSheetIndex(0)
- ->setCellValue('A1', 'Hello')
- ->setCellValue('B2', 'world!');
- $excelService->excelObj->getActiveSheet()->setTitle('Simple');
- // Set active sheet index to the first sheet, so Excel opens this as the first sheet
- $excelService->excelObj->setActiveSheetIndex(0);
-
- //create the Symfony Streamed Response
- $response = $excelService->createStreamedResponse();
-
+ $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
+
+ $phpExcelObject->getProperties()->setCreator("liuggio")
+ ->setLastModifiedBy("Giulio De Donato")
+ ->setTitle("Office 2005 XLSX Test Document")
+ ->setSubject("Office 2005 XLSX Test Document")
+ ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
+ ->setKeywords("office 2005 openxml php")
+ ->setCategory("Test result file");
+ $phpExcelObject->setActiveSheetIndex(0)
+ ->setCellValue('A1', 'Hello')
+ ->setCellValue('B2', 'world!');
+ $phpExcelObject->getActiveSheet()->setTitle('Simple');
+ // Set active sheet index to the first sheet, so Excel opens this as the first sheet
+ $phpExcelObject->setActiveSheetIndex(0);
+
+ // create the writer
+ $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
+ // create the response
+ $response = $this->get('phpexcel')->createStreamedResponse($writer);
+ // adding headers
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
- $response->headers->set('Content-Disposition', 'attachment;filename=stdream2.xls');
-
- // If you are using a https connection, you have to set those two headers and use sendHeaders() for compatibility with IE <9
+ $response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
@@ -99,34 +137,13 @@ class DefaultController extends Controller
}
```
-With the correct writer (e.g. PHPExcel_Writer_Excel5) you could also write the output to a file:
-
-``` php
-
- public function indexAction($name)
- {
- $excelService = $this->get('xls.excel5');
- //...load and modify or create php excel object
- $excelService->getStreamWriter()->write($filename);
- }
-```
-
## Contributors
-@pivasyk
-
-@dirkbl
-
-@DerStoffel
-
-@artturi
-
-@isqad88
-
-@mazenovi
-
-@gnat42
+the [list of contributors](https://github.com/liuggio/ExcelBundle/graphs/contributors)
-@jochenhilgers
+## Contribute
-@Squazic
+1. fork the project
+2. clone the repo
+3. get the coding standard fixer: `wget http://cs.sensiolabs.org/get/php-cs-fixer.phar`
+4. before the PullRequest you should run the coding standard fixer with `php php-cs-fixer.phar fix -v fix .`
View
23 Resources/config/services.yml
@@ -1,23 +1,6 @@
parameters:
- xls.phpexcel.class: PHPExcel
- xls.service.class: Liuggio\ExcelBundle\Service\ExcelContainer
+ phpexcel.class: Liuggio\ExcelBundle\Factory
services:
- xls.phpexcel:
- class: %xls.phpexcel.class%
-
- xls.service:
- class: Liuggio\ExcelBundle\Service\Excel
- arguments: ["@xls.phpexcel"]
-
- xls.excel5:
- class: Liuggio\ExcelBundle\Service\Excel
- arguments: ["@xls.phpexcel" ]
- calls:
- - [ setType, [ "Excel5" ] ]
-
- xls.excel2007:
- class: Liuggio\ExcelBundle\Service\Excel
- arguments: ["@xls.phpexcel"]
- calls:
- - [ setType, [ "Excel2007" ] ]
+ phpexcel:
+ class: %phpexcel.class%
View
98 Service/Excel.php
@@ -1,98 +0,0 @@
-<?php
-
-namespace Liuggio\ExcelBundle\Service;
-
-use Symfony\Component\HttpFoundation\StreamedResponse;
-
-/**
- * Excel Service.
- *
- * @author gnat
- */
-class Excel
-{
- public $excelObj;
-
- private $type;
-
- private $reader;
- private $writer;
- private $types = array('Excel5','Excel2007','PDF');
-
- public function __construct(\PHPExcel $excelObj)
- {
- $this->excelObj = $excelObj;
- }
-
- public function load($path)
- {
- if(!$this->reader)
- $this->reader = \PHPExcel_IOFactory::createReader($this->type);
-
- try {
- $this->excelObj = $this->reader->load($path);
-
- return true;
- } catch (\Exception $e) {
- return false;
- }
- }
-
- public function getType()
- {
- return $this->type;
- }
-
- public function setType($type)
- {
- if(!in_array($type, $this->types))
- throw new \InvalidArgumentException();
-
- $this->type = $type;
-
- return $this;
- }
-
- public function getWriter($type = null)
- {
- if(!is_null($type))
- $this->setType($type);
-
- $this->writer = \PHPExcel_IOFactory::createWriter($this->excelObj, $this->type);
-
- return $this->writer;
- }
-
- /**
- * Create the response with the file content.
- *
- * @param int $status
- * @param array $headers
- *
- * @return StreamedResponse
- */
- public function createStreamedResponse($status = 200, $headers = array())
- {
- $writer = $this->getWriter();
-
- return new StreamedResponse(
- function () use ($writer) {
- $writer->save('php://output');
- },
- $status,
- $headers
- );
-
- }
-
- /**
- * @deprecated deprecated since version 2.0
- *
- * @return StreamedResponse
- */
- public function getResponse()
- {
- return $this->createStreamedResponse(200);
- }
-
-}
View
15 Tests/Controller/FakeControllerTest.php
@@ -36,4 +36,19 @@ public function testSaveAction()
$this->assertFileExists($content, sprintf('file %s should exist', $content));
}
+
+ public function testReadAndSaveAction()
+ {
+ $client = static::createClient();
+
+ $client->request('GET', '/fake/read');
+
+ $this->assertEquals(201, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());
+
+ $content = $client->getResponse()->getContent();
+
+ $this->assertStringEndsWith('.xls', $content);
+
+ $this->assertFileExists($content, sprintf('file %s should exist', $content));
+ }
}
View
25 Tests/FactoryTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Liuggio\ExcelBundle\Tests;
+
+use Liuggio\ExcelBundle\Factory;
+
+class FactoryTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCreate()
+ {
+ $factory = new Factory();
+ $this->assertInstanceOf('\PHPExcel', $factory->createPHPExcelObject());
+ }
+
+ public function testCreateStreamedResponse()
+ {
+ $writer = $this->getMock('\PHPExcel_Writer_IWriter');
+ $writer->expects($this->once())
+ ->method('save')
+ ->with('php://output');
+
+ $factory = new Factory();
+ $factory->createStreamedResponse($writer)->sendContent();
+ }
+}
View
47 Tests/app/Controller/FakeController.php
@@ -9,11 +9,15 @@ class FakeController extends Controller
{
public function streamAction()
{
- $response = $this->createXSLObject()->createStreamedResponse();
-
+ // create an empty object
+ $phpExcelObject = $this->createXSLObject();
+ // create the writer
+ $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
+ // create the response
+ $response = $this->get('phpexcel')->createStreamedResponse($writer);
+ // adding headers
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');
- // If you are using a https connection, you have to set those two headers and use sendHeaders() for compatibility with IE <9
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
@@ -22,34 +26,53 @@ public function streamAction()
public function storeAction()
{
- $excelService = $this->createXSLObject();
- //create the response
- $writer = $excelService->getWriter();
+ // create an empty object
+ $phpExcelObject = $this->createXSLObject();
+ // create the writer
+ $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
+ $filename = tempnam(sys_get_temp_dir(), 'xls-') . '.xls';
+ // create filename
+ $writer->save($filename);
+ return new Response($filename, 201);
+ }
+
+ public function readAndSaveAction()
+ {
+ $filename = $this->container->getParameter('xls_fixture_absolute_path');
+ // create an object from a filename
+ $phpExcelObject = $this->createXSLObject($filename);
+ // create the writer
+ $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$filename = tempnam(sys_get_temp_dir(), 'xls-') . '.xls';
+ // create filename
$writer->save($filename);
return new Response($filename, 201);
}
+ /**
+ * utility class
+ * @return mixed
+ */
private function createXSLObject()
{
- $excelService = $this->get('xls.excel5');
+ $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
- $excelService->excelObj->getProperties()->setCreator("liuggio")
+ $phpExcelObject->getProperties()->setCreator("liuggio")
->setLastModifiedBy("Giulio De Donato")
->setTitle("Office 2005 XLSX Test Document")
->setSubject("Office 2005 XLSX Test Document")
->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
->setKeywords("office 2005 openxml php")
->setCategory("Test result file");
- $excelService->excelObj->setActiveSheetIndex(0)
+ $phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!');
- $excelService->excelObj->getActiveSheet()->setTitle('Simple');
+ $phpExcelObject->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
- $excelService->excelObj->setActiveSheetIndex(0);
+ $phpExcelObject->setActiveSheetIndex(0);
- return $excelService;
+ return $phpExcelObject;
}
}
View
3  Tests/app/config.yml
@@ -7,3 +7,6 @@ framework:
storage_id: session.storage.mock_file
profiler:
collect: false
+
+parameters:
+ xls_fixture_absolute_path: "%kernel.root_dir%/fixture/file.xls"
View
BIN  Tests/app/fixture/file.xls
Binary file not shown
View
4 Tests/app/routing.yml
@@ -5,3 +5,7 @@ fake_route_stream:
fake_route_store:
path: /fake/store
defaults: { _controller: LiuggioExcelBundle:Fake:store }
+
+fake_route_read:
+ path: /fake/read
+ defaults: { _controller: LiuggioExcelBundle:Fake:readAndSave }
Please sign in to comment.
Something went wrong with that request. Please try again.