Skip to content

Commit 01e2ccf

Browse files
author
Ivan Gavryshko
committed
MAGETWO-35133: Search for Commands
- changes in readme according to CR - throws exceprtion in case if command class does not exist and added testcase for that - added docbloc descriptions for classes
1 parent e210f78 commit 01e2ccf

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

dev/tools/Magento/Tools/Console/CommandList.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace Magento\Tools\Console;
88

9-
use Symfony\Component\Console\Command\Command;
10-
9+
/**
10+
* Class CommandList contains predefined list of command classes for Tools
11+
*
12+
* @package Magento\Tools\Console
13+
*/
1114
class CommandList
1215
{
1316
/**
@@ -24,17 +27,17 @@ protected function getCommandsClasses()
2427
* Gets list of command instances
2528
*
2629
* @return \Symfony\Component\Console\Command\Command[]
30+
* @throws \Exception
2731
*/
2832
public function getCommands()
2933
{
3034
$commands = [];
3135

3236
foreach ($this->getCommandsClasses() as $class) {
3337
if (class_exists($class)) {
34-
$command = new $class;
35-
if ($command instanceof Command) {
36-
$commands[] = $command;
37-
}
38+
$commands[] = new $class;
39+
} else {
40+
throw new \Exception('Class ' . $class . ' does not exist');
3841
}
3942
}
4043

lib/internal/Magento/Framework/App/Test/Unit/Console/CommandListTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ public function testGetCommands()
3535
$this->objectManager->expects($this->once())->method('get')->with('Symfony\Component\Console\Command\Command');
3636
$this->commandList->getCommands();
3737
}
38+
39+
/**
40+
* @expectedException \Exception
41+
* @expectedExceptionMessage Class Symfony\Component\Console\Command\WrongCommand does not exist
42+
*/
43+
public function testGetCommandsException()
44+
{
45+
$wrongCommands =[
46+
'Symfony\Component\Console\Command\WrongCommand'
47+
];
48+
$commandList = new CommandList($this->objectManager, $wrongCommands);
49+
$commandList->getCommands();
50+
}
3851
}

lib/internal/Magento/Framework/Console/CommandList.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
namespace Magento\Framework\Console;
88

99
use Magento\Framework\ObjectManagerInterface;
10-
use Symfony\Component\Console\Command\Command;
1110

11+
/**
12+
* Class CommandList has a list of commands, which can be extended via DI configuration.
13+
*
14+
* @package Magento\Framework\Console
15+
*/
1216
class CommandList
1317
{
1418
/**
@@ -37,16 +41,16 @@ public function __construct(ObjectManagerInterface $objectManager, array $comman
3741
* Gets list of command instances
3842
*
3943
* @return \Symfony\Component\Console\Command\Command[]
44+
* @throws \Exception
4045
*/
4146
public function getCommands()
4247
{
4348
$commands = [];
4449
foreach ($this->commands as $class) {
4550
if (class_exists($class)) {
46-
$command = $this->objectManager->get($class);
47-
if ($command instanceof Command) {
48-
$commands[] = $command;
49-
}
51+
$commands[] = $this->objectManager->get($class);
52+
} else {
53+
throw new \Exception('Class ' . $class . ' does not exist');
5054
}
5155
}
5256

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
# Console
22

3-
This component has a list of commands, which can be extended via DI configuration.
3+
This component contains Magento Cli and can be extended via DI configuration.
4+
5+
For example we can introduce new command in module using di.xml:
6+
7+
```
8+
<type name="Magento\Framework\Console\CommandList">
9+
<arguments>
10+
<argument name="commands" xsi:type="array">
11+
<item name="session_cleaner" xsi:type="string">Magento\MyModule\Console\TestMeCommand</item>
12+
</argument>
13+
</arguments>
14+
</type>
15+
```
16+

setup/src/Magento/Setup/Console/CommandList.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
namespace Magento\Setup\Console;
88

99
use Zend\ServiceManager\ServiceManager;
10-
use Symfony\Component\Console\Command\Command;
1110

11+
/**
12+
* Class CommandList contains predefined list of commands for Setup
13+
*
14+
* @package Magento\Setup\Console
15+
*/
1216
class CommandList
1317
{
1418
/**
@@ -44,17 +48,17 @@ protected function getCommandsClasses()
4448
* Gets list of command instances
4549
*
4650
* @return \Symfony\Component\Console\Command\Command[]
51+
* @throws \Exception
4752
*/
4853
public function getCommands()
4954
{
5055
$commands = [];
5156

5257
foreach ($this->getCommandsClasses() as $class) {
5358
if (class_exists($class)) {
54-
$command = $this->serviceManager->create($class);
55-
if ($command instanceof Command) {
56-
$commands[] = $command;
57-
}
59+
$commands[] = $this->serviceManager->create($class);
60+
} else {
61+
throw new \Exception('Class ' . $class . ' does not exist');
5862
}
5963
}
6064

0 commit comments

Comments
 (0)