-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define required values for the container (#3)
* Define required values for the container by implementing step dependencies. See README.md
- Loading branch information
Showing
9 changed files
with
604 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Exception/WorkflowStepDependencyNotFulfilledException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPWorkflow\Exception; | ||
|
||
use Exception; | ||
|
||
class WorkflowStepDependencyNotFulfilledException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPWorkflow\Middleware; | ||
|
||
use PHPWorkflow\Exception\WorkflowStepDependencyNotFulfilledException; | ||
use PHPWorkflow\State\WorkflowContainer; | ||
use PHPWorkflow\Step\Dependency\StepDependencyInterface; | ||
use PHPWorkflow\Step\WorkflowStep; | ||
use PHPWorkflow\WorkflowControl; | ||
use ReflectionAttribute; | ||
use ReflectionException; | ||
use ReflectionMethod; | ||
|
||
class WorkflowStepDependencyCheck | ||
{ | ||
/** | ||
* @throws ReflectionException | ||
* @throws WorkflowStepDependencyNotFulfilledException | ||
*/ | ||
public function __invoke( | ||
callable $next, | ||
WorkflowControl $control, | ||
WorkflowContainer $container, | ||
WorkflowStep $step, | ||
) { | ||
$containerParameter = (new ReflectionMethod($step, 'run'))->getParameters()[1] ?? null; | ||
|
||
if ($containerParameter) { | ||
foreach ($containerParameter->getAttributes( | ||
StepDependencyInterface::class, | ||
ReflectionAttribute::IS_INSTANCEOF, | ||
) as $dependencyAttribute | ||
) { | ||
/** @var StepDependencyInterface $dependency */ | ||
$dependency = $dependencyAttribute->newInstance(); | ||
$dependency->check($container); | ||
} | ||
} | ||
|
||
return $next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPWorkflow\Step\Dependency; | ||
|
||
use Attribute; | ||
use PHPWorkflow\Exception\WorkflowStepDependencyNotFulfilledException; | ||
use PHPWorkflow\State\WorkflowContainer; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
class Requires implements StepDependencyInterface | ||
{ | ||
public function __construct(private string $key, private ?string $type = null) {} | ||
|
||
public function check(WorkflowContainer $container): void | ||
{ | ||
if (!$container->has($this->key)) { | ||
throw new WorkflowStepDependencyNotFulfilledException("Missing '$this->key' in container"); | ||
} | ||
|
||
$value = $container->get($this->key); | ||
|
||
if ($this->type === null || (str_starts_with($this->type, '?') && $value === null)) { | ||
return; | ||
} | ||
|
||
$type = str_replace('?', '', $this->type); | ||
|
||
if (preg_match('/^(string|bool|int|float|object|array|iterable|scalar)$/', $type, $matches) === 1) { | ||
$checkMethod = 'is_' . $matches[1]; | ||
|
||
if ($checkMethod($value)) { | ||
return; | ||
} | ||
} elseif (class_exists($type) && ($value instanceof $type)) { | ||
return; | ||
} | ||
|
||
throw new WorkflowStepDependencyNotFulfilledException( | ||
sprintf( | ||
"Value for '%s' has an invalid type. Expected %s, got %s", | ||
$this->key, | ||
$this->type, | ||
gettype($value) . (is_object($value) ? sprintf(' (%s)', $value::class) : ''), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPWorkflow\Step\Dependency; | ||
|
||
use PHPWorkflow\Exception\WorkflowStepDependencyNotFulfilledException; | ||
use PHPWorkflow\State\WorkflowContainer; | ||
|
||
interface StepDependencyInterface | ||
{ | ||
/** | ||
* @throws WorkflowStepDependencyNotFulfilledException | ||
*/ | ||
public function check(WorkflowContainer $container): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.