Comments (9)
@yCodeTech Try v1.8.3, could be related to #69.
from silly.
@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.
@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.
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.
Suggested directly to Symfony: symfony/symfony#17374
from silly.
Declined for addition in Symfony, to add in this project.
from silly.
Implemented and documented. Will be released as 1.2.0.
from silly.
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.
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)
- Default values for options HOT 4
- Alias support HOT 3
- PHP Deprecated: Non-static method HOT 6
- Single Command Applications HOT 4
- PSR-11? HOT 2
- Default parameters for hyphen variables HOT 4
- Question: injecting Symfony\Component\Console\Style\SymfonyStyle ? HOT 2
- Conflicting php-di/php-di and php-di/invoker versions HOT 3
- single command applications with arguments HOT 4
- Get helper from classes autowired by PHP-DI HOT 1
- Allow to disable `bindTo`. HOT 1
- Cannot set a default value when using InputOption::VALUE_NONE mode HOT 5
- Possible regression: loss of default option values via reflection HOT 2
- Still alive? HOT 1
- Update symfony/console dependency HOT 1
- mnapoli/silly issues with Laravel HOT 2
- Expressions written with HEREDOC/NOWDOC format oddly
- Feature: Specifying a value of an option as a space/comma delimited array HOT 2
- Using ArrayInput instead of StringInput breaks sub-command arguments
- Missing Symfony 7 support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from silly.