Skip to content

Commit

Permalink
chevere 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 17, 2023
1 parent 0cd73bb commit e32f93a
Show file tree
Hide file tree
Showing 23 changed files with 42 additions and 43 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
```

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion demo/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class ImageResize extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return string();
}
Expand Down
2 changes: 1 addition & 1 deletion demo/StoreFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class StoreFile extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return null();
}
Expand Down
4 changes: 2 additions & 2 deletions demo/hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class GreetAction extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return string();
}
Expand All @@ -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;
4 changes: 2 additions & 2 deletions demo/image-resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
];
$run = run($workflow, $variables);
echo <<<PLAIN
thumbFile: {$run->getResponse('thumb')->string()}
posterFile: {$run->getResponse('poster')->string()}
thumbFile: {$run->getReturn('thumb')->string()}
posterFile: {$run->getReturn('poster')->string()}
PLAIN;
2 changes: 1 addition & 1 deletion src/Actions/ClosureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
) {
}

public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return null();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/RunInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -155,7 +155,7 @@ private function storeReferences(string $job, JobInterface $item): void
$this->references = $this->references
->withPut(
strval(response($job)),
$acceptResponse,
$return,
);
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

Check failure on line 96 in src/Run.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Trying to invoke Chevere\Parameter\Interfaces\ParameterInterface but it might not be a callable.

Check failure on line 96 in src/Run.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Trying to invoke Chevere\Parameter\Interfaces\ParameterInterface but it might not be a callable.
$new->map = $new->map->withPut($job, $response);

return $new;
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()];
}

/**
Expand All @@ -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%', [
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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()
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/src/TestActionAppendString.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

final class TestActionAppendString extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return string();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionFileWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

final class TestActionFileWrite extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return null();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionIntToString.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

final class TestActionIntToString extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return string();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionNoParamsBoolResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

final class TestActionNoParamsBoolResponses extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return arrayp(
true: bool(),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionNoParamsIntResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

final class TestActionNoParamsIntResponse extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return arrayp(
id: int()
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionParamFooResponse1.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class TestActionParamFooResponse1 extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return arrayp(response1: string());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionParamFooResponseBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class TestActionParamFooResponseBar extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return arrayp(
bar: string('/^bar$/'),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestActionParamsFooBarResponse2.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class TestActionParamsFooBarResponse2 extends Action
{
public static function acceptResponse(): ParameterInterface
public static function return(): ParameterInterface
{
return arrayp(response2: string());
}
Expand Down

0 comments on commit e32f93a

Please sign in to comment.