Skip to content

Commit

Permalink
♻️ Update to FlowFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
matyo91 committed Oct 27, 2024
1 parent a192c34 commit df7b8b6
Show file tree
Hide file tree
Showing 9 changed files with 737 additions and 705 deletions.
693 changes: 347 additions & 346 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ services:
- '../src/Entity/'
- '../src/Kernel.php'

Flow\FlowFactory:
autowire: true

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
11 changes: 9 additions & 2 deletions src/Command/CarbonImageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Flow\Driver\ReactDriver;
use Flow\Driver\SpatieDriver;
use Flow\Driver\SwooleDriver;
use Flow\Flow\Flow;
use Flow\FlowFactory;
use Flow\Ip;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -27,6 +27,13 @@
)]
class CarbonImageCommand extends Command
{
public function __construct(
private FlowFactory $flowFactory,
?string $name = null,
) {
parent::__construct($name);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand All @@ -39,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// 5 => new SpatieDriver(),
};

$flow = Flow::do(static function () use ($io) {
$flow = $this->flowFactory->create(static function () use ($io) {
yield new CarbonImageJob(__DIR__ . '/../../data/carbon-image/carbon-config.json');
yield static function (CarbonImage $carbonImage) use ($io) {
$io->info(sprintf('Finished %s %s', $carbonImage->code, $carbonImage->url));
Expand Down
13 changes: 10 additions & 3 deletions src/Command/FlowExemplesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Flow\Driver\ReactDriver;
use Flow\Driver\SpatieDriver;
use Flow\Driver\SwooleDriver;
use Flow\Flow\Flow;
use Flow\Flow\YFlow;
use Flow\FlowFactory;
use Flow\Ip;
use Flow\IpStrategy\MaxIpStrategy;
use Flow\Job\YJob;
Expand All @@ -42,6 +42,13 @@
)]
class FlowExemplesCommand extends Command
{
public function __construct(
private FlowFactory $flowFactory,
?string $name = null,
) {
parent::__construct($name);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand All @@ -60,10 +67,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
printf("- DataC(f)\n");

echo "begin - synchronous\n";
$asyncTask = static function () use ($driver) {
$asyncTask = function () use ($driver) {
echo "begin - flow asynchronous\n";

$flow = Flow::do(static function () use ($driver) {
$flow = $this->flowFactory->create(static function () use ($driver) {
yield [new Job1($driver), new ErrorJob1(), new MaxIpStrategy(2)];
yield [new Job2($driver), new ErrorJob2(), new MaxIpStrategy(2)];
yield new Job3();
Expand Down
11 changes: 9 additions & 2 deletions src/Command/PhpEtlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Command;

use Flow\Flow\Flow;
use Flow\FlowFactory;
use Flow\Ip;
use Generator;
use Kiboko\Component\Bucket\AcceptanceResultBucket;
Expand All @@ -31,9 +31,16 @@
)]
class PhpEtlCommand extends Command
{
public function __construct(
private FlowFactory $flowFactory,
?string $name = null,
) {
parent::__construct($name);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$flow = Flow::do(static function () {
$flow = $this->flowFactory->create(static function () {
yield static function ($data) {
$pipeline = new Pipeline(new PipelineRunner(new NullLogger()), new NullState());
$pipeline->extract(
Expand Down
5 changes: 3 additions & 2 deletions src/Command/ScrapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Fiber;
use Flow\AsyncHandler\DeferAsyncHandler;
use Flow\Driver\FiberDriver;
use Flow\Flow\Flow;
use Flow\FlowFactory;
use Flow\Ip;
use Flow\Job\YJob;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -38,6 +38,7 @@ class ScrapCommand extends Command
private SluggerInterface $slugger;

public function __construct(
private FlowFactory $flowFactory,
HttpClientInterface $httpClient,
ParameterBagInterface $parameterBag,
SluggerInterface $slugger
Expand Down Expand Up @@ -84,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$driver = new FiberDriver();

$flow = Flow::do(function () use ($io) {
$flow = $this->flowFactory->create(function () use ($io) {
yield new ScrapUrlsJob();
yield static function (array $urlDatas) use ($io) {
$io->writeln(sprintf('ScrapUrlsJob : Finished scrapping %d urls', count($urlDatas)));
Expand Down
5 changes: 3 additions & 2 deletions src/Flow/WaveFunctionCollapse/FlowFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
use App\Job\WaveFunctionCollapse\ImgJob;
use App\Job\WaveFunctionCollapse\Mp4Job;
use App\Model\WaveFunctionCollapse\Board;
use Flow\Flow\Flow;
use Flow\Flow\YFlow;
use Flow\FlowFactory as FlowFlowFactory;
use Flow\FlowInterface;
use Imagine\Gd\Imagine;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class FlowFactory
{
public function __construct(
private FlowFlowFactory $flowFactory,
#[Autowire('%kernel.project_dir%/assets')]
private string $assetsDir,
#[Autowire('%kernel.cache_dir%/wave_function_collapse')]
Expand All @@ -31,7 +32,7 @@ public function doMp4(DataSetEnumType $dataSet): FlowInterface
{
$imagine = new Imagine();

return Flow::do(function () use ($imagine, $dataSet) {
return $this->flowFactory->create(function () use ($imagine, $dataSet) {
yield static function ($data) {
[$width, $height, $dataSet] = $data;

Expand Down
8 changes: 6 additions & 2 deletions src/Twig/Components/WaveFunctionCollapse/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\EnumType\WaveFunctionCollapse\DataSetEnumType;
use App\Job\WaveFunctionCollapse\CollapseJob;
use App\Model\WaveFunctionCollapse\Board;
use Flow\Flow\Flow;
use Flow\FlowFactory;
use Flow\Ip;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
Expand All @@ -28,6 +28,10 @@ final class Ui
#[LiveProp]
public bool $pool = true;

public function __construct(
private FlowFactory $flowFactory,
) {}

public function mount(int $width, int $height): void
{
$this->board = new Board($width, $height);
Expand All @@ -43,7 +47,7 @@ public function reset(): void
#[LiveAction]
public function collapse(): void
{
$flow = Flow::do(function () {
$flow = $this->flowFactory->create(function () {
yield new CollapseJob();
yield function ($nextBoard) {
if ($nextBoard === null) {
Expand Down
Loading

0 comments on commit df7b8b6

Please sign in to comment.