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

Run a sub-command easily #17

Closed
mnapoli opened this issue Jan 14, 2016 · 9 comments
Closed

Run a sub-command easily #17

mnapoli opened this issue Jan 14, 2016 · 9 comments
Milestone

Comments

@mnapoli
Copy link
Owner

mnapoli commented Jan 14, 2016

In Symfony Console to run a sub-command is a bit verbose:

$app->command('foo', function (OutputInterface $output) use ($app) {
    $arguments = array(
        'command' => 'demo:greet',
        'name'    => 'Fabien',
        '--yell'  => true,
    );
    $app->find('demo:greet')->run(new ArrayInput($arguments), $output);

    // ...
});

It would be great to add a helper method like:

$app->runCommand('demo:greet Fabien --yell', $output)

Example:

$app->command('init', function ($input, $output) {
    $this->runCommand('db:drop --force', $output)
    $this->runCommand('db:create', $output)
    $this->runCommand('db:fixtures --verbose', $output)
});
@mnapoli
Copy link
Owner Author

mnapoli commented Jan 14, 2016

Suggested directly to Symfony: symfony/symfony#17374

@mnapoli
Copy link
Owner Author

mnapoli commented Jan 27, 2016

Declined for addition in Symfony, to add in this project.

@mnapoli
Copy link
Owner Author

mnapoli commented Feb 1, 2016

Implemented and documented. Will be released as 1.2.0.

@mnapoli mnapoli closed this as completed Feb 1, 2016
@yCodeTech
Copy link

yCodeTech commented Jun 19, 2023

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...

$ myApp start arg something will result in calling the sub command with argument of something. However, I need to pass a hardcoded string to the sub command instead.

Background:

I have 2 similar commands start and restart. The only thing different about the command blocks is the output wording of "started" and "restarted". Now in DRY terms this is a big no-no. So I'm trying to make the start command run the restart command with either a hardcoded argument or option that specifies the word "start", then that would be passed to the restart command.

$ myApp start arg would run: $this->runCommand('restart ' . $arg. ' started');

$app->command('restart [service] [txt]', function ($service, $txt = "restarted") {
    output = $txt;
}

Please make this a possibility.

@mcaskill
Copy link
Contributor

@yCodeTech Try v1.8.3, could be related to #69.

@amfischer
Copy link

Is there a way to call a sub-command when using an object as the command callable?

console.php

$app->command('foo', FooInvokableObject::class);
$app->command('bar', BarInvokableObject::class);
FooInvokableObject.php

class FooInvokableObject
{
    public function __invoke()
    {
        $this->runCommand('bar');
    }
}

When I try this I get fatal error - call to undefined method FooInvokableObject::runCommand()

@mcaskill
Copy link
Contributor

mcaskill commented Nov 30, 2023

@amfischer As far as I know, it is not possible in PHP to change the reference of $this within a class. It is possible within an anonymous function (i.e., a Closure).

You would have to inject or pass the Application into your invokable class:

// 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');
    }
}

@amfischer
Copy link

@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 Application instance to the container since I define it after the container is built.

$container = require __DIR__ . '/../../bootstrap/app.php';

$app = new Silly\Application();

$app->useContainer($container, $injectWithTypeHint = true);

$app->command('foo', FooInvokableObject)

I thought about trying to define the Application in the container definition file, but I couldn't make sense of that. I also thought maybe autowiring would take care of everything, but when I only type-hint Application on the InvokableObject without first defining it in the container it resolves a new instance without any of my previous commands defined.

I finally stumbled upon a set method for the container. I call it right after $app->useContainer() and it works.

// register $app in container so we can run sub-commands
$app->getContainer()->set(Application::class, $app);

Thanks for your help and the fast reply 👍

@mcaskill
Copy link
Contributor

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);

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

No branches or pull requests

4 participants