Skip to content

Instantly share code, notes, and snippets.

@n1215
Last active May 9, 2019 12:07
Show Gist options
  • Save n1215/9f7920447e5c01f8403786450cc0b772 to your computer and use it in GitHub Desktop.
Save n1215/9f7920447e5c01f8403786450cc0b772 to your computer and use it in GitHub Desktop.
LaraSeedGraph_idea
<?php
use FlowFactory as Flow;
use SourceFactory as Source;
use SinkFactory as Sink;
$builder = new GraphBuilder();
/** @var GraphInterface $seedGraph */
$seedGraph = $builder
->from(Source::csv($filePath))
->via(Flow::each(function (array $row, int $index) {
info($index, $row);
}))
->via(Flow::map(function (array $row) {
$row['hoge'] = 'hoge';
return $row;
}))
->via(Flow::filter(function (array $row) {
return array_key_exists($row['fuga']);
}))
->via(Flow::validation($rules, $messages, $attributes))
->to(Sink::buklInsertToDB($model, 100))
->on(function (Started $ev) {
echo 'start seeding...' . PHP_EOL;
\DB::beginTransaction();
})
->on(function (Completed $ev) {
\DB::commit();
echo 'complete!' . PHP_EOL;
})
->catch(function (HogeException $e) {
\DB::rollBack();
echo 'error! : ' . $e->getMessage() . PHP_EOL;
})
->finally(function () {
echo 'finished.' . PHP_EOL;
})
->build();
$seedGraph->run();
<?php
interface GraphEvent {
}
class Started implements GraphEvent {
public function getGraph();
}
class RowProcessed implements GraphEvent {
public function getIndex();
public function getRow(): array;
}
class RowSkipped extends RowProcessed {
public function getIndex();
public function getRow(): array;
}
class Completed implements GraphEvent {
public function getGraph();
}
interface GraphInterface {
public function run(): void;
}
interface FlowInterface {
/**
* @param Iterable<array> $stream
* @return Iterable<array>
*/
public function pass(Iterable $stream): Iterable;
}
interface SinkInterface {
/**
* @param Iterable<array> $stream
*/
public function write(Iterable $stream): void;
}
interface SourceInterface {
/**
* @return Iterable<array> $stream
*/
public function read(): Iterable;
}
interface GraphBuilderInterface {
public function from(SourceInterface): SourceBluePrintInterface;
}
interface SourceBluePrintInterface {
public function via(FlowInterface $flow): SourceBluePrintInterface;
public function to(SinkInterface $sink): GraphBluePrintInterface;
}
interface GraphBluePrintInterface {
public function on($eventClass, callable $eventListener): GraphBluePrintInterface;
public function catch(callable $errorHandler): GraphBluePrintInterface;
public function finally(callable $finalHandler): GraphBluePrintInterface;
public function build(): GraphInterface;
}
<?php
class SourceFactory {
public static csv(string $filePath): SourceInterface
{
}
}
class FlowFactory {
public static function map(callable $callable): FlowInterface
{
}
public static function filter(callable $callable): FlowInterface
{
}
public static function validation(array $rules, array $messages = [], array $customAttributes = []): FlowInterface
{
}
}
class SinkFactory {
public static function bulkIntertDB(\Illuminate\Eloquent\Model $model): SinkInterface
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment