Created
November 26, 2012 09:11
-
-
Save k-holy/4147326 to your computer and use it in GitHub Desktop.
Volcanus\Csv\Writerの利用サンプル(Silex)
This file contains 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
{ | |
"require": { | |
"silex/silex": "1.0.*", | |
"doctrine/dbal": "2.2.*", | |
"volcanus/csv": "dev-master" | |
}, | |
"minimum-stability": "dev" | |
} |
This file contains 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\HttpKernel\Exception\BadRequestHttpException; | |
$app = new Application(); | |
$app->register(new DoctrineServiceProvider(), array( | |
'db.options' => include __DIR__ . '/../doctrine.php' | |
)); | |
$app->get('/logs/export', function(Application $app, Request $request) { | |
$select_sql = <<< SQL | |
SELECT | |
id | |
, requested_at | |
, name | |
, user_agent | |
FROM | |
accesslogs | |
SQL; | |
$statement = $app['db']->executeQuery($select_sql); | |
$writer = new \Volcanus\Csv\Writer(array( | |
'delimiter' => ',', | |
'enclosure' => '"', | |
'escape' => '"', | |
'inputEncoding' => 'UTF-8', | |
'outputEncoding' => 'SJIS-win', | |
'writeHeaderLine' => true, | |
'responseFilename' => 'accesslogs.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']); | |
}, 'フィールド加工サンプル'), | |
)); | |
$writer->file = new \SplFileObject('php://temp', 'r+'); | |
$writer->write($statement); | |
$writer->send(); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment