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

Skip to content

Commit 5c34ae4

Browse files
committed
feature #17484 [FrameworkBundle][DX] Add Levenshtein suggesters to AbstractConfigCommand (kix)
This PR was merged into the 3.1-dev branch. Discussion ---------- [FrameworkBundle][DX] Add Levenshtein suggesters to AbstractConfigCommand | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | none It could be helpful to output the best guesses for bundle names and container extension aliases when one could not be found by the exact query. Perhaps, I could regroup the logic so that it only looks through bundle names if the `Bundle` suffix is present, but I guess this might narrow the use case scope here. Commits ------- 3c0b0ae Add Levenshtein suggesters to AbstractConfigCommand
2 parents bcf0e91 + 3c0b0ae commit 5c34ae4

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ protected function listBundles($output)
5252
protected function findExtension($name)
5353
{
5454
$bundles = $this->initializeBundles();
55+
$minScore = INF;
56+
5557
foreach ($bundles as $bundle) {
5658
if ($name === $bundle->getName()) {
5759
if (!$bundle->getContainerExtension()) {
@@ -61,16 +63,37 @@ protected function findExtension($name)
6163
return $bundle->getContainerExtension();
6264
}
6365

66+
$distance = levenshtein($name, $bundle->getName());
67+
68+
if ($distance < $minScore) {
69+
$guess = $bundle->getName();
70+
$minScore = $distance;
71+
}
72+
6473
$extension = $bundle->getContainerExtension();
65-
if ($extension && $name === $extension->getAlias()) {
66-
return $extension;
74+
75+
if ($extension) {
76+
if ($name === $extension->getAlias()) {
77+
return $extension;
78+
}
79+
80+
$distance = levenshtein($name, $extension->getAlias());
81+
82+
if ($distance < $minScore) {
83+
$guess = $extension->getAlias();
84+
$minScore = $distance;
85+
}
6786
}
6887
}
6988

7089
if ('Bundle' !== substr($name, -6)) {
71-
$message = sprintf('No extensions with configuration available for "%s"', $name);
90+
$message = sprintf('No extensions with configuration available for "%s".', $name);
7291
} else {
73-
$message = sprintf('No extension with alias "%s" is enabled', $name);
92+
$message = sprintf('No extension with alias "%s" is enabled.', $name);
93+
}
94+
95+
if (isset($guess) && $minScore < 3) {
96+
$message .= sprintf("\n\nDid you mean \"%s\"?", $guess);
7497
}
7598

7699
throw new \LogicException($message);

0 commit comments

Comments
 (0)