-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce a command loader factory and improve laziness
- Loading branch information
Showing
16 changed files
with
247 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry\Console package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\Console\Bridge\CommandLoader; | ||
|
||
use Fidry\Console\Command\Command as FidryCommand; | ||
use Fidry\Console\Command\LazyCommandEnvelope; | ||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; | ||
|
||
interface CommandLoaderFactory | ||
{ | ||
/** | ||
* @param array<LazyCommandEnvelope|FidryCommand> $commands | ||
*/ | ||
public function createCommandLoader(array $commands): CommandLoaderInterface; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Bridge/CommandLoader/SymfonyFactoryCommandLoaderFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry\Console package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\Console\Bridge\CommandLoader; | ||
|
||
use Fidry\Console\Bridge\Command\SymfonyCommandFactory; | ||
use Fidry\Console\Command\Command as FidryCommand; | ||
use Fidry\Console\Command\LazyCommandEnvelope; | ||
use Symfony\Component\Console\Command\Command as SymfonyNativeCommand; | ||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; | ||
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader; | ||
|
||
final class SymfonyFactoryCommandLoaderFactory implements CommandLoaderFactory | ||
{ | ||
public function __construct( | ||
private readonly SymfonyCommandFactory $commandFactory, | ||
) { | ||
} | ||
|
||
public function createCommandLoader(array $commands): CommandLoaderInterface | ||
{ | ||
$factories = []; | ||
|
||
foreach ($commands as $commandOrEnvelope) { | ||
$command = $this->createCommand($commandOrEnvelope); | ||
/** @var string $name */ | ||
$name = $command->getName(); | ||
|
||
$factories[$name] = static fn (): SymfonyNativeCommand => $command; | ||
} | ||
|
||
return new FactoryCommandLoader($factories); | ||
} | ||
|
||
private function createCommand(LazyCommandEnvelope|FidryCommand $commandOrCommandFactory): SymfonyNativeCommand | ||
{ | ||
return $commandOrCommandFactory instanceof FidryCommand | ||
? $this->commandFactory->crateSymfonyCommand($commandOrCommandFactory) | ||
: $this->commandFactory->crateSymfonyLazyCommand( | ||
$commandOrCommandFactory->name, | ||
$commandOrCommandFactory->description, | ||
$commandOrCommandFactory->factory, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry\Console package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\Console\Command; | ||
|
||
// TODO: description & doc | ||
use Closure; | ||
|
||
final class LazyCommandEnvelope | ||
{ | ||
/** | ||
* @param Closure(): Command $factory | ||
*/ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string $description, | ||
public readonly Closure $factory, | ||
) { | ||
} | ||
|
||
/** | ||
* @param class-string<LazyCommand> $commandClassName | ||
* @param Closure():LazyCommand $factory | ||
*/ | ||
public static function wrap(string $commandClassName, Closure $factory): self | ||
{ | ||
return new self( | ||
$commandClassName::getName(), | ||
$commandClassName::getDescription(), | ||
$factory, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.