From a159926a36a6857beecf08d228dcec1ebef6313b Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Tue, 18 Mar 2014 10:33:19 +0100 Subject: [PATCH] Add new CSVProvider --- composer.json | 1 + composer.lock | 59 +++++++++++++- lib/Alchemy/Phrasea/Application.php | 2 + .../Core/Provider/CSVServiceProvider.php | 77 +++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 lib/Alchemy/Phrasea/Core/Provider/CSVServiceProvider.php diff --git a/composer.json b/composer.json index cf3aea836f..9abe96a0ab 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "gedmo/doctrine-extensions" : "~2.3.0", "alchemy/google-plus-api-client" : "~0.6.2", "alchemy/geonames-api-consumer" : "~0.1.0", + "goodby/csv" : "~1.0", "guzzle/guzzle" : "~3.0", "imagine/imagine" : "0.6.x-dev@dev", "jms/serializer" : "~0.10", diff --git a/composer.lock b/composer.lock index 41da8c3709..16fbcbd070 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "6d21ea16cd4f85707ff5c89c7a5777ac", + "hash": "2b68364c4ebb09b6f5c3902e0afdcc1a", "packages": [ { "name": "alchemy-fr/tcpdf-clone", @@ -1062,6 +1062,63 @@ ], "time": "2013-08-18 07:18:44" }, + { + "name": "goodby/csv", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/goodby/csv.git", + "reference": "da672802985d196cae767da29b11618a676496b0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/goodby/csv/zipball/da672802985d196cae767da29b11618a676496b0", + "reference": "da672802985d196cae767da29b11618a676496b0", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=5.3.2" + }, + "require-dev": { + "mikey179/vfsstream": ">=1.1.0", + "mockery/mockery": ">=0.7.2", + "phpunit/phpunit": "3.7.*", + "suin/php-expose": ">=1.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "Goodby\\CSV": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "suin", + "email": "suinyeze@gmail.com", + "homepage": "https://www.facebook.com/suinyeze", + "role": "Developer, Renaming Specialist" + }, + { + "name": "reoring", + "email": "mori.reo@gmail.com", + "homepage": "https://www.facebook.com/reoring", + "role": "Developer" + } + ], + "description": "CSV import/export library", + "homepage": "https://github.com/goodby/csv", + "keywords": [ + "csv", + "export", + "import" + ], + "time": "2013-11-22 19:10:34" + }, { "name": "guzzle/guzzle", "version": "v3.7.4", diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index f6fd8d4e06..270a13ba61 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -83,6 +83,7 @@ use Alchemy\Phrasea\Core\Provider\BorderManagerServiceProvider; use Alchemy\Phrasea\Core\Provider\CacheServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationTesterServiceProvider; +use Alchemy\Phrasea\Core\Provider\CSVServiceProvider; use Alchemy\Phrasea\Core\Provider\FileServeServiceProvider; use Alchemy\Phrasea\Core\Provider\FtpServiceProvider; use Alchemy\Geonames\GeonamesServiceProvider; @@ -193,6 +194,7 @@ class Application extends SilexApplication $this->register(new BrowserServiceProvider()); $this->register(new ConfigurationServiceProvider()); $this->register(new ConfigurationTesterServiceProvider); + $this->register(new CSVServiceProvider()); $this->register(new RegistrationServiceProvider()); $this->register(new CacheServiceProvider()); $this->register(new ImagineServiceProvider()); diff --git a/lib/Alchemy/Phrasea/Core/Provider/CSVServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/CSVServiceProvider.php new file mode 100644 index 0000000000..be841339f3 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Provider/CSVServiceProvider.php @@ -0,0 +1,77 @@ +share(function () { + $config = new ExporterConfig(); + return $config + ->setDelimiter(",") + ->setEnclosure('"') + ->setEscape("\\") + ->setToCharset('UTF-8') + ->setFromCharset('UTF-8'); + + }); + + $app['csv.exporter'] = $app->share(function ($app) { + return new Exporter($app['csv.exporter.config']); + }); + + $app['csv.lexer.config'] = $app->share(function ($app) { + return new LexerConfig(); + }); + + $app['csv.lexer'] = $app->share(function ($app) { + return new Lexer($app['csv.lexer.config']); + }); + + $app['csv.interpreter'] = $app->share(function ($app) { + return new Interpreter(); + }); + + $app['csv.response'] = $app->protect(function ($callback) use ($app) { + // set headers to fix ie issues + $response = new StreamedResponse($callback, 200, array( + 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', + 'Last-Modified' => gmdate('D, d M Y H:i:s'). ' GMT', + 'Cache-Control' => 'no-store, no-cache, must-revalidate', + 'Cache-Control' => 'post-check=0, pre-check=0', + 'Pragma' => 'no-cache', + 'Content-Type' => 'text/csv', + 'Cache-Control' => 'max-age=3600, must-revalidate', + 'Content-Disposition' => 'max-age=3600, must-revalidate', + )); + + $response->headers->set('Content-Disposition', $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + 'export.csv' + )); + }); + } + + public function boot(Application $app) + { + } +}