Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcfa734

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] [Console] add a warning when command is not found
1 parent 4271fec commit dcfa734

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14+
use Symfony\Component\Console\Exception\CommandNotFoundException;
15+
use Symfony\Component\Console\Exception\CommandNotRegisteredException;
1416
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1517
use Symfony\Component\Console\Style\SymfonyStyle;
1618
use Symfony\Component\Debug\Exception\FatalThrowableError;
@@ -91,7 +93,18 @@ public function find($name)
9193
{
9294
$this->registerCommands();
9395

94-
return parent::find($name);
96+
try {
97+
return parent::find($name);
98+
} catch (CommandNotFoundException $e) {
99+
if ($this->registrationErrors) {
100+
foreach ($this->registrationErrors as $error) {
101+
$errors[] = $error->getMessage();
102+
}
103+
throw new CommandNotRegisteredException(implode('
104+
', $errors), 0, $e);
105+
}
106+
throw $e;
107+
}
95108
}
96109

97110
/**
@@ -101,7 +114,18 @@ public function get($name)
101114
{
102115
$this->registerCommands();
103116

104-
$command = parent::get($name);
117+
try {
118+
$command = parent::get($name);
119+
} catch (CommandNotFoundException $e) {
120+
if ($this->registrationErrors) {
121+
foreach ($this->registrationErrors as $error) {
122+
$errors[] = $error->getMessage();
123+
}
124+
throw new CommandNotRegisteredException(implode('
125+
', $errors));
126+
}
127+
throw $e;
128+
}
105129

106130
if ($command instanceof ContainerAwareInterface) {
107131
$command->setContainer($this->kernel->getContainer());

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
165165
$this->assertContains('fine', $output);
166166
}
167167

168+
public function testRunOnlyWarnsOnNoName()
169+
{
170+
$container = new ContainerBuilder();
171+
$container->register('event_dispatcher', EventDispatcher::class);
172+
$container->register(ThrowingCommand::class, ThrowingCommand::class);
173+
$container->setParameter('console.command.ids', array(ThrowingCommand::class => ThrowingCommand::class));
174+
175+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
176+
$kernel
177+
->method('getBundles')
178+
->willReturn(array($this->createBundleMock(
179+
array((new Command(null))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); }))
180+
)));
181+
$kernel
182+
->method('getContainer')
183+
->willReturn($container);
184+
185+
$application = new Application($kernel);
186+
$application->setAutoExit(false);
187+
188+
$tester = new ApplicationTester($application);
189+
$tester->run(array('command' => 'fine'));
190+
$tester->getOutput();
191+
$output = $tester->getDisplay();
192+
193+
$this->assertSame(1, $tester->getStatusCode());
194+
$this->assertContains('Some commands could not be registered:', $output);
195+
$this->assertContains('throwing', $output);
196+
$this->assertContains('fine', $output);
197+
}
198+
168199
private function getKernel(array $bundles, $useDispatcher = false)
169200
{
170201
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* Represents a non-registered command.
16+
*
17+
* @author Hamza Amrouche <[email protected]>
18+
*/
19+
class CommandNotRegisteredException extends CommandNotFoundException
20+
{
21+
/**
22+
* @param string $message Exception message to throw
23+
* @param int $code Exception code
24+
* @param \Exception $previous Previous exception used for the exception chaining
25+
*/
26+
public function __construct($message, $code = 0, \Exception $previous = null)
27+
{
28+
$message = sprintf("[WARNING] Some commands could not be registered: \n %s", $message);
29+
parent::__construct($message, $previous instanceof CommandNotFoundException ? $previous->getAlternatives() : array(), $code, $previous);
30+
}
31+
}

0 commit comments

Comments
 (0)