-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c18f8e2
add command resolver (proof of concept)
997d45d
fix code style
1009753
fix php 5.3 $this context
997d431
fix code style according to code review
48c5426
fix cs. Remove white spaces
d883f89
fix code style and phpdoc
d07ee8a
fix phpdoc
caf62b3
fix code style according to @aitboudad code review
0d9ddec
fix spaces in test class
40f304a
Improve test command resolver class
64a9e67
add phpdoc
6b0f347
add phpdoc to add method
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Console/CommandsResolver/CommandResolver.php
This file contains hidden or 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,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); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Console/CommandsResolver/CommandResolverInterface.php
This file contains hidden or 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,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(); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Console/Tests/CommandResolver/CommandResolverTest.php
This file contains hidden or 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,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> | ||
*/ | ||
65E0 | 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()