Skip to content

Commit

Permalink
feat: League CSV dependency and update
Browse files Browse the repository at this point in the history
RoleController.php
  • Loading branch information
dogukanoksuz committed Jan 30, 2024
1 parent 8bb46c3 commit aa7564e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
32 changes: 20 additions & 12 deletions app/Http/Controllers/API/Settings/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use League\Csv\Writer;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
Expand Down Expand Up @@ -608,6 +609,7 @@ public function exportDetailedListAsCsv()
$headers = [
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename='.$fileName,
'Content-Transfer-Encoding' => 'utf-8',
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
Expand All @@ -620,20 +622,26 @@ public function exportDetailedListAsCsv()
'İzin Değeri',
];

$callback = function () use ($data, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
$writer = Writer::createFromPath("php://temp", "r+");
$writer->insertOne($columns);

foreach ($data as $row) {
fputcsv($file, [
$columns[0] => $row['username'],
$columns[1] => $row['role_name'],
$columns[2] => $row['perm_type'],
$columns[3] => $row['perm_value'],
]);
}
foreach ($data as $row) {
$writer->insertOne([
$row['username'],
$row['role_name'],
$row['perm_type'],
$row['perm_value'],
]);
}

fclose($file);
$flushThreshold = 1000;
$callback = function () use ($writer, $flushThreshold) {
foreach ($writer->chunk(1024) as $offset => $chunk) {
echo $chunk;
if ($offset % $flushThreshold === 0) {
flush();
}
}
};

return response()->stream($callback, 200, $headers);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"laravel/helpers": "^1.5",
"laravel/tinker": "^2.7",
"laravel/ui": "^4.0",
"league/csv": "^9.0",
"limanmys/php-smb": "^3.5",
"mervick/aes-everywhere": "^1.1",
"mews/captcha": "^3.2",
Expand Down
91 changes: 90 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa7564e

Please sign in to comment.