Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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