-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Run a sub-command easily #17
Comments
Suggested directly to Symfony: symfony/symfony#17374 |
Declined for addition in Symfony, to add in this project. |
Implemented and documented. Will be released as 1.2.0. |
At the moment the only way to pass variables from a command to another command is if the original command was given the argument in the console...
Background:I have 2 similar commands
$app->command('restart [service] [txt]', function ($service, $txt = "restarted") {
output = $txt;
} Please make this a possibility. |
@yCodeTech Try v1.8.3, could be related to #69. |
Is there a way to call a sub-command when using an object as the command callable?
When I try this I get fatal error - call to undefined method |
@amfischer As far as I know, it is not possible in PHP to change the reference of You would have to inject or pass the // Via a dependency-injection container
$app->useContainer($container);
$app->command('greet name [--yell]', 'foo-invokable-object-service-id');
// Via manual instantiation
$app->command('greet name [--yell]', new FooInvokableObject($app));
class FooInvokableObject
{
public function __construct(private Application $app)
{
}
public function __invoke(string $name, bool $yell = false)
{
$this->app->runCommand('bar');
}
} $app->command('greet name [--yell]', fn(...$args) => (new BarInvokableObject)($app, ...$args));
class BarInvokableObject
{
public function __invoke(Application $app, string $name, bool $yell = false)
{
$app->runCommand('bar');
}
} |
@mcaskill I got it to work using the inject option you demonstrated, but I was getting stuck for a moment. I'm using PHP-DI as my container and I wasn't sure how to add the
I thought about trying to define the I finally stumbled upon a set method for the container. I call it right after
Thanks for your help and the fast reply 👍 |
Glad I could help. I forgot about the chicken-and-egg nature of fetching the application from the container and assigning it to the application. I suppose this could also work: $container = require __DIR__ . '/../../bootstrap/app.php';
$app = $container->get(Application::class);
$app->useContainer($container, injectWithTypeHint: true);
$app->command('foo', FooInvokableObject); |
In Symfony Console to run a sub-command is a bit verbose:
It would be great to add a helper method like:
Example:
The text was updated successfully, but these errors were encountered: