public function register(Application $app) { $app['csv.exporter.config'] = $app->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) { $lexer = new LexerConfig(); $lexer->setDelimiter(';')->setEnclosure('"')->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8'); return $lexer; }); $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, ['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')); }); }
/** * Handles response for csv-request. * * @param ViewHandler $handler * @param View $view * @param Request $request * @param string $format * * @return Response * * @throws ObjectNotSupportedException */ public function createResponse(ViewHandler $handler, View $view, Request $request, $format) { if (!$view->getData() instanceof ListRepresentation) { throw new ObjectNotSupportedException($view); } $viewData = $view->getData(); $data = new CallbackCollection($viewData->getData(), [$this, 'prepareData']); $fileName = sprintf('%s.csv', $viewData->getRel()); $config = new ExporterConfig(); $exporter = new Exporter($config); $data->rewind(); if ($row = $data->current()) { $config->setColumnHeaders(array_keys($row)); } $config->setDelimiter($this->convertValue($request->get('delimiter', ';'), self::$delimiterMap)); $config->setNewline($this->convertValue($request->get('newLine', '\\n'), self::$newLineMap)); $config->setEnclosure($request->get('enclosure', '"')); $config->setEscape($request->get('escape', '\\')); $response = new StreamedResponse(); $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $fileName, $fileName); $response->headers->set('Content-Type', 'text/csv'); $response->headers->set('Content-Disposition', $disposition); $response->setCallback(function () use($data, $exporter) { $exporter->export('php://output', $data); }); $response->send(); return $response; }
public function __construct($host, $database, $user, $pass) { $this->database = new \PDO("mysql:host={$host};dbname={$database}", $user, $pass); $config = new ExporterConfig(); $config->setDelimiter(";")->setEnclosure("'")->setEscape("\\")->setToCharset('SJIS-win')->setFromCharset('UTF-8')->setFileMode(CsvFileObject::FILE_MODE_WRITE); $this->exporter = new Exporter($config); }
/** * @param array $attributes array( '中文描述' => 'key' ); * @param string $title * @param array $data * @param string $fromCharSet * @param string $toCharSet * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function export_csv(array $attributes, $title = 'csv-data-dump', $data = array(), $fromCharSet = 'UTF-8', $toCharSet = 'UTF-8') { $title .= '-' . date('Ymd-His'); $config = new ExporterConfig(); $config->setDelimiter(';')->setEnclosure("'")->setEscape("\\")->setFromCharset($fromCharSet)->setToCharset($toCharSet); $head = array([], []); foreach ($attributes as $key => $val) { $head[0][] = $key; $head[1][] = $val; } $data = array_merge($head, $data); $headers = array('Content-type' => "application/csv; filename=\"{$title}.csv\"", 'Content-Disposition' => "attachement; filename=\"{$title}.csv\"", 'Cache-Control' => "no-cache"); $response = \Response::stream(function () use($config, $data) { $exporter = new Exporter($config); $exporter->export('php://output', $data); }, 200, $headers); return $response; }
public function testStaticCreateWithSetExporterConfig() { $response = CsvResponse::create(array(array('1', 'alice', '*****@*****.**'), array('2', 'bob', '*****@*****.**'), array('3', 'carol', '*****@*****.**'))); $config = new ExporterConfig(); $config->setDelimiter("\t"); $response->setExporterConfig($config); ob_start(); $response->sendContent(); $content = ob_get_contents(); ob_end_clean(); $this->assertContains("1\talice\talice@example.com", $content); $this->assertContains("2\tbob\tbob@example.com", $content); $this->assertContains("3\tcarol\tcarol@example.com", $content); }
public function testDelimiter() { $config = new ExporterConfig(); $this->assertSame(',', $config->getDelimiter()); $this->assertSame('del', $config->setDelimiter('del')->getDelimiter()); }