Skip to content

Commit

Permalink
Enable altering of exported content
Browse files Browse the repository at this point in the history
  • Loading branch information
MisatoTremor committed Nov 20, 2019
1 parent b4f30f6 commit d4cce48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Controller/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public function __construct(EventDispatcherInterface $eventDispatcher, ExporterI
*
* @return Response
*/
public function exportAction($alias)
public function exportAction($alias): Response
{
$class = $this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias));

$this->exporter->init($class);

$this->eventDispatcher->dispatch(AvroCsvEvents::EXPORT, new ExportEvent($this->exporter));

$content = $this->exporter->getContent();
$exportedEvent = new ExportedEvent($this->exporter->getContent());

$this->eventDispatcher->dispatch(AvroCsvEvents::EXPORTED, new ExportedEvent($content));
$this->eventDispatcher->dispatch(AvroCsvEvents::EXPORTED, $exportedEvent);

$response = new Response($content);
$response = new Response($exportedEvent->getContent());
$response->headers->set('Content-Type', 'application/csv');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s.csv"', $alias));

Expand Down
6 changes: 3 additions & 3 deletions Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(
*
* @return Response
*/
public function uploadAction($alias)
public function uploadAction($alias): Response
{
$fieldChoices = $this->fieldRetriever->getFields(
$this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)),
Expand Down Expand Up @@ -91,7 +91,7 @@ public function uploadAction($alias)
*
* @return Response
*/
public function mappingAction(Request $request, $alias)
public function mappingAction(Request $request, $alias): Response
{
$fieldChoices = $this->fieldRetriever->getFields(
$this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)),
Expand Down Expand Up @@ -156,7 +156,7 @@ public function mappingAction(Request $request, $alias)
*
* @return Response
*/
public function processAction(Request $request, $alias)
public function processAction(Request $request, $alias): Response
{
$fieldChoices = $this->fieldRetriever->getFields(
$this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)),
Expand Down
10 changes: 10 additions & 0 deletions Event/ExportedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ public function getContent(): string
{
return $this->content;
}

/**
* Set the csv data.
*
* @param string $content
*/
public function setContent(string $content): void
{
$this->content = $content;
}
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ This bundle provides some simple exporting functionality.
Navigating to "/export/your-alias" will export all of your data to a csv and allow
you to download it from the browser.

You can customize the export query builder and the exported data by listening to the
corresponding events (See ``Avro\CsvBundle\AvroCsvEvents``).

If you want to customize data returned, just create your own controller action and grab
the queryBuilder from the exporter and add your constraints before calling "getContent()".

Expand All @@ -281,12 +284,12 @@ class ExportController extends AbstractController
/**
* Export a db table.
*
* @param ExporterInterface $alias The exporter
* @param string $alias The objects alias
* @param ExporterInterface $exporter The exporter
* @param string $alias The objects alias
*
* @return Response
*/
public function exportAction(ExporterInterface $exporter), string $alias): Response
public function exportAction(ExporterInterface $exporter, string $alias): Response
{
$class = $this->getParameter(sprintf('avro_csv.objects.%s.class', $alias));

Expand Down

0 comments on commit d4cce48

Please sign in to comment.