From e32f93a2daf5a77b49c46a6bd240da7358e37337 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:11:36 -0300 Subject: [PATCH] chevere 4.0 --- README.md | 6 +++--- demo/ImageResize.php | 2 +- demo/StoreFile.php | 2 +- demo/hello-world.php | 4 ++-- demo/image-resize.php | 4 ++-- src/Actions/ClosureAction.php | 2 +- src/Interfaces/RunInterface.php | 2 +- src/Jobs.php | 12 ++++++------ src/Run.php | 5 ++--- src/Runner.php | 10 +++++----- src/Workflow.php | 2 +- src/functions.php | 2 +- tests/RunTest.php | 4 ++-- tests/RunnerTest.php | 8 ++++---- tests/WorkflowTest.php | 4 ++-- tests/src/TestActionAppendString.php | 2 +- tests/src/TestActionFileWrite.php | 2 +- tests/src/TestActionIntToString.php | 2 +- tests/src/TestActionNoParamsBoolResponses.php | 2 +- tests/src/TestActionNoParamsIntResponse.php | 2 +- tests/src/TestActionParamFooResponse1.php | 2 +- tests/src/TestActionParamFooResponseBar.php | 2 +- tests/src/TestActionParamsFooBarResponse2.php | 2 +- 23 files changed, 42 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 7dd933f..ee7684b 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ $variables = [ 'username' => $argv[1] ?? 'Walala', ]; $run = run($workflow, $variables); -echo $run->getResponse('greet')->string(); +echo $run->getReturn('greet')->string(); // Hello, Walala! ``` @@ -134,10 +134,10 @@ $variables = [ $run = run($workflow, ...$variables); ``` -Use `getResponse` to retrieve a job response as a `CastArgument` object which can be used to get a typed response. +Use `getReturn` to retrieve a job response as a `CastArgument` object which can be used to get a typed response. ```php -$thumbFile = $run->getResponse('thumb')->string(); +$thumbFile = $run->getReturn('thumb')->string(); ``` ### Notes on async diff --git a/demo/ImageResize.php b/demo/ImageResize.php index 45ca509..0fcdb5c 100644 --- a/demo/ImageResize.php +++ b/demo/ImageResize.php @@ -19,7 +19,7 @@ class ImageResize extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return string(); } diff --git a/demo/StoreFile.php b/demo/StoreFile.php index 8e1e98c..5653822 100644 --- a/demo/StoreFile.php +++ b/demo/StoreFile.php @@ -19,7 +19,7 @@ class StoreFile extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return null(); } diff --git a/demo/hello-world.php b/demo/hello-world.php index ad3f28f..f11c7dd 100644 --- a/demo/hello-world.php +++ b/demo/hello-world.php @@ -30,7 +30,7 @@ class GreetAction extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return string(); } @@ -50,5 +50,5 @@ protected function run(string $username): string $run = run($workflow, [ 'username' => $argv[1] ?? 'Walala', ]); -echo $run->getResponse('greet')->string(); +echo $run->getReturn('greet')->string(); echo PHP_EOL; diff --git a/demo/image-resize.php b/demo/image-resize.php index dcc4d32..0318ad4 100644 --- a/demo/image-resize.php +++ b/demo/image-resize.php @@ -56,7 +56,7 @@ ]; $run = run($workflow, $variables); echo <<getResponse('thumb')->string()} -posterFile: {$run->getResponse('poster')->string()} +thumbFile: {$run->getReturn('thumb')->string()} +posterFile: {$run->getReturn('poster')->string()} PLAIN; diff --git a/src/Actions/ClosureAction.php b/src/Actions/ClosureAction.php index 3fa19ab..07c8fb1 100644 --- a/src/Actions/ClosureAction.php +++ b/src/Actions/ClosureAction.php @@ -25,7 +25,7 @@ public function __construct( ) { } - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return null(); } diff --git a/src/Interfaces/RunInterface.php b/src/Interfaces/RunInterface.php index f34bcbc..df44b20 100644 --- a/src/Interfaces/RunInterface.php +++ b/src/Interfaces/RunInterface.php @@ -58,5 +58,5 @@ public function withSkip(string ...$job): self; /** * Provides access to the ResponseInterface instance for the given `$job`. */ - public function getResponse(string $job): CastInterface; + public function getReturn(string $job): CastInterface; } diff --git a/src/Jobs.php b/src/Jobs.php index 836fbf0..4eb05c7 100644 --- a/src/Jobs.php +++ b/src/Jobs.php @@ -142,9 +142,9 @@ private function putAdded(JobInterface ...$job): void private function storeReferences(string $job, JobInterface $item): void { $action = $item->action(); - $acceptResponse = $action::acceptResponse(); - if ($acceptResponse instanceof ParametersAccessInterface) { - foreach ($acceptResponse->parameters() as $key => $parameter) { + $return = $action::return(); + if ($return instanceof ParametersAccessInterface) { + foreach ($return->parameters() as $key => $parameter) { $this->references = $this->references ->withPut( strval(response($job, $key)), @@ -155,7 +155,7 @@ private function storeReferences(string $job, JobInterface $item): void $this->references = $this->references ->withPut( strval(response($job)), - $acceptResponse, + $return, ); } } @@ -206,7 +206,7 @@ private function mapParameter( /** @var JobInterface $referenceJob */ $referenceJob = $this->map->get($value->job()); /** @var ParameterInterface $accept */ - $accept = $referenceJob->action()::acceptResponse(); + $accept = $referenceJob->action()::return(); if ($value->key() !== null) { if (! $accept instanceof ParametersAccessInterface) { throw new TypeError( @@ -270,7 +270,7 @@ private function handleRunIfReference(mixed $runIf): void return; } $action = $this->get($runIf->job())->action(); - $accept = $action::acceptResponse(); + $accept = $action::return(); if ($runIf->key() !== null) { if (! $accept instanceof ParametersAccessInterface) { throw new TypeError( diff --git a/src/Run.php b/src/Run.php index d898289..ff0663e 100644 --- a/src/Run.php +++ b/src/Run.php @@ -26,7 +26,6 @@ use OverflowException; use Ramsey\Uuid\Uuid; use function Chevere\Message\message; -use function Chevere\Parameter\assertArgument; use function Chevere\VarSupport\deepCopy; final class Run implements RunInterface @@ -94,7 +93,7 @@ public function withResponse(string $job, CastInterface $response): RunInterface $this->assertNoSkipOverflow($job, message('Job %job% is skipped')); $new = clone $this; $new->workflow->jobs()->get($job); - assertArgument($new->workflow->getJobResponseParameter($job), $response->mixed()); + $new->workflow->getJobResponseParameter($job)($response->mixed()); $new->map = $new->map->withPut($job, $response); return $new; @@ -112,7 +111,7 @@ public function withSkip(string ...$job): RunInterface return $new; } - public function getResponse(string $job): CastInterface + public function getReturn(string $job): CastInterface { return $this->map->get($job); } diff --git a/src/Runner.php b/src/Runner.php index 9c0e674..46f8a34 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -77,7 +77,7 @@ public function withRunJob(string $name): RunnerInterface } foreach ($job->dependencies() as $dependency) { try { - $new->run()->getResponse($dependency); + $new->run()->getReturn($dependency); } catch (OutOfBoundsException) { $new->addJobSkip($name); @@ -97,7 +97,7 @@ private function getRunIfCondition(VariableInterface|ResponseReferenceInterface /** @var boolean */ return $runIf instanceof VariableInterface ? $this->run->arguments()->required($runIf->__toString())->bool() - : $this->run->getResponse($runIf->job())->array()[$runIf->key()]; + : $this->run->getReturn($runIf->job())->array()[$runIf->key()]; } /** @@ -108,7 +108,7 @@ private function getActionResponse( array $arguments ): CastInterface { try { - return $action->getResponse(...$arguments); + return $action->__invoke(...$arguments); } catch (Throwable $e) { // @codeCoverageIgnoreStart $actionTrace = $e->getTrace()[1] ?? []; $fileLine = strtr('%file%:%line%', [ @@ -152,13 +152,13 @@ private function getJobArguments(JobInterface $job): array } /** @var ResponseReferenceInterface $value */ if ($value->key() === null) { - $arguments[$name] = $this->run->getResponse($value->job())->mixed(); + $arguments[$name] = $this->run->getReturn($value->job())->mixed(); continue; } /** @var ResponseReferenceInterface $value */ - $arguments[$name] = $this->run->getResponse($value->job())->array()[$value->key()]; + $arguments[$name] = $this->run->getReturn($value->job())->array()[$value->key()]; } return $arguments; diff --git a/src/Workflow.php b/src/Workflow.php index b73b240..6d3a80f 100644 --- a/src/Workflow.php +++ b/src/Workflow.php @@ -92,7 +92,7 @@ private function putParameters(string $name, JobInterface $job): void { $action = $job->action(); $parameters = getParameters($action::class); - $this->provided = $this->provided->withPut($name, $action::acceptResponse()); + $this->provided = $this->provided->withPut($name, $action::return()); foreach ($job->arguments() as $argument => $value) { try { $parameter = $parameters->get($argument); diff --git a/src/functions.php b/src/functions.php index 91e9717..036d7e4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -81,7 +81,7 @@ function variable(string $name): VariableInterface function runnerForJob(RunnerInterface $runner, string $job): RunnerInterface { try { - $runner->run()->getResponse($job); + $runner->run()->getReturn($job); return $runner; } catch (Throwable) { diff --git a/tests/RunTest.php b/tests/RunTest.php index d85fe6d..c1aba5e 100644 --- a/tests/RunTest.php +++ b/tests/RunTest.php @@ -50,7 +50,7 @@ public function testConstruct(): void $this->assertSame($workflow, $run->workflow()); $this->assertSame($arguments, $run->arguments()->toArray()); $this->expectException(OutOfBoundsException::class); - $run->getResponse('not-found'); + $run->getReturn('not-found'); } public function testWithStepResponse(): void @@ -75,7 +75,7 @@ public function testWithStepResponse(): void $run = (new Run($workflow, ...$arguments)); $workflowRunWithStepResponse = $run->withResponse('job0', new Cast([])); $this->assertNotSame($run, $workflowRunWithStepResponse); - $this->assertSame([], $workflowRunWithStepResponse->getResponse('job0')->array()); + $this->assertSame([], $workflowRunWithStepResponse->getReturn('job0')->array()); } public function testWithAddedNotFound(): void diff --git a/tests/RunnerTest.php b/tests/RunnerTest.php index 93a0769..0bd0008 100644 --- a/tests/RunnerTest.php +++ b/tests/RunnerTest.php @@ -197,7 +197,7 @@ public function testWithRunIfVariable(): void $action = $job->action(); $this->assertSame( $action->run(), - $runner->run()->getResponse('job1')->array() + $runner->run()->getReturn('job1')->array() ); $arguments = [ $name => false, @@ -209,7 +209,7 @@ public function testWithRunIfVariable(): void $run = run($workflow, ...$arguments); $this->assertSame($workflow->jobs()->keys(), $runner->run()->skip()->toArray()); $this->expectException(OutOfBoundsException::class); - $runner->run()->getResponse('job1'); + $runner->run()->getReturn('job1'); } public function testRunIfReference(): void @@ -228,7 +228,7 @@ public function testRunIfReference(): void $runner = new Runner($run); foreach ($workflow->jobs()->keys() as $name) { $runner = $runner->withRunJob($name); - $runner->run()->getResponse($name)->array(); + $runner->run()->getReturn($name)->array(); } $workflow = workflow( job1: $job1, @@ -256,7 +256,7 @@ private function assertExpectedRun(array $jobs, array $runArguments, RunInterfac $action = $job->action(); $this->assertSame( $action->run(...$runArguments[$name]), - $run->getResponse($name)->array() + $run->getReturn($name)->array() ); } } diff --git a/tests/WorkflowTest.php b/tests/WorkflowTest.php index ba9edde..0a87c61 100644 --- a/tests/WorkflowTest.php +++ b/tests/WorkflowTest.php @@ -105,7 +105,7 @@ public function testWithReference(): void $this->assertTrue($workflow->jobs()->has('job1')); $this->assertContains('job1', $workflow->jobs()->get('job2')->dependencies()); $run = run($workflow); - $this->assertSame('test!!', $run->getResponse('job2')->string()); + $this->assertSame('test!!', $run->getReturn('job2')->string()); } public function testIntToString(): void @@ -117,6 +117,6 @@ public function testIntToString(): void ) ); $run = run($workflow); - $this->assertSame('1234', $run->getResponse('toString')->string()); + $this->assertSame('1234', $run->getReturn('toString')->string()); } } diff --git a/tests/src/TestActionAppendString.php b/tests/src/TestActionAppendString.php index e66f29f..40e9a27 100644 --- a/tests/src/TestActionAppendString.php +++ b/tests/src/TestActionAppendString.php @@ -19,7 +19,7 @@ final class TestActionAppendString extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return string(); } diff --git a/tests/src/TestActionFileWrite.php b/tests/src/TestActionFileWrite.php index 56dcef4..0a87d21 100644 --- a/tests/src/TestActionFileWrite.php +++ b/tests/src/TestActionFileWrite.php @@ -20,7 +20,7 @@ final class TestActionFileWrite extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return null(); } diff --git a/tests/src/TestActionIntToString.php b/tests/src/TestActionIntToString.php index 30790d2..68c9b84 100644 --- a/tests/src/TestActionIntToString.php +++ b/tests/src/TestActionIntToString.php @@ -19,7 +19,7 @@ final class TestActionIntToString extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return string(); } diff --git a/tests/src/TestActionNoParamsBoolResponses.php b/tests/src/TestActionNoParamsBoolResponses.php index 7a9ffdb..dca6d6f 100644 --- a/tests/src/TestActionNoParamsBoolResponses.php +++ b/tests/src/TestActionNoParamsBoolResponses.php @@ -20,7 +20,7 @@ final class TestActionNoParamsBoolResponses extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return arrayp( true: bool(), diff --git a/tests/src/TestActionNoParamsIntResponse.php b/tests/src/TestActionNoParamsIntResponse.php index 862c6ce..3195f06 100644 --- a/tests/src/TestActionNoParamsIntResponse.php +++ b/tests/src/TestActionNoParamsIntResponse.php @@ -20,7 +20,7 @@ final class TestActionNoParamsIntResponse extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return arrayp( id: int() diff --git a/tests/src/TestActionParamFooResponse1.php b/tests/src/TestActionParamFooResponse1.php index d606c5b..e088e72 100644 --- a/tests/src/TestActionParamFooResponse1.php +++ b/tests/src/TestActionParamFooResponse1.php @@ -20,7 +20,7 @@ class TestActionParamFooResponse1 extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return arrayp(response1: string()); } diff --git a/tests/src/TestActionParamFooResponseBar.php b/tests/src/TestActionParamFooResponseBar.php index 26ffe5b..466e0c4 100644 --- a/tests/src/TestActionParamFooResponseBar.php +++ b/tests/src/TestActionParamFooResponseBar.php @@ -22,7 +22,7 @@ class TestActionParamFooResponseBar extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return arrayp( bar: string('/^bar$/'), diff --git a/tests/src/TestActionParamsFooBarResponse2.php b/tests/src/TestActionParamsFooBarResponse2.php index 076aedb..985c9a7 100644 --- a/tests/src/TestActionParamsFooBarResponse2.php +++ b/tests/src/TestActionParamsFooBarResponse2.php @@ -20,7 +20,7 @@ class TestActionParamsFooBarResponse2 extends Action { - public static function acceptResponse(): ParameterInterface + public static function return(): ParameterInterface { return arrayp(response2: string()); }