Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php

return PhpCsFixer\Config::create()
->setRules(
[
'@Symfony' => true,
'ordered_imports' => true,
]
)
->setUsingCache(true)
->setRules(['@Symfony' => true, 'array_syntax' => ['syntax' => 'short'], 'ordered_imports' => true])
->setCacheFile(__DIR__.'/.php_cs.cache');
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

before_install:
- |
Expand All @@ -15,6 +14,3 @@ before_install:

install:
- composer install --no-interaction --prefer-dist

notifications:
email: joris.w.dewit@gmail.com
49 changes: 49 additions & 0 deletions AvroCsvEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle;

/**
* Contains all events thrown in the AvroCsv bundle.
*
* @author Steffen Roßkamp <steffen.rosskamp@gimmickmedia.de>
*/
final class AvroCsvEvents
{
/**
* The EXPORT event occurs after the exporter has been initialized.
*
* This event allows you to alter the exporter or its query builder.
*
* @Event("Avro\CsvBundle\Event\ExportEvent")
*/
const EXPORT = 'avro_csv.exporter_export';
/**
* The EXPORTED event occurs after the exporter has generated the content.
*
* This event allows you to further process the exported content.
*
* @Event("Avro\CsvBundle\Event\ExportedEvent")
*/
const EXPORTED = 'avro_csv.exporter_exported';
/**
* The ROW_ADDED event occurs after the importer has generated the entity.
*
* This event allows you to alter or nullify the entity.
*
* @Event("Avro\CsvBundle\Event\RowAddedEvent")
*/
const ROW_ADDED = 'avro_csv.row_added';
/**
* The ROW_ERROR event occurs when the importer could not import an row.
*
* This event allows you to further process the erroneous row.
*
* @Event("Avro\CsvBundle\Event\RowErrorEvent")
*/
const ROW_ERROR = 'avro_csv.row_error';
}
6 changes: 4 additions & 2 deletions Controller/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Avro\CsvBundle\Controller;

use Avro\CsvBundle\AvroCsvEvents;
use Avro\CsvBundle\Event\ExportedEvent;
use Avro\CsvBundle\Event\ExportEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
Expand All @@ -21,6 +22,7 @@
class ExportController implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* Export a db table.
*
Expand All @@ -36,11 +38,11 @@ public function exportAction($alias)
$exporter->init($class);

$dispatcher = $this->container->get('event_dispatcher');
$dispatcher->dispatch('avro_csv.exporter_export', new ExportEvent($exporter));
$dispatcher->dispatch(AvroCsvEvents::EXPORT, new ExportEvent($exporter));

$content = $exporter->getContent();

$dispatcher->dispatch('avro_csv.exporter_exported', new ExportedEvent($content));
$dispatcher->dispatch(AvroCsvEvents::EXPORTED, new ExportedEvent($content));

$response = new Response($content);
$response->headers->set('Content-Type', 'application/csv');
Expand Down
19 changes: 10 additions & 9 deletions Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class ImportController implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* Upload a csv.
*
Expand All @@ -33,12 +34,12 @@ public function uploadAction($alias)
{
$fieldChoices = $this->container->get('avro_csv.field_retriever')->getFields($this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)), 'title', true);

$form = $this->container->get('form.factory')->create(ImportFormType::class, null, array('field_choices' => $fieldChoices));
$form = $this->container->get('form.factory')->create(ImportFormType::class, null, ['field_choices' => $fieldChoices]);

return $this->container->get('templating')->renderResponse('@AvroCsv/Import/upload.html.twig', array(
return $this->container->get('templating')->renderResponse('@AvroCsv/Import/upload.html.twig', [
'form' => $form->createView(),
'alias' => $alias,
));
]);
}

/**
Expand All @@ -53,7 +54,7 @@ public function mappingAction(Request $request, $alias)
{
$fieldChoices = $this->container->get('avro_csv.field_retriever')->getFields($this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)), 'title', true);

$form = $this->container->get('form.factory')->create(ImportFormType::class, null, array('field_choices' => $fieldChoices));
$form = $this->container->get('form.factory')->create(ImportFormType::class, null, ['field_choices' => $fieldChoices]);

if ('POST' == $request->getMethod()) {
$form->handleRequest($request);
Expand All @@ -73,19 +74,19 @@ public function mappingAction(Request $request, $alias)
$headers = $this->container->get('avro_csv.importer')->toFormFieldName($fileHeaders);

// Recreate form and create proper fields child for each header
$form = $this->container->get('form.factory')->create(ImportFormType::class, null, array('field_choices' => $fieldChoices));
$form = $this->container->get('form.factory')->create(ImportFormType::class, null, ['field_choices' => $fieldChoices]);
$form->get('fields')->setData(array_fill_keys((array) $headers, null));
$form->handleRequest($request);

$rows = $reader->getRows($this->container->getParameter('avro_csv.sample_count'));

return $this->container->get('templating')->renderResponse('@AvroCsv/Import/mapping.html.twig', array(
return $this->container->get('templating')->renderResponse('@AvroCsv/Import/mapping.html.twig', [
'form' => $form->createView(),
'alias' => $alias,
'headers' => array_combine((array) $headers, (array) $fileHeaders),
'headersJson' => json_encode($this->container->get('avro_case.converter')->toTitleCase($fileHeaders), JSON_FORCE_OBJECT),
'rows' => $rows,
));
]);
}
} else {
return new RedirectResponse($this->container->get('router')->generate($this->container->getParameter(sprintf('avro_csv.objects.%s.redirect_route', $alias))));
Expand All @@ -104,7 +105,7 @@ public function processAction(Request $request, $alias)
{
$fieldChoices = $this->container->get('avro_csv.field_retriever')->getFields($this->container->getParameter(sprintf('avro_csv.objects.%s.class', $alias)), 'title', true);

$form = $this->container->get('form.factory')->create(ImportFormType::class, null, array('field_choices' => $fieldChoices));
$form = $this->container->get('form.factory')->create(ImportFormType::class, null, ['field_choices' => $fieldChoices]);

if ('POST' == $request->getMethod()) {
$form->handleRequest($request);
Expand All @@ -124,7 +125,7 @@ public function processAction(Request $request, $alias)

$importer->import($form['fields']->getData());

$this->container->get('session')->getFlashBag()->set('success', $importer->getImportCount().' items imported.');
$this->container->get('session')->getFlashBag()->set('success', $importer->getImportCount().' items imported. '.$importer->getImportErrors().' errors.');
} else {
$this->container->get('session')->getFlashBag()->set('error', 'Import failed. Please try again.');
}
Expand Down
Loading