Skip to content
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

Add support for getting raw parameters without validation #322

Merged
merged 1 commit into from Mar 11, 2024

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Mar 11, 2024

Example :

#[AsTask(ignoreValidationErrors: true)]
function ls(): void
{
    $params = input()->getRawParameters();
    // remove command name
    array_shift($params);
    run(['ls', ...$params], tty: true);
}

image

@pyrech
Copy link
Member

pyrech commented Mar 11, 2024

Do you have a usecase in mind?

@lyrixx
Copy link
Member Author

lyrixx commented Mar 11, 2024

See symfony/symfony#54238

Yes! @tlenclos needs that. (I asked a ticket but... no shame 😂)

Anyway, this is very useful for such cases:

castor phpunit
castor phpunit --filter testFoobar
castor phpunit -v -d

same for MANY commands actually !

@lyrixx
Copy link
Member Author

lyrixx commented Mar 11, 2024

  • RFR
  • I updated the description with a real word usecase

@lyrixx lyrixx requested a review from pyrech March 11, 2024 15:04
@lyrixx
Copy link
Member Author

lyrixx commented Mar 11, 2024

Should I ship that?

#[AsTask(ignoreValidationErrors: true)]
function ls(#[AsRawTokens] array $extraParam): void
{
    run(['ls', ...$extraParam], tty: true);
}
Implementation

diff --git a/src/Attribute/AsRawTokens.php b/src/Attribute/AsRawTokens.php
new file mode 100644
index 0000000..3071806
--- /dev/null
+++ b/src/Attribute/AsRawTokens.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Castor\Attribute;
+
+#[\Attribute(\Attribute::TARGET_PARAMETER)]
+class AsRawTokens
+{
+}
diff --git a/src/Console/Command/TaskCommand.php b/src/Console/Command/TaskCommand.php
index adefbb2..a6ccf52 100644
--- a/src/Console/Command/TaskCommand.php
+++ b/src/Console/Command/TaskCommand.php
@@ -5,8 +5,10 @@ namespace Castor\Console\Command;
 use Castor\Attribute\AsArgument;
 use Castor\Attribute\AsCommandArgument;
 use Castor\Attribute\AsOption;
+use Castor\Attribute\AsRawTokens;
 use Castor\Attribute\AsTask;
 use Castor\Console\Application;
+use Castor\Console\Input\Input;
 use Castor\Event\AfterExecuteTaskEvent;
 use Castor\Event\BeforeExecuteTaskEvent;
 use Castor\EventDispatcher;
@@ -81,6 +83,10 @@ class TaskCommand extends Command implements SignalableCommandInterface
         }
 
         foreach ($this->function->getParameters() as $parameter) {
+            if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
+                continue;
+            }
+
             $taskArgumentAttribute = $parameter->getAttributes(AsCommandArgument::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null;
 
             if ($taskArgumentAttribute) {
@@ -144,11 +150,23 @@ class TaskCommand extends Command implements SignalableCommandInterface
         }
     }
 
+    /**
+     * @param Input $input
+     */
     protected function execute(InputInterface $input, OutputInterface $output): int
     {
         $args = [];
 
         foreach ($this->function->getParameters() as $parameter) {
+            if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
+                $parameters = $input->getRawParameters();
+                // Remove command name
+                array_shift($parameters);
+                $args[] = $parameters;
+
+                continue;
+            }
+
             $name = $this->getParameterName($parameter);
             if ($input->hasArgument($name)) {
                 $args[] = $input->getArgument($name);

@lyrixx
Copy link
Member Author

lyrixx commented Mar 11, 2024

Shipped

Copy link
Member

@pyrech pyrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the usecase, thanks.

Could you add some documentation about it?

@lyrixx
Copy link
Member Author

lyrixx commented Mar 11, 2024

Oups, totally forgot the doc. Sorry! Is it better?

@joelwurtz
Copy link
Member

Really nice thanks you @lyrixx

@joelwurtz joelwurtz merged commit 6ee0ebc into main Mar 11, 2024
9 checks passed
@joelwurtz joelwurtz deleted the argument-passthru branch March 11, 2024 16:03
@tlenclos
Copy link
Member

Thanks ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants