Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Sep 22, 2023
1 parent aa119b1 commit b85b15a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
13 changes: 13 additions & 0 deletions doc/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace App\Console;

use App\Console\Command\CreateUserCommand;
use Fidry\Console\Application\Application as FidryApplication;
use Fidry\Console\Command\LazyCommandEnvelope;
use function sprintf;

final class Application implements FidryApplication
Expand Down Expand Up @@ -51,7 +52,19 @@ final class Application implements FidryApplication
public function getCommands() : array
{
return [
// Regular command
new CreateUserCommand(),
// Declare a lazy command
new LazyCommandEnvelope(
'app:wrapped-lazy',
'Wrapped lazy command description.',
static fn () => new CreateUserCommand(/*...*/),
),
// Register a FidryLazyCommand as lazy
LazyCommandEnvelope::wrap(
CreateUserCommand::class,
static fn () => new CreateUserCommand(/*...*/),
),
];
}

Expand Down
48 changes: 44 additions & 4 deletions doc/lazy-command.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
## How to Make Commands Lazily Loaded

To make your command lazily loaded, you need to implement the
`Fidry\Console\Command\LazyCommand` interface:
There is two ways to make a command lazily loaded.

The first way is, if your command is a standard command, to
make it lazy by registering it like as follows to your application:

```php
<?php declare(strict_types=1);

// src/Console/Application.php
namespace App\Console;

use App\Console\Command\CreateUserCommand;
use Fidry\Console\Application\Application as FidryApplication;
use Fidry\Console\Command\LazyCommandEnvelope;
use function sprintf;

final class Application implements FidryApplication
{
// ...

public function getCommands() : array
{
return [
// Declare a lazy command
new LazyCommandEnvelope(
'app:wrapped-lazy',
'Wrapped lazy command description.',
static fn () => new CreateUserCommand(/*...*/),
),
// Alternatively, you can wrap it directly if
// it implements the FidryLazyCommand interface.
LazyCommandEnvelope::wrap(
CreateUserCommand::class,
static fn () => new CreateUserCommand(/*...*/),
),
];
}
}
```

The other way is to implement the `Fidry\Console\Command\LazyCommand` interface:

```php
<?php declare(strict_types=1);
Expand Down Expand Up @@ -30,11 +69,12 @@ final class CreateUserCommand implements LazyCommand
```

Due to the design of `Fidry\Console\Command\LazyCommand`, you can have one and
only one lazy command instance per class. If you wish to circumvent this, you
only one lazy command instance per class. If you wish to get around this limitation, you
can still implement a lazy command the "regular" way:

- Implement `Fidry\Console\Command\Command` instead of `Fidry\Console\Command\LazyCommand`
- Adjust the service registration as follows:
- Make use of the `Fidry\Console\Command\LazyCommandEnvelope` (see at the beginning of this page).
- Alternatively, you can also adjust the service registration as follows if you are in a Symfony application:

```yaml
services:
Expand Down
6 changes: 5 additions & 1 deletion src/Command/LazyCommandEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

namespace Fidry\Console\Command;

// TODO: description & doc
use Closure;

/**
* Envelope to make a command lazy. Unlike Symfony, it requires the command
* description to be provided as well to prevent the command to be instantiated
* by the `list` command.
*/
final class LazyCommandEnvelope
{
/**
Expand Down

0 comments on commit b85b15a

Please sign in to comment.