Code Monkey home page Code Monkey logo

Comments (9)

mcaskill avatar mcaskill commented on May 9, 2024 1

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

from silly.

mcaskill avatar mcaskill commented on May 9, 2024 1

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

from silly.

amfischer avatar amfischer commented on May 9, 2024 1

@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 👍

from silly.

mcaskill avatar mcaskill commented on May 9, 2024 1

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

from silly.

mnapoli avatar mnapoli commented on May 9, 2024

Suggested directly to Symfony: symfony/symfony#17374

from silly.

mnapoli avatar mnapoli commented on May 9, 2024

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

from silly.

mnapoli avatar mnapoli commented on May 9, 2024

Implemented and documented. Will be released as 1.2.0.

from silly.

yCodeTech avatar yCodeTech commented on May 9, 2024

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.

from silly.

amfischer avatar amfischer commented on May 9, 2024

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

from silly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.