8000 [Console] Add command resolver (proof of concept) by funivan · Pull Request #16438 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Console] Add command resolver (proof of concept) #16438

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

Closed
wants to merge 12 commits into from
Closed
52 changes: 26 additions & 26 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Console;

use Symfony\Component\Console\CommandsResolver\CommandResolver;
use Symfony\Component\Console\CommandsResolver\CommandResolverInterface;
use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Exception\ExceptionInterface;
Expand Down Expand Up @@ -60,7 +62,10 @@
*/
class Application
{
private $commands = array();
/**
* @var CommandResolverInterface
*/
private $commandResolver;
private $wantHelps = false;
private $runningCommand;
private $name;
Expand All @@ -76,17 +81,20 @@ class Application
/**
* Constructor.
*
* @param string $name The name of the application
* @param string $version The version of the application
* @param string $name The name of the application
* @param string $version The version of the application
* @param CommandResolverInterface $commandResolver
*/
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', CommandResolverInterface $commandResolver = null)
{
$this->name = $name;
$this->version = $version;
$this->defaultCommand = 'list';
$this->helperSet = $this->getDefaultHelperSet();
$this->definition = $this->getDefaultInputDefinition();

$this->commandResolver = $commandResolver !== null ? $commandResolver : new CommandResolver();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use $this->commandResolver = $commandResolver ?: new CommandResolver()


foreach ($this->getDefaultCommands() as $command) {
$this->add($command);
}
Expand Down Expand Up @@ -366,11 +374,7 @@ public function add(Command $command)
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
}

$this->commands[$command->getName()] = $command;

foreach ($command->getAliases() as $alias) {
$this->commands[$alias] = $command;
}
$this->commandResolver->add($command);

return $command;
}
Expand All @@ -386,12 +390,12 @@ public function add(Command $command)
*/
public function get($name)
{
if (!isset($this->commands[$name])) {
$command = $this->commandResolver->get($name);

if (!$command) {
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
}

$command = $this->commands[$name];

if ($this->wantHelps) {
$this->wantHelps = false;

Expand All @@ -413,7 +417,7 @@ public function get($name)
*/
public function has($name)
{
return isset($this->commands[$name]);
return $this->commandResolver->has($name);
}

/**
Expand All @@ -426,12 +430,8 @@ public function has($name)
public function getNamespaces()
{
$namespaces = array();
foreach ($this->commands as $command) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));

foreach ($command->getAliases() as $alias) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
}
foreach ($this->commandResolver->getAllNames() as $command) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command));
}

return array_values(array_unique(array_filter($namespaces)));
Expand Down Expand Up @@ -490,7 +490,7 @@ public function findNamespace($namespace)
*/
public function find($name)
{
$allCommands = array_keys($this->commands);
$allCommands = $this->commandResolver->getAllNames();
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$commands = preg_grep('{^'.$expr.'}', $allCommands);

Expand All @@ -516,9 +516,9 @@ public function find($name)

// filter out aliases for commands which are already on the list
if (count($commands) > 1) {
$commandList = $this->commands;
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
$commandName = $commandList[$nameOrAlias]->getName();
$resolver = $this->commandResolver;
$commands = array_filter($commands, function ($nameOrAlias) use ($resolver, $commands) {
$commandName = $resolver->get($nameOrAlias)->getName();

return $commandName === $nameOrAlias || !in_array($commandName, $commands);
});
Expand Down Expand Up @@ -546,13 +546,13 @@ public function find($name)
public function all($namespace = null)
{
if (null === $namespace) {
return $this->commands;
return $this->commandResolver->getAll();
}

$commands = array();
foreach ($this->commands as $name => $command) {
foreach ($this->commandResolver->getAllNames() as $name) {
if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
$commands[$name] = $command;
$commands[$name] = $this->commandResolver->get($name);
}
}

Expand Down
67 changes: 67 additions & 0 deletions src/Symfony/Component/Console/CommandsResolver/CommandResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\CommandsResolver;

use Symfony\Component\Console\Command\Command;

/**
* @author Ivan Shcherbak <dev@funivan.com>
*/
class CommandResolver implements CommandResolverInterface
{
private $commands = array();

/**
* {@inheritdoc}
*/
public function add(Command $command)
{
$this->commands[$command->getName()] = $command;

foreach ($command->getAliases() as $alias) {
$this->commands[$alias] = $command;
}

}

/**
* {@inheritdoc}
*/
public function has($name)
{
return isset($this->commands[$name]);
}

/**
* {@inheritdoc}
*/
public function get($name)
{
return isset($this->commands[$name]) ? $this->commands[$name] : null;
}

/**
* {@inheritdoc}
*/
public function getAll()
{
return $this->commands;
}

/**
* {@inheritdoc}
*/
public function getAllNames()
{
return array_keys($this->commands);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\CommandsResolver;

use Symfony\Component\Console\Command\Command;

/**
* @author Ivan Shcherbak <dev@funivan.com>
*/
interface CommandResolverInterface
{
/**
* Store initialized commands.
* Important! Commands with the same name will be overridden.
*
* @param Command $command
*/
public function add(Command $command);

/**
* Check if command exist.
*
* @param string $name
*
* @return bool
*/
public function has($name);

/**
* Get command by name or alias.
*
* @param string $name
*
* @return Command|null
*/
public function get($name);

/**
* Return all available commands related to this application.
*
* @return Command[]
*/
public function getAll();

/**
* Return all command names and aliases.
*
* @return array
*/
public function getAllNames();
}
65E0
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Test\CommandsResolver;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tests\Fixtures\CustomCommandResolver;
use Symfony\Component\Console\Tests\Fixtures\LazyTestCommand;

/**
* @author Ivan Shcherbak <dev@funivan.com>
*/
class CommandResolverTest extends \PHPUnit_Framework_TestCase
{
public function testLazyCommandResolver()
{
$resolver = new CustomCommandResolver();
$names = $resolver->getAllNames();
$this->assertEquals(array('lazyTest'), $names);
}

public function testLazyLoadingCommands()
{
$application = new Application('foo', 'bar', new CustomCommandResolver());
$command = $application->get('lazyTest');
$this->assertNotEmpty($command);
$this->assertTrue($command instanceof LazyTestCommand);
}
}
Loading
0