-
Notifications
You must be signed in to change notification settings - Fork 0
feat(poc,spiral-emit): Add web and cli emitting of YAML OP instruction #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
poc/spiral-emit/app/src/Modules/OPSpiral/Endpoints/Console/OPInstructionEmitCommand.php
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Modules\OPSpiral\Endpoints\Console; | ||
|
|
||
| use App\Modules\OPSpiral\Infrastructure\InstructionEmitter; | ||
| use Spiral\Console\Attribute\AsCommand; | ||
| use Spiral\Console\Command; | ||
|
|
||
| #[AsCommand(name: 'op:instruction:emit', description: 'Emit OP instruction')] | ||
| final class OPInstructionEmitCommand extends Command | ||
| { | ||
| public function __invoke(InstructionEmitter $emitter): int | ||
| { | ||
| $this->info(json_encode($emitter->emit(), JSON_THROW_ON_ERROR)); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
poc/spiral-emit/app/src/Modules/OPSpiral/Endpoints/Web/OPInstructionEmitRoute.php
This file contains hidden or 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,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Modules\OPSpiral\Endpoints\Web; | ||
|
|
||
| use App\Modules\OPSpiral\Infrastructure\InstructionEmitter; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Spiral\Http\ResponseWrapper; | ||
| use Spiral\Router\Annotation\Route; | ||
|
|
||
| class OPInstructionEmitRoute | ||
| { | ||
| #[Route('/op/instruction', name: 'op.instruction.emit', methods: ['GET'])] | ||
| public function __invoke(InstructionEmitter $emitter, ResponseWrapper $response): ResponseInterface | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| { | ||
| $instruction = $emitter->emit(); | ||
|
|
||
| $r = $response->create(200)->withHeader('Content-Type', 'text/yaml'); | ||
|
|
||
| $r->getBody()->write($instruction->toYaml()); | ||
|
|
||
| return $r; | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
poc/spiral-emit/app/src/Modules/OPSpiral/Infrastructure/InstructionEmitter.php
This file contains hidden or 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,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Modules\OPSpiral\Infrastructure; | ||
|
|
||
| use App\Modules\OPSpiral\Infrastructure\Web\WebOperationsEmitter; | ||
| use Thumbrise\OP\Universal\Schema\Term; | ||
|
|
||
| class InstructionEmitter | ||
| { | ||
| public function __construct( | ||
| private readonly WebOperationsEmitter $webOperationsEmitter, | ||
| ) {} | ||
|
|
||
| public function emit(): SerializableInstruction | ||
| { | ||
| $web = $this->webOperationsEmitter->emit(); | ||
|
|
||
| $operations = array_merge( | ||
| $web, | ||
| // TODO cli, | ||
| // TODO anything, | ||
| ); | ||
|
|
||
| return new SerializableInstruction( | ||
| id: 'Dog shop', | ||
| comment: 'Dowg', | ||
| version: '1.0.0', | ||
| operations: $operations, | ||
| trait: [ | ||
| new Term('my/opinion', 'my honest opinion'), | ||
| ], | ||
| ); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...mit/app/src/Modules/OPSpiral/Infrastructure/Internal/Reflection/DocBlockSummaryReader.php
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Modules\OPSpiral\Infrastructure\Internal\Reflection; | ||
|
|
||
| use phpDocumentor\Reflection\DocBlockFactory; | ||
| use ReflectionMethod; | ||
|
|
||
| class DocBlockSummaryReader | ||
| { | ||
| /** | ||
| * @todo Wrong docblock for method WITHOUT docblock, if above exist use trait WITH docblock | ||
| */ | ||
| public function read(ReflectionMethod $method): string | ||
| { | ||
| $v = $method->getDocComment(); | ||
| if (empty($v)) { | ||
| return ''; | ||
| } | ||
|
|
||
| $factory = DocBlockFactory::createInstance(); | ||
| $docBlock = $factory->create($v); | ||
|
|
||
| return $docBlock->getSummary(); | ||
|
thumbrise marked this conversation as resolved.
|
||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
...piral-emit/app/src/Modules/OPSpiral/Infrastructure/Internal/Reflection/PropertyReader.php
This file contains hidden or 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 App\Modules\OPSpiral\Infrastructure\Internal\Reflection; | ||
| use ReflectionProperty; | ||
|
|
||
| class PropertyReader | ||
| { | ||
| public function read(object $object, string $property): mixed | ||
| { | ||
| $ref = new ReflectionProperty($object, $property); | ||
|
|
||
| return $ref->getValue($object); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
poc/spiral-emit/app/src/Modules/OPSpiral/Infrastructure/SerializableInstruction.php
This file contains hidden or 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,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Modules\OPSpiral\Infrastructure; | ||
|
|
||
| use JsonSerializable; | ||
| use Symfony\Component\Yaml\Yaml; | ||
| use Thumbrise\OP\Universal\Schema\Instruction; | ||
|
|
||
| class SerializableInstruction extends Instruction implements JsonSerializable | ||
| { | ||
| public function __toString(): string | ||
| { | ||
| return $this->toYaml(); | ||
| } | ||
|
|
||
| public function toYaml(): string | ||
| { | ||
| return Yaml::dump($this->toAssoc(), flags: Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_OBJECT_AS_MAP); | ||
| } | ||
|
thumbrise marked this conversation as resolved.
|
||
|
|
||
| public function jsonSerialize(): array | ||
| { | ||
| return $this->toAssoc(); | ||
| } | ||
|
|
||
| private function toAssoc(): array | ||
| { | ||
| $vars = get_object_vars($this); | ||
|
|
||
| $json = json_encode($vars); | ||
|
|
||
| $assoc = json_decode($json, true); | ||
|
|
||
| return $this->recursiveNullFilter($assoc); | ||
|
thumbrise marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private function recursiveNullFilter(array $data): array | ||
| { | ||
| $filtered = []; | ||
| foreach ($data as $key => $value) { | ||
| if (is_array($value)) { | ||
| $value = $this->recursiveNullFilter($value); | ||
| } | ||
| if (null !== $value) { | ||
| $filtered[$key] = $value; | ||
| } | ||
| } | ||
|
|
||
| return $filtered; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.