Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Console/src/Attribute/AsInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Spiral\Console\Attribute;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class AsInput
{
}
40 changes: 38 additions & 2 deletions src/Console/src/Configurator/Attribute/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Spiral\Attributes\ReaderInterface;
use Spiral\Console\Attribute\Argument;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\AsInput;
use Spiral\Console\Attribute\Option;
use Spiral\Console\Command;
use Spiral\Console\Configurator\CommandDefinition;
Expand Down Expand Up @@ -41,10 +42,12 @@ public function parse(\ReflectionClass $reflection): CommandDefinition
$attribute = $reflection->getAttributes(SymfonyAsCommand::class)[0]->newInstance();
}

$parseSourceReflection = $this->getParseSource($reflection);

return new CommandDefinition(
name: $attribute->name,
arguments: $this->parseArguments($reflection),
options: $this->parseOptions($reflection),
arguments: $this->parseArguments($parseSourceReflection),
options: $this->parseOptions($parseSourceReflection),
description: $attribute->description,
help: $attribute instanceof AsCommand ? $attribute->help : null
);
Expand Down Expand Up @@ -84,6 +87,39 @@ public function fillProperties(Command $command, InputInterface $input): void
}
}

/**
* Get the method that should be used to parse the command.
*
* This either can be the command itself, of the `#[AsInput]` parameter of the `perform` or `__invoke` method.
*/
private function getParseSource(\ReflectionClass $reflection): \ReflectionClass
{
$method = $reflection->hasMethod('perform')
? $reflection->getMethod('perform')
: ($reflection->hasMethod('__invoke')
? $reflection->getMethod('__invoke')
: null);

if ($method === null) {
return $reflection;
}

$parameter = null;
foreach ($method->getParameters() as $param) {
$attribute = $this->reader->firstParameterMetadata($param, AsInput::class);
if ($attribute !== null) {
$parameter = $param;
break;
}
}

if ($parameter === null) {
return $reflection;
}

return new \ReflectionClass($parameter->getType()->getName());
}

private function parseArguments(\ReflectionClass $reflection): array
{
$result = [];
Expand Down
35 changes: 35 additions & 0 deletions src/Console/tests/Configurator/Attribute/InputSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Spiral\Tests\Console\Configurator\Attribute;

use PHPUnit\Framework\TestCase;
use Spiral\Attributes\Factory;
use Spiral\Console\Configurator\Attribute\Parser;
use Spiral\Tests\Console\Fixtures\Attribute\WithInvokeInputParameterCommand;
use Spiral\Tests\Console\Fixtures\Attribute\WithPerformInputParameterCommand;

class InputSourceTest extends TestCase
{
private Parser $parser;

protected function setUp(): void
{
$this->parser = new Parser((new Factory())->create());
}

public function testWithInvokeMethod(): void
{
$result = $this->parser->parse(new \ReflectionClass(WithInvokeInputParameterCommand::class));

self::assertNotEmpty($result->arguments);
self::assertNotEmpty($result->options);
}

public function testWithPerformMethod(): void
{
$result = $this->parser->parse(new \ReflectionClass(WithPerformInputParameterCommand::class));

self::assertNotEmpty($result->arguments);
self::assertNotEmpty($result->options);
}
}
15 changes: 15 additions & 0 deletions src/Console/tests/Fixtures/Attribute/Input/InputSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spiral\Tests\Console\Fixtures\Attribute\Input;

use Spiral\Console\Attribute\Argument;
use Spiral\Console\Attribute\Option;

class InputSource
{
#[Argument]
private string $arg;

#[Option]
private int $opt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spiral\Tests\Console\Fixtures\Attribute;

use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\AsInput;
use Spiral\Console\Command;
use Spiral\Tests\Console\Fixtures\Attribute\Input\InputSource;

#[AsCommand(name: 'attribute-with-description', description: 'Some description text')]
final class WithInvokeInputParameterCommand extends Command
{
public function __invoke(#[AsInput] InputSource $input): int
{
$this->write($this->getDescription());

return self::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spiral\Tests\Console\Fixtures\Attribute;

use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\AsInput;
use Spiral\Console\Command;
use Spiral\Tests\Console\Fixtures\Attribute\Input\InputSource;

#[AsCommand(name: 'attribute-with-description', description: 'Some description text')]
final class WithPerformInputParameterCommand extends Command
{
public function perform(#[AsInput] InputSource $input): int
{
$this->write($this->getDescription());

return self::SUCCESS;
}
}