Skip to content

Commit 7856fa2

Browse files
committed
Completely lazy load console commands when resolving
1 parent 932e601 commit 7856fa2

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/Illuminate/Console/Application.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class Application extends SymfonyApplication implements ApplicationContract
5454
protected static $bootstrappers = [];
5555

5656
/**
57-
* A map of command names to classes.
57+
* The lazy command loader.
5858
*
59-
* @var array
59+
* @var \Illuminate\Console\ContainerCommandLoader
6060
*/
61-
protected $commandMap = [];
61+
protected $commandLoader;
6262

6363
/**
6464
* Create a new Artisan console application.
@@ -265,8 +265,8 @@ protected function addToParent(SymfonyCommand $command)
265265
*/
266266
public function resolve($command)
267267
{
268-
if (class_exists($command) && ($commandName = $command::getDefaultName())) {
269-
$this->commandMap[$commandName] = $command;
268+
if (class_exists($command) && $this->commandLoader) {
269+
$this->commandLoader->add($command);
270270

271271
return null;
272272
}
@@ -298,7 +298,7 @@ public function resolveCommands($commands)
298298
*/
299299
public function setContainerCommandLoader()
300300
{
301-
$this->setCommandLoader(new ContainerCommandLoader($this->laravel, $this->commandMap));
301+
$this->setCommandLoader($this->commandLoader = new ContainerCommandLoader($this->laravel));
302302

303303
return $this;
304304
}

src/Illuminate/Console/ContainerCommandLoader.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,32 @@ class ContainerCommandLoader implements CommandLoaderInterface
1717
protected $container;
1818

1919
/**
20-
* A map of command names to classes.
20+
* A list of class names.
2121
*
2222
* @var array
2323
*/
24-
protected $commandMap;
24+
protected $classes;
2525

2626
/**
2727
* Create a new command loader instance.
2828
*
2929
* @param \Psr\Container\ContainerInterface $container
30-
* @param array $commandMap
3130
* @return void
3231
*/
33-
public function __construct(ContainerInterface $container, array $commandMap)
32+
public function __construct(ContainerInterface $container)
3433
{
3534
$this->container = $container;
36-
$this->commandMap = $commandMap;
35+
}
36+
37+
/**
38+
* Add class to the loader.
39+
*
40+
* @param string $name
41+
* @return void
42+
*/
43+
public function add(string $name)
44+
{
45+
$this->classes[] = $name;
3746
}
3847

3948
/**
@@ -50,7 +59,7 @@ public function get(string $name): Command
5059
throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
5160
}
5261

53-
return $this->container->get($this->commandMap[$name]);
62+
return $this->container->get($name);
5463
}
5564

5665
/**
@@ -61,7 +70,7 @@ public function get(string $name): Command
6170
*/
6271
public function has(string $name): bool
6372
{
64-
return $name && isset($this->commandMap[$name]);
73+
return in_array($name, $this->classes);
6574
}
6675

6776
/**
@@ -71,6 +80,12 @@ public function has(string $name): bool
7180
*/
7281
public function getNames(): array
7382
{
74-
return array_keys($this->commandMap);
83+
$names = [];
84+
85+
foreach ($this->classes as $class) {
86+
$names[] = $class::getDefaultName();
87+
}
88+
89+
return $names;
7590
}
7691
}

src/Illuminate/Foundation/Console/Kernel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ protected function getArtisan()
328328
{
329329
if (is_null($this->artisan)) {
330330
$this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
331-
->resolveCommands($this->commands)
332-
->setContainerCommandLoader();
331+
->setContainerCommandLoader()
332+
->resolveCommands($this->commands);
333+
333334
}
334335

335336
return $this->artisan;

0 commit comments

Comments
 (0)