8000 Replace SpoonFileCSV by tijsverkoyen · Pull Request #3160 · forkcms/forkcms · GitHub
[go: up one dir, main page]

Skip to content

Replace SpoonFileCSV#3160

Merged
carakas merged 13 commits intomasterfrom
replace-spooncsv
Aug 12, 2020
Merged

Replace SpoonFileCSV#3160
carakas merged 13 commits intomasterfrom
replace-spooncsv

Conversation

@tijsverkoyen
Copy link
Member
@tijsverkoyen tijsverkoyen commented Aug 5, 2020

Type

  • Enhancement

Resolves the following issues

Pull request description

SpoonFileCSV is deprecated so it should be replaced with something that is more maintained.

With this PR we introduce the usage of PhpSpreadsheet. I choose this one over league/csv, as the later one does not really have a lot of features, and does not really integrate in the Symfony responses, per example: outputting a CSV file won't return a string, but just outputs the CSV and stop the code.

Writing CSV files

So instead of working with arrays you should create a SpreadSheet instance first,
check the official documentation.

The easiest way to convert existing code is:

$spreadSheet = new Spreadsheet();
$sheet = $spreadSheet->getActiveSheet();
$sheet->fromArray($headers, null, 'A1');
$sheet->fromArray($rows, null, 'A2');

A service called ForkCMS\Utility\Csv\Writer is added, which allows you to force the download of a spreadsheet in CSV-format thru a Symfony StreamedResponse. If you want to force a download you can use the code below in the backend:

    throw new RedirectException(
        'Return the csv data',
        $this->get(Writer::class)
            ->forBackendUser(Authentication::getUser())
            ->output($spreadSheet, date('Ymd_His') . '.csv')
    );

We will use PhpSpreadsheet as a replacement for SpoonFileCSV.
As a benefit PhpSpreadsheet has more features, which may come in
handy later on.
This service will enable you to get a preconfigured CSV writer.
…with the usage of the ForkCMS\Utility\Csv\Writer service
@tijsverkoyen tijsverkoyen added FormBuilder Profiles Has PR deprecation indicates that this issue/pr deprecates somethign labels Aug 5, 2020
@tijsverkoyen tijsverkoyen added this to the 5.9.0 milestone Aug 5, 2020
@tijsverkoyen tijsverkoyen requested a review from carakas August 5, 2020 15:33
@tijsverkoyen tijsverkoyen requested a review from a team as a code owner August 5, 2020 15:33
* This is our extended version of SpoonFileCSV
* @deprecated remove this in Fork 6, just use ForkCMS\Utility\Csv\Writer
*/
class Csv extends \SpoonFileCSV
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the parent class?
As far as I know this is not backwards compatible

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a142503


public function forBackendUser(User $user): self
{
$this->options['Delimiter'] = $user->getSetting('csv_split_character');
Copy link
Member
@carakas carakas Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to clone the instance here and return the clone since using it after calling the forBackendUser will still run it with those changes otherwise

Copy link
Member Author
@tijsverkoyen tijsverkoyen Aug 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f941db1

Jelmer made a good point the service can be reused and setting the
options for a user would affect other usages.

I rewrote the whole class to be be more usefull and have no sideeffects.
This method is heavily inspired on importCsv. In essence it is a
working version. As at the moment I think importCsv will fail because
of fields that can't be NULL, but the code does not handle it.

I didn't fix importCsv because it would require database-structure changes
and won't be backwards compatible.
It introduces 2 methods:

* findColumnIndexes, which will return the column names for the PhpSpreadsheet
  for the given named columns. This will be used to check if the columns are
  present in a CSV file
* convertRowIntoMappedArray, which will convert a PhpSpreadsheet Row into a named
  array. At this moment we use a lot of array instead of objects so this will come
  in handy
@tijsverkoyen tijsverkoyen changed the title [WIP] Replace SpoonFileCSV Replace SpoonFileCSV Aug 6, 2020
@tijsverkoyen tijsverkoyen requested review from a team and carakas August 6, 2020 14:16
@carakas carakas merged commit ea01bb2 into master Aug 12, 2020
@carakas carakas deleted the replace-spooncsv branch August 12, 2020 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecation indicates that this issue/pr deprecates somethign FormBuilder Has PR Profiles

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change the implementation of Backend/Core/Engine/Csv away from spoon library

2 participants

0