Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from vladgorenkin/lazy-loaded-commands
Implement lazy-loading of cli commands
- Loading branch information
Showing
12 changed files
with
174 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/** | ||
* Spiral Framework. | ||
* | ||
* @license MIT | ||
* @author Vladislav Gorenkin (vladgorenkin) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Console\Traits; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Spiral\Console\Command as SpiralCommand; | ||
use Symfony\Component\Console\Command\Command as SymfonyCommand; | ||
use Symfony\Component\Console\Command\LazyCommand; | ||
|
||
trait LazyTrait | ||
{ | ||
/** @var ContainerInterface */ | ||
private $container; | ||
|
||
/** | ||
* Check if command can be lazy-loaded. | ||
* | ||
* @param class-string $class | ||
* @return bool | ||
*/ | ||
private function supportsLazyLoading(string $class): bool | ||
{ | ||
return is_subclass_of($class, SpiralCommand::class) | ||
&& \defined(sprintf('%s::%s', $class, 'NAME')); | ||
} | ||
|
||
/** | ||
* Wrap given command into LazyCommand which will be executed only on run. | ||
* | ||
* @param class-string $class | ||
* @return LazyCommand | ||
*/ | ||
private function createLazyCommand(string $class): LazyCommand | ||
{ | ||
return new LazyCommand( | ||
$class::NAME, | ||
[], | ||
\defined(sprintf('%s::%s', $class, 'DESCRIPTION')) | ||
? $class::DESCRIPTION | ||
: '', | ||
false, | ||
function () use ($class): SymfonyCommand { | ||
$command = $this->container->get($class); | ||
$command->setContainer($this->container); | ||
|
||
return $command; | ||
} | ||
); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
/** | ||
* Spiral Framework, SpiralScout LLC. | ||
* | ||
* @author Vladislav Gorenkin (vladgorenkin) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Console\Fixtures; | ||
|
||
use Exception; | ||
use Spiral\Console\Command; | ||
|
||
class LazyLoadedCommand extends Command | ||
{ | ||
public const NAME = 'lazy'; | ||
public const DESCRIPTION = 'Lazy description'; | ||
|
||
public function perform(): int | ||
{ | ||
$this->write('OK'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
/** | ||
* Spiral Framework, SpiralScout LLC. | ||
* | ||
* @author Vladislav Gorenkin (vladgorenkin) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Console; | ||
|
||
use Spiral\Console\CommandLocator; | ||
use Spiral\Console\StaticLocator; | ||
use Spiral\Tests\Console\Fixtures\LazyLoadedCommand; | ||
use Spiral\Tokenizer\ClassesInterface; | ||
use Symfony\Component\Console\Command\LazyCommand; | ||
|
||
class LazyTest extends BaseTest | ||
{ | ||
public function testLazyCommandCreationInCommandLocator(): void | ||
{ | ||
$locator = new CommandLocator( | ||
new class() implements ClassesInterface { | ||
public function getClasses($target = null): array | ||
{ | ||
return [ | ||
new \ReflectionClass(LazyLoadedCommand::class), | ||
]; | ||
} | ||
}, | ||
$this->container | ||
); | ||
$commands = $locator->locateCommands(); | ||
$command = reset($commands); | ||
|
||
$this->assertInstanceOf(LazyCommand::class, $command); | ||
$this->assertSame('lazy', $command->getName()); | ||
$this->assertSame('Lazy description', $command->getDescription()); | ||
} | ||
|
||
public function testLazyCommandCreationInStaticLocator(): void | ||
{ | ||
$locator = new StaticLocator([LazyLoadedCommand::class]); | ||
$commands = $locator->locateCommands(); | ||
$command = reset($commands); | ||
|
||
$this->assertInstanceOf(LazyCommand::class, $command); | ||
$this->assertSame('lazy', $command->getName()); | ||
$this->assertSame('Lazy description', $command->getDescription()); | ||
} | ||
|
||
public function testLazyCommandExecution(): void | ||
{ | ||
$core = $this->getCore(new StaticLocator([LazyLoadedCommand::class])); | ||
$output = $core->run('lazy'); | ||
$this->assertSame('OK', $output->getOutput()->fetch()); | ||
} | ||
} |
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