Skip to content

Commit 268e80f

Browse files
[Console] Added suggestions for missing packages
1 parent 4d21ab4 commit 268e80f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Symfony/Component/Console/Application.php

+41
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@
6262
*/
6363
class Application
6464
{
65+
const SUGGESTED_PACKAGES = [
66+
'doctrine' => [
67+
'Doctrine ORM',
68+
'symfony/orm-pack',
69+
],
70+
'generate' => [
71+
'SensioGeneratorBundle',
72+
'sensio/generator-bundle',
73+
],
74+
'make' => [
75+
'MakerBundle',
76+
'symfony/maker-bundle --dev',
77+
],
78+
'server' => [
79+
'Symfony Web Server',
80+
'symfony/web-server-bundle --dev',
81+
],
82+
];
83+
6584
private $commands = array();
6685
private $wantHelps = false;
6786
private $runningCommand;
@@ -563,6 +582,8 @@ public function findNamespace($namespace)
563582
$namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);
564583

565584
if (empty($namespaces)) {
585+
$this->suggestPackage($namespace);
586+
566587
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
567588

568589
if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {
@@ -1171,4 +1192,24 @@ private function init()
11711192
$this->add($command);
11721193
}
11731194
}
1195+
1196+
/**
1197+
* Suggests a package, that should be installed via composer,
1198+
* if the package is missing, and the provided namespace can be mapped to a Symfony bundle.
1199+
*
1200+
* @param string $namespace A namespace to search for
1201+
*
1202+
* @return void
1203+
*
1204+
* @throws NamespaceNotFoundException When namespace is mapped to a Symfony bundle
1205+
*/
1206+
private function suggestPackage($namespace) {
1207+
if (isset(self::SUGGESTED_PACKAGES[$namespace])) {
1208+
$suggestion = self::SUGGESTED_PACKAGES[$namespace];
1209+
1210+
$message = sprintf("It seems that you are trying to run a command from '%s' bundle/package, but you do not have it installed. You may try installing the missing bundle/package by running:\n\n composer require %s", $suggestion[0], $suggestion[1]);
1211+
1212+
throw new NamespaceNotFoundException($message);
1213+
}
1214+
}
11741215
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ public function testFindInvalidNamespace()
304304
$application->findNamespace('bar');
305305
}
306306

307+
/**
308+
* @expectedException \Symfony\Component\Console\Exception\NamespaceNotFoundException
309+
* @expectedExceptionMessageRegExp /^It seems that you are trying to run a command from .+/
310+
*/
311+
public function testFindSuggestedPackage()
312+
{
313+
$application = new Application();
314+
$application->findNamespace('make');
315+
}
316+
307317
/**
308318
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
309319
* @expectedExceptionMessage Command "foo1" is not defined

0 commit comments

Comments
 (0)