Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Mar 8, 2021
1 parent 464227b commit 5b35af7
Show file tree
Hide file tree
Showing 30 changed files with 695 additions and 603 deletions.
29 changes: 18 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"record processor",
"php"
],
"type": "library",
"license": "MIT",
"authors": [
{
Expand All @@ -14,23 +15,29 @@
}
],
"require": {
"php": ">=7.0",
"psr/log": "^1.0",
"rodrigopedra/record-processor": "^0.9",
"illuminate/support": "^5.4|^6.0",
"illuminate/contracts": "^5.4|^6.0",
"illuminate/database": "^5.4|^6.0",
"illuminate/http": "^5.4|^6.0"
"php": "^7.4|^8.0",
"rodrigopedra/record-processor": "^1.0",
"illuminate/contracts": "^7.0|^8.0",
"illuminate/database": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0"
},
"require-dev": {
"symfony/console": "^5.1",
"symfony/http-foundation": "^5.1",
"symfony/process": "^5.1",
"symfony/var-dumper": "^5.1"
},
"autoload": {
"psr-4": {
"RodrigoPedra\\LaravelRecordProcessor\\": "src/LaravelRecordProcessor/",
"RodrigoPedra\\LaravelRecordProcessor\\": "src/",
"RodrigoPedra\\LaravelRecordProcessor\\Examples\\": "examples/"
}
},
"require-dev": {
"symfony/console": "^3.3|~4.0|~4.1|~4.2|~4.3",
"symfony/var-dumper": "^3.3|~4.0|~4.1|~4.2|~4.3"
"config": {
"sort-packages": true
},
"suggest": {
"symfony/http-foundation": "to use download file response"
},
"extra": {
"laravel": {
Expand Down
8 changes: 4 additions & 4 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Console\Application;
use RodrigoPedra\RecordProcessor\Examples\DownloadCommand;
use RodrigoPedra\LaravelRecordProcessor\Examples\DownloadCommand;
use RodrigoPedra\LaravelRecordProcessor\Examples\ExamplesCommand;
use Symfony\Component\Console\Application;

$application = new Application();

$application->add(new ExamplesCommand);
$application->add(new DownloadCommand);
$application->add(new ExamplesCommand());
$application->add(new DownloadCommand());

$application->run();
39 changes: 39 additions & 0 deletions examples/DownloadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace RodrigoPedra\LaravelRecordProcessor\Examples;

use RodrigoPedra\RecordProcessor\Examples\Loggers\ConsoleOutputLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class DownloadCommand extends Command
{
protected function configure()
{
$this->setName('download');

$this->setDescription('Start a server to showcase download usage');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$logger = new ConsoleOutputLogger($output);

$assetsDirectory = __DIR__ . \DIRECTORY_SEPARATOR . 'download';

$process = new Process(['php', '-S', 'localhost:8080', '-t', $assetsDirectory]);

$logger->info('Navigate in your browser to http://localhost:8080');
$logger->info('Type CTRL+C to exit');

$process->run(function ($type, $buffer) use ($logger) {
$logger->info($buffer);
});

$logger->info($process->getOutput());

return 0;
}
}
41 changes: 0 additions & 41 deletions examples/ExampleLaravelBuilderFormatter.php

This file was deleted.

12 changes: 5 additions & 7 deletions examples/ExampleLaravelBuilderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
class ExampleLaravelBuilderParser implements RecordParser
{
/**
* Generates Record objects from raw data
*
* @param Reader $reader
* @param mixed $rawContent
* @return Record
* @param \RodrigoPedra\RecordProcessor\Contracts\Reader $reader
* @param \Illuminate\Database\Eloquent\Model|\stdClass $rawContent
* @return \RodrigoPedra\RecordProcessor\Contracts\Record
*/
public function parseRecord(Reader $reader, $rawContent)
public function parseRecord(Reader $reader, $rawContent): Record
{
return new ExampleRecord([
return new ExampleRecord($rawContent->name, [
'name' => $rawContent->name,
'email' => $rawContent->email,
]);
Expand Down
32 changes: 32 additions & 0 deletions examples/ExampleLaravelBuilderSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace RodrigoPedra\LaravelRecordProcessor\Examples;

use RodrigoPedra\RecordProcessor\Contracts\Record;
use RodrigoPedra\RecordProcessor\Contracts\RecordSerializer;
use RodrigoPedra\RecordProcessor\Contracts\Serializer;
use RodrigoPedra\RecordProcessor\Records\RecordKeyAggregate;

class ExampleLaravelBuilderSerializer implements RecordSerializer
{
public function serializeRecord(Serializer $serializer, Record $record): bool
{
if (! $record->isValid()) {
return false;
}

$data = $record->toArray();

if ($record instanceof RecordKeyAggregate) {
$data = [
'name' => $record->key(),
'email' => \implode(', ',
\array_map(fn (Record $record) => $record->field('email'), $record->records())),
];
}

$serializer->append(new UserEloquentModel($data));

return true;
}
}
56 changes: 28 additions & 28 deletions examples/ExamplesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,38 @@
namespace RodrigoPedra\LaravelRecordProcessor\Examples;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Eloquent\Builder;
use RodrigoPedra\LaravelRecordProcessor\Configurators\Serializers\EloquentSerializerConfigurator;
use RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder;
use RodrigoPedra\RecordProcessor\Helpers\Writers\WriterConfigurator;
use RodrigoPedra\RecordProcessor\Examples\ExamplesCommand as BaseExamplesCommand;
use RodrigoPedra\RecordProcessor\ProcessorBuilder as BaseProcessorBuilder;

class ExamplesCommand extends BaseExamplesCommand
{
protected function getAvailableReaders()
protected function availableParsers(): string
{
return parent::getAvailableReaders() . '|eloquent|query-builder';
return parent::availableParsers() . '|eloquent|query-builder';
}

protected function getAvailableWriters()
protected function availableSerializers(): string
{
return parent::getAvailableWriters() . '|eloquent|query-builder';
return parent::availableSerializers() . '|eloquent|query-builder';
}

protected function makeBuilder()
protected function makeBuilder(): ProcessorBuilder
{
return new ProcessorBuilder;
return new ProcessorBuilder();
}

/**
* @param ProcessorBuilder $builder
* @param \RodrigoPedra\RecordProcessor\ProcessorBuilder&\RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder $builder
* @param string $reader
* @return mixed
* @return \RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder
*/
protected function readFrom($builder, $reader)
protected function readFrom(BaseProcessorBuilder $builder, string $reader): BaseProcessorBuilder
{
if ($reader === 'eloquent') {
$builder->usingParser(new ExampleLaravelBuilderParser);
$builder->withRecordParser(new ExampleLaravelBuilderParser());

$eloquentBuilder = $this->makeEloquentBuilder();
$eloquentBuilder->take(10);
Expand All @@ -41,7 +43,7 @@ protected function readFrom($builder, $reader)
}

if ($reader === 'query-builder') {
$builder->usingParser(new ExampleLaravelBuilderParser);
$builder->withRecordParser(new ExampleLaravelBuilderParser());

$eloquentBuilder = $this->makeEloquentBuilder();
$eloquentBuilder->take(10);
Expand All @@ -54,31 +56,31 @@ protected function readFrom($builder, $reader)
}

/**
* @param ProcessorBuilder $builder
* @param string $writer
* @return mixed
* @param \RodrigoPedra\RecordProcessor\ProcessorBuilder&\RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder $builder
* @param string $serializer
* @return \RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder
*/
protected function writeTo($builder, $writer)
protected function serializeTo(BaseProcessorBuilder $builder, string $serializer): BaseProcessorBuilder
{
if ($writer === 'eloquent') {
if ($serializer === 'eloquent') {
$eloquentBuilder = $this->makeEloquentBuilder();

return $builder->writeToEloquent($eloquentBuilder, function (WriterConfigurator $configurator) {
$configurator->outputModels(true);
$configurator->setRecordFormatter(new ExampleLaravelBuilderFormatter);
return $builder->writeToEloquent($eloquentBuilder, function (EloquentSerializerConfigurator $configurator) {
$configurator->withRecordSerializer(new ExampleLaravelBuilderSerializer());
$configurator->withShouldOutputModels(true);
});
}

if ($writer === 'query-builder') {
if ($serializer === 'query-builder') {
$eloquentBuilder = $this->makeEloquentBuilder();

return $builder->writeToQueryBuilder($eloquentBuilder->getQuery());
}

return parent::writeTo($builder, $writer);
return parent::serializeTo($builder, $serializer);
}

protected function storagePath($file)
protected function storagePath(string $file): string
{
return __DIR__ . '/../storage/' . $file;
}
Expand All @@ -87,7 +89,7 @@ protected function startLaravelConnection()
{
$this->makeConnection();

$capsule = new Capsule;
$capsule = new Capsule();

$capsule->addConnection([
'driver' => 'sqlite',
Expand All @@ -100,12 +102,10 @@ protected function startLaravelConnection()
$capsule->bootEloquent();
}

protected function makeEloquentBuilder()
public function makeEloquentBuilder(): Builder
{
$this->startLaravelConnection();

$model = new UserEloquentModel;

return $model->newQuery();
return UserEloquentModel::query();
}
}
24 changes: 24 additions & 0 deletions examples/download/download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

use RodrigoPedra\LaravelRecordProcessor\Examples\ExamplesCommand;
use RodrigoPedra\LaravelRecordProcessor\ProcessorBuilder;
use RodrigoPedra\RecordProcessor\Configurators\Serializers\SerializerConfigurator;
use RodrigoPedra\RecordProcessor\Stages\DownloadFileOutput;

$storagePath = __DIR__ . '/../../storage/';

$command = new ExamplesCommand();

$processor = (new ProcessorBuilder())
->readFromEloquent($command->makeEloquentBuilder())
->serializeToExcelFile($storagePath . 'output.xlsx', function (SerializerConfigurator $configurator) {
$configurator->withHeader(['name', 'email']);
})
->downloadFileOutput('report.xlsx', DownloadFileOutput::DELETE_FILE_AFTER_DOWNLOAD)
->build();

$processor->process();

exit;
6 changes: 6 additions & 0 deletions examples/download/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<body>
<a href="download.php">download excel</a>
</body>
</html>
23 changes: 23 additions & 0 deletions src/Configurators/Readers/EloquentReaderConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace RodrigoPedra\LaravelRecordProcessor\Configurators\Readers;

use Illuminate\Database\Eloquent\Builder;
use RodrigoPedra\LaravelRecordProcessor\Readers\EloquentReader;
use RodrigoPedra\RecordProcessor\Configurators\Readers\ReaderConfigurator;

/**
* @property \RodrigoPedra\LaravelRecordProcessor\Readers\EloquentReader $reader
*/
class EloquentReaderConfigurator extends ReaderConfigurator
{
public function __construct(EloquentReader $reader)
{
parent::__construct($reader);
}

public function builder(): Builder
{
return $this->reader->builder();
}
}
Loading

0 comments on commit 5b35af7

Please sign in to comment.