Skip to content

Commit 1e61d2e

Browse files
committed
minor #18664 [Console] Add more examples for lazy commands (theofidry)
This PR was submitted for the 6.4 branch but it was squashed and merged into the 6.3 branch instead. Discussion ---------- [Console] Add more examples for lazy commands I think the current doc is a bit confusing regarding the laziness of commands: - It does not mention `LazyCommand` which is quite important to make a command lazy in a way it still works with the `list` command which is used a lot. - The `list` doc regarding instantiating the commands is a bit too absolute. Technically it is not wrong but there is also more subtleties to it. Indeed what it will really do is instantiate the command and then try to get the name and description. Which also means if the underlying command/factory can still not be instantiated (either by tweaking `Command` or using `LazyCommand`). This is unlike another command like the auto-complete or help which will try to get the input definition in which case even a `LazyCommand` will call its command factory. I am not sure which branch it should target however and I am sure there is a lot to say about the phrasing used. Commits ------- 5f063a2 [Console] Add more examples for lazy commands
2 parents a9e847b + 5f063a2 commit 1e61d2e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

console/commands_as_services.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ only when the ``app:sunshine`` command is actually called.
133133
.. caution::
134134

135135
Calling the ``list`` command will instantiate all commands, including lazy commands.
136+
However, if the command is a ``Symfony\Component\Console\Command\LazyCommand``, then
137+
the underlying command factory will not be executed.

console/lazy_commands.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ The traditional way of adding commands to your application is to use
1010
:method:`Symfony\\Component\\Console\\Application::add`, which expects a
1111
``Command`` instance as an argument.
1212

13+
This approach can have downsides as some commands might be expensive to
14+
instantiate in which case you may want to lazy-load them. Note however that lazy-loading
15+
is not absolute. Indeed a few commands such as `list`, `help` or `_complete` can
16+
require to instantiate other commands although they are lazy. For example `list` needs
17+
to get the name and description of all commands, which might require the command to be
18+
instantiated to get.
19+
1320
In order to lazy-load commands, you need to register an intermediate loader
1421
which will be responsible for returning ``Command`` instances::
1522

@@ -19,7 +26,9 @@ which will be responsible for returning ``Command`` instances::
1926
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
2027

2128
$commandLoader = new FactoryCommandLoader([
22-
'app:heavy' => function (): Command { return new HeavyCommand(); },
29+
// Note that the `list` command will still instantiate that command
30+
// in this example.
31+
'app:heavy' => static fn(): Command => new HeavyCommand(),
2332
]);
2433

2534
$application = new Application();
@@ -36,6 +45,28 @@ method accepts any
3645
:class:`Symfony\\Component\\Console\\CommandLoader\\CommandLoaderInterface`
3746
instance so you can use your own implementation.
3847

48+
Another way to do so is to take advantage of ``Symfony\Component\Console\Command\LazyCommand``::
49+
50+
use App\Command\HeavyCommand;
51+
use Symfony\Component\Console\Application;
52+
use Symfony\Component\Console\Command\Command;
53+
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
54+
55+
// In this case although the command is instantiated, the underlying command factory
56+
// will not be executed unless the command is actually executed or one tries to access
57+
// to its input definition to know its argument or option inputs.
58+
$lazyCommand = new LazyCommand(
59+
'app:heavy',
60+
[],
61+
'This is another more complete form of lazy command.',
62+
false,
63+
static fn (): Command => new HeavyCommand(),
64+
);
65+
66+
$application = new Application();
67+
$application->add($lazyCommand);
68+
$application->run();
69+
3970
Built-in Command Loaders
4071
------------------------
4172

0 commit comments

Comments
 (0)