Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0f93cbd

Browse filesBrowse files
author
Amrouche Hamza
committed
[FrameworkBundle] [Console] add a warning when command is not found
1 parent 4271fec commit 0f93cbd
Copy full SHA for 0f93cbd

File tree

Expand file treeCollapse file tree

3 files changed

+85
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+85
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Console/Application.php
+23-2Lines changed: 23 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;
@@ -69,7 +71,15 @@ public function doRun(InputInterface $input, OutputInterface $output)
6971
$this->renderRegistrationErrors($input, $output);
7072
}
7173

72-
return parent::doRun($input, $output);
74+
try {
75+
return parent::doRun($input, $output);
76+
} catch (CommandNotFoundException $e) {
77+
if ($this->registrationErrors) {
78+
$this->renderRegistrationErrors($input, $output);
79+
}
80+
81+
throw $e;
82+
}
7383
}
7484

7585
/**
@@ -91,7 +101,18 @@ public function find($name)
91101
{
92102
$this->registerCommands();
93103

94-
return parent::find($name);
104+
try {
105+
return parent::find($name);
106+
} catch (CommandNotFoundException $e) {
107+
if ($this->registrationErrors) {
108+
foreach ($this->registrationErrors as $error) {
109+
$errors[] = $error->getMessage();
110+
}
111+
throw new CommandNotRegisteredException(implode('
112+
', $errors), 0, $e);
113+
}
114+
throw $e;
115+
}
95116
}
96117

97118
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php
+31Lines 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();
+31Lines 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 <fabien@symfony.com>
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 <hamza.simperfit@gmail.com>
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.