Пример #1
0
//Set up the StoryTeller - basically a StoryTeller is a generic message handler but specifically
//designed to handle a single Story configured with so called ChapterTemplates
$jsonToCSVStoryTeller = new \Prooph\Done\Story\StoryTeller($storyName, [new \Prooph\Done\Story\ChapterTemplate(\Prooph\Done\Story\ChapterName::fromString('Merge Json Files'), new \Prooph\Done\Message\MessageTemplate(\ProophExample\Done\JsonToCSVStory\MergeJsonFilesChapter\MergeJsonFiles::class, $messageFactory, ['json_files_directory' => __DIR__ . DIRECTORY_SEPARATOR . 'data', 'to_filename' => 'users.json', 'file_pattern' => '/^user[\\d]{1,}\\.json$/']), new \ProophExample\Done\JsonToCSVStory\IsStartStoryCommand(), new \ProophExample\Done\JsonToCSVStory\IsInfoSuccessEvent()), new \Prooph\Done\Story\ChapterTemplate(\Prooph\Done\Story\ChapterName::fromString('Convert Json to CSV'), new \Prooph\Done\Message\MessageTemplate(\ProophExample\Done\JsonToCSVStory\JsonToCSVChapter\ConvertJsonToCSV::class, $messageFactory, [], [], function (\Prooph\Done\ChapterLogger\Event\Info $previousMessage, \ArrayObject $payload, \ArrayObject $metadata) {
    //We extract the generated filename from the previous event
    //to use it as input file for the next chapter command
    $inputFile = $previousMessage->payload()['context']['to_file_path'];
    $outputFile = str_replace('.json', '.csv', $inputFile);
    //Payload and metadata for the next chapter command are passed as \ArrayObjects
    //to the PayloadGenerator callback so that the callback can manipulate them both
    $payload['input_file'] = $inputFile;
    $payload['output_file'] = $outputFile;
}), new \ProophExample\Done\JsonToCSVStory\IsInfoSuccessEvent(), new \ProophExample\Done\JsonToCSVStory\IsInfoSuccessEvent())], $storyLog, $delayedCommandBus, $messageFactory, $messageConverter);
//We route the start command of the story to the responsible story teller
$commandRouter->route(\ProophExample\Done\JsonToCSVStory\StartStory::class)->to($jsonToCSVStoryTeller);
//As we perform the chapter in the same process as handling the Story
//We can use the StoryTellerBackend and pass it to the ChapterCommandHandlers
//If the chapters would be handled in child processes we would use
//a DoneBackend implementation that uses an event bus + async message producer
//to send logged events back to main process that handles the Story
$doneBackend = new \Prooph\Done\StoryTellerBackend($eventStore);
$doneBackend->registerStoryTeller($storyName, $jsonToCSVStoryTeller);
$mergeJsonFilesHandler = new \ProophExample\Done\JsonToCSVStory\MergeJsonFilesChapter\MergeJsonFilesHandler($doneBackend);
$commandRouter->route(\ProophExample\Done\JsonToCSVStory\MergeJsonFilesChapter\MergeJsonFiles::class)->to($mergeJsonFilesHandler);
$covertJsonToCSVHandler = new \ProophExample\Done\JsonToCSVStory\JsonToCSVChapter\ConvertJsonToCSVHandler($doneBackend);
$commandRouter->route(\ProophExample\Done\JsonToCSVStory\JsonToCSVChapter\ConvertJsonToCSV::class)->to($covertJsonToCSVHandler);
//Ok everything is set up. Let's run the story
$commandBus->dispatch(new \ProophExample\Done\JsonToCSVStory\StartStory([]));
//Check your terminal for all logged events.
//If the last event is a StoryWasDone event you should find a new CSV file
//in example/data called users.csv which contains two rows
//with the user data of the json user files.