8000 [9.x] Add support for lazy loading commands by paras-malhotra · Pull Request #34873 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Add support for lazy loading commands #34873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[9.x] Add support for lazy loading commands
  • Loading branch information
paras-malhotra committed Oct 17, 2020
commit 0aca5a9bb2f8fc9ee3d22ca854bc84d3f9524036
27 changes: 26 additions & 1 deletion src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Application extends SymfonyApplication implements ApplicationContract
*/
protected static $bootstrappers = [];

/**
* A map of command names to classes.
*
* @var array
*/
protected $commandMap = [];

/**
* The Event Dispatcher.
*
Expand Down Expand Up @@ -254,10 +261,16 @@ protected function addToParent(SymfonyCommand $command)
* Add a command, resolving through the application.
*
* @param string $command
* @return \Symfony\Component\Console\Command\Command
* @return \Symfony\Component\Console\Command\Command|null
*/
public function resolve($command)
{
if (class_exists($command) && ($commandName = $command::getDefaultName())) {
$this->commandMap[$commandName] = $command;

return null;
}

return $this->add($this->laravel->make($command));
}

Expand All @@ -278,6 +291,18 @@ public function resolveCommands($commands)
return $this;
}

/**
* Set the Container Command Loader
*
* @return $this
*/
public function setContainerCommandLoader()
{
$this->setCommandLoader(new ContainerCommandLoader($this->laravel, $this->commandMap));

return $this;
}

/**
* Get the default input definition for the application.
*
Expand Down
72 changes: 72 additions & 0 deletions src/Illuminate/Console/ContainerCommandLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Illuminate\Console;

use Psr\Container\ContainerInterface;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Exception\CommandNotFoundException;

class ContainerCommandLoader implements CommandLoaderInterface
{
/**
* The container instance.
*
* @var \Psr\Container\ContainerInterface
*/
protected $container;

/**
* A map of command names to classes.
*
* @var array
*/
protected $commandMap;

/**
* @param \Psr\Container\ContainerInterface $container
* @param array $commandMap
*/
public function __construct(ContainerInterface $container, array $commandMap)
{
$this->container = $container;
$this->commandMap = $commandMap;
}

/**
* Loads a command.
*
* @param string $name
* @return \Symfony\Component\Console\Command\Command
*
* @throws \Symfony\Component\Console\Exception\CommandNotFoundException
*/
public function get(string $name)
{
if (!$this->has($name)) {
throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
}

return $this->container->get($this->commandMap[$name]);
}

/**
* Checks if a command exists.
*
* @param string $name
* @return bool
*/
public function has(string $name)
{
return $name && isset($this->commandMap[$name]);
}

/**
* Get the command names.
*
* @return string[]
*/
public function getNames()
{
return array_keys($this->commandMap);
}
}
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ public function bootstrap()
protected function getArtisan()
{
if (is_null($this->artisan)) {
return $this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
->resolveCommands($this->commands);
$this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
->resolveCommands($this->commands);

return $this->artisan->setContainerCommandLoader();
}

return $this->artisan;
Expand Down
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/console.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class {{ class }} extends Command
*/
protected $signature = '{{ command }}';

/**
* The default command name for lazy loading.
*
* @var string|null
*/
protected static $defaultName = '{{ command }}';

/**
* The console command description.
*
Expand Down
0