Created
November 28, 2012 01:47
-
-
Save k-holy/4158537 to your computer and use it in GitHub Desktop.
Volcanus\Csv\Writerの利用サンプル(Silex + DoctrineDBAL + StreamedResponse版)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/../vendor/autoload.php'; | |
use Silex\Application; | |
use Silex\Provider\DoctrineServiceProvider; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\StreamedResponse; | |
$app = new Application(); | |
$app->register(new DoctrineServiceProvider(), array( | |
'db.options' => include __DIR__ . '/../doctrine.php' | |
)); | |
$app->get('/logs/export', function(Application $app, Request $request) { | |
$writer = new \Volcanus\Csv\Writer(array( | |
'inputEncoding' => 'UTF-8', | |
'outputEncoding' => 'SJIS-win', | |
'writeHeaderLine' => true, | |
'responseFilename' => '日本語ファイル.csv', | |
)); | |
$writer->fields(array( | |
array('id' , 'アクセスログID'), | |
array('requested_at', 'アクセス日時'), | |
array('name' , '名前'), | |
array('user_agent' , 'UserAgent'), | |
array(function($item) { | |
return sprintf("[%d]\r\n%s", $item['id'], $item['name']); | |
}, 'フィールド加工サンプル'), | |
)); | |
$statement = $app['db']->executeQuery( | |
'SELECT id, requested_at, name, user_agent FROM accesslogs' | |
); | |
$writer->file = new \SplFileObject('php://temp', 'r+'); | |
$writer->write($statement); | |
// StreamedResponse だとコールバックでレスポンス内容を返せる | |
return new StreamedResponse( | |
function() use ($writer) { | |
$writer->flush(); | |
}, 200, $writer->buildResponseHeaders() | |
); | |
}); | |
$app->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ResponseHeaderBagのメソッドを利用する場合 | |
$response = new StreamedResponse( | |
function() use ($writer) { | |
$writer->flush(); | |
}, 200 | |
); | |
$response->headers->set('Content-Type', 'text/csv'); | |
$response->headers->set('Content-Length', $writer->contentLength()); | |
$response->headers->set('Content-Disposition', $response->headers->makeDisposition('attachment', | |
$writer->responseFilename, | |
sprintf('=?UTF-8?B?%s?=', base64_encode($writer->responseFilename)) | |
)); | |
return $response; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 自分でレスポンスヘッダを定義する場合 | |
return new StreamedResponse( | |
function() use ($writer) { | |
$writer->flush(); | |
}, 200, array( | |
'Content-Type' => 'text/csv', | |
'Content-Length' => $writer->contentLength(), | |
'Content-Disposition' => sprintf('attachment; filename="%s"', | |
rawurlencode($writer->responseFilename)), | |
) | |
); | |
// Writer::contentLength() はPHP5.4的に書くと $writer->file->fstat()['size'] と同じです |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 自分でレスポンスヘッダを定義する場合 | |
return new StreamedResponse( | |
function() use ($writer) { | |
$writer->flush(); | |
}, 200, array( | |
'Content-Type' => 'text/csv', | |
'Content-Length' => $writer->contentLength(), | |
'Content-Disposition' => sprintf('attachment; filename="%s"', | |
rawurlencode($writer->responseFilename)), | |
) | |
); | |
// Writer::contentLength() はPHP5.4的に書くと $writer->file->fstat()['size'] と同じです |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment