|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace App\Responder; |
| 15 | + |
| 16 | +use Pagerfanta\PagerfantaInterface; |
| 17 | +use Port\Csv\CsvWriter; |
| 18 | +use Sylius\Component\Grid\Definition\Field; |
| 19 | +use Sylius\Component\Grid\Renderer\GridRendererInterface; |
| 20 | +use Sylius\Component\Grid\View\GridViewInterface; |
| 21 | +use Sylius\Resource\Context\Context; |
| 22 | +use Sylius\Resource\Metadata\Operation; |
| 23 | +use Sylius\Resource\State\ResponderInterface; |
| 24 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 25 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 26 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 27 | +use Webmozart\Assert\Assert; |
| 28 | + |
| 29 | +final class ExportGridToCsvResponder implements ResponderInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + #[Autowire(service: 'sylius.grid.renderer')] |
| 33 | + private readonly GridRendererInterface $gridRenderer, |
| 34 | + private readonly TranslatorInterface $translator, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param GridViewInterface $data |
| 40 | + */ |
| 41 | + public function respond(mixed $data, Operation $operation, Context $context): mixed |
| 42 | + { |
| 43 | + Assert::isInstanceOf($data, GridViewInterface::class); |
| 44 | + |
| 45 | + $response = new StreamedResponse(function () use ($data) { |
| 46 | + $output = fopen('php://output', 'w'); |
| 47 | + |
| 48 | + if (false === $output) { |
| 49 | + throw new \RuntimeException('Unable to open output stream.'); |
| 50 | + } |
| 51 | + |
| 52 | + $writer = new CsvWriter(); |
| 53 | + $writer->setStream($output); |
| 54 | + |
| 55 | + $fields = $this->sortFields($data->getDefinition()->getFields()); |
| 56 | + $this->writeHeaders($writer, $fields); |
| 57 | + $this->writeRows($writer, $fields, $data); |
| 58 | + |
| 59 | + $writer->finish(); |
| 60 | + }); |
| 61 | + |
| 62 | + $response->headers->set('Content-Type', 'text/csv; charset=UTF-8'); |
| 63 | + $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"'); |
| 64 | + |
| 65 | + return $response; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param Field[] $fields |
| 70 | + */ |
| 71 | + private function writeHeaders(CsvWriter $writer, array $fields): void |
| 72 | + { |
| 73 | + $labels = array_map(fn (Field $field) => $this->translator->trans($field->getLabel()), $fields); |
| 74 | + |
| 75 | + $writer->writeItem($labels); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param Field[] $fields |
| 80 | + */ |
| 81 | + private function writeRows(CsvWriter $writer, array $fields, GridViewInterface $gridView): void |
| 82 | + { |
| 83 | + /** @var PagerfantaInterface $paginator */ |
| 84 | + $paginator = $gridView->getData(); |
| 85 | + Assert::isInstanceOf($paginator, PagerfantaInterface::class); |
| 86 | + |
| 87 | + for ($currentPage = 1; $currentPage <= $paginator->getNbPages(); ++$currentPage) { |
| 88 | + $paginator->setCurrentPage($currentPage); |
| 89 | + $this->writePageResults($writer, $fields, $gridView, $paginator->getCurrentPageResults()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param Field[] $fields |
| 95 | + * @param iterable<object> $pageResults |
| 96 | + */ |
| 97 | + private function writePageResults(CsvWriter $writer, array $fields, GridViewInterface $gridView, iterable $pageResults): void |
| 98 | + { |
| 99 | + foreach ($pageResults as $resource) { |
| 100 | + $rows = []; |
| 101 | + foreach ($fields as $field) { |
| 102 | + $rows[] = $this->getFieldValue($gridView, $field, $resource); |
| 103 | + } |
| 104 | + $writer->writeItem($rows); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private function getFieldValue(GridViewInterface $gridView, Field $field, object $data): string |
| 109 | + { |
| 110 | + $renderedData = $this->gridRenderer->renderField($gridView, $field, $data); |
| 111 | + $renderedData = str_replace(\PHP_EOL, '', $renderedData); |
| 112 | + |
| 113 | + return trim(strip_tags($renderedData)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param Field[] $fields |
| 118 | + * |
| 119 | + * @return Field[] |
| 120 | + */ |
| 121 | + private function sortFields(array $fields): array |
| 122 | + { |
| 123 | + $sortedFields = $fields; |
| 124 | + |
| 125 | + uasort($sortedFields, fn (Field $fieldA, Field $fieldB) => $fieldA->getPosition() <=> $fieldB->getPosition()); |
| 126 | + |
| 127 | + return $sortedFields; |
| 128 | + } |
| 129 | +} |
0 commit comments