-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
116 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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; | ||
|
||
/** | ||
* Create a new command loader instance. | ||
* | ||
* @param \Psr\Container\ContainerInterface $container | ||
* @param array $commandMap | ||
* @return void | ||
*/ | ||
public function __construct(ContainerInterface $container, array $commandMap) | ||
{ | ||
$this->container = $container; | ||
$this->commandMap = $commandMap; | ||
} | ||
|
||
/** | ||
* Resolve a command from the container. | ||
* | ||
* @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]); | ||
} | ||
|
||
/** | ||
* Determines 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); | ||
} | ||
} |
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