Skip to content

Commit

Permalink
close #28, close #27
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed May 13, 2023
1 parent 25dc1ae commit d901dda
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 246 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
],
"require": {
"amphp/parallel": "^1.4",
"chevere/chevere": "^2.0.x-dev"
"chevere/chevere": "^3.0.x-dev"
},
"require-dev": {
"chevere/xr": "^0.7.0",
"chevere/var-dump": "^0.8.x-dev",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5",
"symplify/easy-coding-standard": "^11.1"
Expand Down
14 changes: 5 additions & 9 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

use Chevere\DataStructure\Interfaces\VectorInterface;
use Chevere\DataStructure\Map;
use function Chevere\DataStructure\mapToArray;
use Chevere\DataStructure\Traits\MapTrait;
use Chevere\DataStructure\Vector;
use function Chevere\DataStructure\vectorToArray;
use function Chevere\Message\message;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Workflow\Interfaces\GraphInterface;
Expand Down Expand Up @@ -60,9 +58,7 @@ public function withPut(
if ($new->map->has($name)) {
/** @var VectorInterface<string> $existing */
$existing = $new->map->get($name);
$existingArray = vectorToArray($existing);
$vectorArray = vectorToArray($vector);
$merge = array_merge($existingArray, $vectorArray);
$merge = array_merge($existing->toArray(), $vector->toArray());
$vector = new Vector(...$merge);
}
$new->handleDependencyUpdate($name, $vector);
Expand Down Expand Up @@ -125,13 +121,13 @@ public function toArray(): array
*/
private function getSortAsc(): array
{
$array = mapToArray($this->map);
$array = $this->map->toArray();
// @phpstan-ignore-next-line
uasort($array, function (VectorInterface $a, VectorInterface $b) {
return match (true) {
$b->contains(...vectorToArray($a)) => -1,
$b->contains(...$a->toArray()) => -1,
// @infection-ignore-all
$a->contains(...vectorToArray($b)) => 1,
$a->contains(...$b->toArray()) => 1,
default => 0
};
});
Expand Down Expand Up @@ -165,7 +161,7 @@ private function getSortJobs(array $sort, array $sync): array
$aux++;
}
/** @var array<int, array<int, string>> */
$array = vectorToArray($vector);
$array = $vector->toArray();

return array_values(
array_filter($array)
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Chevere\Workflow\Interfaces;

use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Action\Interfaces\ActionNameInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;

/**
Expand All @@ -37,7 +37,7 @@ public function withIsSync(bool $flag = true): self;

public function withDepends(string ...$jobs): self;

public function action(): ActionInterface;
public function actionName(): ActionNameInterface;

/**
* @return array<string, mixed>
Expand Down
24 changes: 12 additions & 12 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Chevere\Workflow;

use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Action\Interfaces\ActionNameInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;
use Chevere\DataStructure\Vector;
use function Chevere\Message\message;
use function Chevere\Parameter\assertArgument;
use function Chevere\Parameter\assertNamedArgument;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\String\AssertString;
use Chevere\String\StringAssert;
use Chevere\Throwable\Errors\ArgumentCountError;
use Chevere\Throwable\Exceptions\BadMethodCallException;
use Chevere\Throwable\Exceptions\OverflowException;
Expand Down Expand Up @@ -48,13 +48,13 @@ final class Job implements JobInterface
private VectorInterface $runIf;

public function __construct(
private ActionInterface $action,
private ActionNameInterface $actionName,
private bool $isSync = false,
mixed ...$argument
) {
$this->runIf = new Vector();
$this->dependencies = new Vector();
$this->parameters = $action->parameters();
$this->parameters = $actionName->__toString()::getParameters();
$this->arguments = [];
$this->setArguments(...$argument);
}
Expand Down Expand Up @@ -103,9 +103,9 @@ public function withDepends(string ...$jobs): JobInterface
return $new;
}

public function action(): ActionInterface
public function actionName(): ActionNameInterface
{
return $this->action;
return $this->actionName;
}

public function arguments(): array
Expand Down Expand Up @@ -148,7 +148,7 @@ private function setArguments(mixed ...$argument): void
throw new BadMethodCallException(
message('Missing argument(s) [%arguments%] for %action%')
->withCode('%arguments%', implode(', ', $missing))
->withCode('%action%', $this->action::class)
->withCode('%action%', $this->actionName->__toString())
);
}
$this->arguments = $values;
Expand All @@ -164,12 +164,12 @@ private function assertArgumentsCount(array $arguments): void
if ($countRequired > $countProvided
|| $countRequired !== $countProvided
) {
$parameters = implode(', ', $this->parameters->required());
$parameters = implode(', ', $this->parameters->required()->toArray());
$parameters = $parameters === '' ? '' : "[{$parameters}]";

throw new ArgumentCountError(
message('%symbol% requires %countRequired% argument(s) %parameters%')
->withCode('%symbol%', $this->action::class . '::run')
->withCode('%symbol%', $this->actionName->__toString() . '::run')
->withCode('%countRequired%', strval($countRequired))
->withCode('%parameters%', $parameters)
);
Expand All @@ -181,7 +181,7 @@ private function assertParameter(string $name, ParameterInterface $parameter, mi
if ($value instanceof ReferenceInterface || $value instanceof VariableInterface) {
return;
}
assertArgument($parameter, $name, $value);
assertNamedArgument($name, $parameter, $value);
}

private function inferDependencies(mixed $argument): void
Expand Down Expand Up @@ -220,7 +220,7 @@ private function assertDependencies(string ...$dependencies): void
);
}
foreach ($dependencies as $dependency) {
(new AssertString($dependency))
(new StringAssert($dependency))
->notEmpty()
->notCtypeDigit()
->notCtypeSpace();
Expand Down
22 changes: 10 additions & 12 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
use Chevere\DataStructure\Map;
use Chevere\DataStructure\Traits\MapTrait;
use Chevere\DataStructure\Vector;
use function Chevere\DataStructure\vectorToArray;
use function Chevere\Message\message;
use function Chevere\Parameter\booleanParameter;
use function Chevere\Parameter\boolean;
use Chevere\Parameter\Interfaces\BooleanParameterInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Throwable\Errors\TypeError;
Expand Down Expand Up @@ -139,8 +138,8 @@ private function putAdded(JobInterface ...$job): void

private function storeReferences(string $name, JobInterface $job): void
{
$action = $job->action();
foreach ($action->responseParameters() as $key => $parameter) {
$action = $job->actionName()->__toString();
foreach ($action::acceptResponse()->parameters() as $key => $parameter) {
$this->references = $this->references
->withPut(
...[
Expand All @@ -153,8 +152,8 @@ private function storeReferences(string $name, JobInterface $job): void
private function handleArguments(string $name, JobInterface $job): void
{
foreach ($job->arguments() as $argument => $value) {
$action = $job->action();
$parameter = $action->parameters()->get($argument);
$action = $job->actionName()->__toString();
$parameter = $action::getParameters()->get($argument);
$property = match (true) {
$value instanceof VariableInterface => 'variables',
$value instanceof ReferenceInterface => 'references',
Expand Down Expand Up @@ -231,9 +230,8 @@ private function handleRunIfReference(mixed $runIf): void
if (! $runIf instanceof ReferenceInterface) {
return;
}
$action = $this->get($runIf->job())->action();
$parameter = $action->getResponseParameters()
->get($runIf->parameter());
$action = $this->get($runIf->job())->actionName()->__toString();
$parameter = $action::acceptResponse()->parameters()->get($runIf->parameter());
if ($parameter->type()->primitive() !== 'boolean') {
throw new TypeError(
message('Reference %reference% must be of type boolean')
Expand All @@ -251,7 +249,7 @@ private function handleRunIfVariable(string $name, mixed $runIf): void
$this->variables = $this->variables
->withPut(
...[
$runIf->__toString() => booleanParameter(),
$runIf->__toString() => boolean(),
]
);

Expand All @@ -271,11 +269,11 @@ private function handleRunIfVariable(string $name, mixed $runIf): void

private function assertDependencies(string $job): void
{
$dependencies = vectorToArray($this->jobDependencies);
$dependencies = $this->jobDependencies->toArray();
if (! $this->jobs->contains(...$dependencies)) {
$missing = array_diff(
$dependencies,
vectorToArray($this->jobs)
$this->jobs->toArray()
);

throw new OutOfBoundsException(
Expand Down
6 changes: 3 additions & 3 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Chevere\Workflow;

use Chevere\String\AssertString;
use Chevere\String\StringAssert;
use Chevere\Workflow\Interfaces\ReferenceInterface;

final class Reference implements ReferenceInterface
Expand All @@ -26,8 +26,8 @@ public function __construct(
private string $job,
private string $parameter
) {
(new AssertString($job))->notCtypeSpace()->notEmpty();
(new AssertString($parameter))->notCtypeSpace()->notEmpty();
(new StringAssert($job))->notCtypeSpace()->notEmpty();
(new StringAssert($parameter))->notCtypeSpace()->notEmpty();
}

public function __toString(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
$this->uuid = Uuid::uuid4()->toString();
$this->arguments = new Arguments(
$workflow->parameters(),
...$variable
$variable
);
$this->map = new Map();
$this->skip = new Vector();
Expand Down Expand Up @@ -96,7 +96,7 @@ public function withResponse(string $job, ResponseInterface $response): RunInter
$new->workflow->jobs()->get($job);
$tryArguments = new Arguments(
$new->workflow->getJobResponseParameters($job),
...$response->data()
$response->data()
);
$tryArguments->parameters();
$new->map = $new->map->withPut(...[
Expand Down
5 changes: 4 additions & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class Runner implements RunnerInterface
{
public function __construct(
private RunInterface $run,
// @phpstan-ignore-next-line
private ContainerInterface $container
) {
}
Expand Down Expand Up @@ -79,8 +80,10 @@ public function withRunJob(string $name): RunnerInterface
return $new;
}
}
$action = $job->action()->withContainer($new->container);
$arguments = $new->getJobArguments($job);
$action = $job->actionName()->__toString();
/** @var ActionInterface $action */
$action = new $action();
$response = $new->getActionResponse($action, $arguments);
$new->addJobResponse($name, $response);

Expand Down
10 changes: 5 additions & 5 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Chevere\DataStructure\Map;
use function Chevere\Message\message;
use function Chevere\Parameter\booleanParameter;
use function Chevere\Parameter\boolean;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Parameter\Parameters;
Expand Down Expand Up @@ -85,11 +85,11 @@ public function getJobResponseParameters(string $job): ParametersInterface

private function putParameters(string $name, JobInterface $job): void
{
$action = $job->action();
$parameters = $action->parameters();
$action = $job->actionName()->__toString();
$parameters = $action::getParameters();
$this->provided = $this->provided->withPut(
...[
$name => $action->responseParameters(),
$name => $action::acceptResponse()->parameters(),
]
);
foreach ($job->arguments() as $argument => $value) {
Expand Down Expand Up @@ -147,7 +147,7 @@ private function putAdded(JobInterface ...$job): void

private function putJobConditions(JobInterface $job): void
{
$parameter = booleanParameter();
$parameter = boolean();
foreach ($job->runIf() as $value) {
$this->putVariableReference($value, $parameter);
}
Expand Down
19 changes: 9 additions & 10 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Chevere\Workflow;

use Chevere\Action\ActionName;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Container\Container;
use Chevere\Workflow\Interfaces\JobInterface;
Expand All @@ -39,29 +40,27 @@ function workflow(JobInterface ...$job): WorkflowInterface
/**
* Creates a synchronous Job instance for the given action and arguments.
*
* @param class-string<ActionInterface> $action
* @param mixed ...$argument Action arguments for its run method (raw, reference or variable)
*/
function sync(ActionInterface $action, mixed ...$argument): JobInterface
function sync(string $action, mixed ...$argument): JobInterface
{
$action = new ActionName($action);

return new Job($action, true, ...$argument);
}

/**
* Creates an asynchronous Job instance for the given action and arguments.
*
* @param class-string<ActionInterface> $action
* @param mixed ...$argument Action arguments for its run method (raw, reference or variable)
*/
function async(ActionInterface $action, mixed ...$argument): JobInterface
function async(string $action, mixed ...$argument): JobInterface
{
return new Job($action, false, ...$argument);
}
$action = new ActionName($action);

/**
* @deprecated Use sync() and async() instead
*/
function job(ActionInterface $action, mixed ...$argument): JobInterface
{
return async($action, ...$argument);
return new Job($action, false, ...$argument);
}

/**
Expand Down
Loading

0 comments on commit d901dda

Please sign in to comment.