8000 [TwigBridge] Added bundle name suggestion on wrongly overrided templa… · symfony/symfony@d9d6bf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9d6bf3

Browse files
author
Pascal Montoya
committed
[TwigBridge] Added bundle name suggestion on wrongly overrided templates paths
1 parent 6bbb5bc commit d9d6bf3

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* add a `workflow_metadata` function
8+
* add bundle name suggestion on wrongly overrided templates paths
89

910
3.4.0
1011
-----

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class DebugCommand extends Command
3131

3232
private $twig;
3333
private $projectDir;
34+
private $bundlesMetadata;
3435

35-
public function __construct(Environment $twig, string $projectDir = null)
36+
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array())
3637
{
3738
parent::__construct();
3839

3940
$this->twig = $twig;
4041
$this->projectDir = $projectDir;
42+
$this->bundlesMetadata = $bundlesMetadata;
4143
}
4244

4345
protected function configure()
@@ -83,6 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8385
$data['tests'] = array_keys($data['tests']);
8486
$data['loader_paths'] = $this->getLoaderPaths();
8587
$io->writeln(json_encode($data));
88+
$this->displayAlternatives($this->findWrongBundleOverrides($data['loader_paths']), $io);
8689

8790
return 0;
8891
}
@@ -108,6 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
108111
}
109112

110113
$rows = array();
114+
$loaderPaths = $this->getLoaderPaths();
111115
foreach ($this->getLoaderPaths() as $namespace => $paths) {
112116
if (count($paths) > 1) {
113117
$rows[] = array('', '');
@@ -123,6 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
123127
array_pop($rows);
124128
$io->section('Loader Paths');
125129
$io->table(array('Namespace', 'Paths'), $rows);
130+
$this->displayAlternatives($this->findWrongBundleOverrides($loaderPaths), $io);
126131

127132
return 0;
128133
}
@@ -242,4 +247,63 @@ private function getPrettyMetadata($type, $entity)
242247
return $meta ? '('.implode(', ', $meta).')' : '';
243248
}
244249
}
250+
251+
private function findWrongBundleOverrides($loaderPaths)
252+
{
253+
$alternatives = array();
254+
$paths = array_unique($loaderPaths['(None)']);
255+
$bundleNames = array();
256+
foreach ($paths as $path) {
257+
$relativePath = $path.'/bundles/';
258+
if (file_exists($dir = $this->projectDir.'/'.$relativePath)) {
259+
$folders = glob($dir.'*', GLOB_ONLYDIR);
260+
$bundleNames = array_reduce($folders, function ($carry, $absolutePath) use ($relativePath) {
261+
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
262+
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
263+
$name = ltrim(substr($path, \strlen($relativePath)), DIRECTORY_SEPARATOR);
264+
$carry[$name] = $path;
265+
}
266+
267+
return $carry;
268+
}, $bundleNames);
269+
}
270+
}
271+
272+
if (\count($bundleNames)) {
273+
$loadedBundles = $this->bundlesMetadata;
274+
$notFoundBundles = array_diff_key($bundleNames, $loadedBundles);
275+
if (\count($notFoundBundles)) {
276+
$alternatives = array();
277+
foreach ($notFoundBundles as $notFoundBundle => $path) {
278+
$alternatives[$path] = array();
279+
foreach ($loadedBundles as $name => $bundle) {
280+
$lev = levenshtein($notFoundBundle, $name);
281+
if ($lev <= \strlen($notFoundBundle) / 3 || false !== strpos($name, $notFoundBundle)) {
282+
$alternatives[$path][] = $name;
283+
}
284+
}
285+
}
286+
}
287+
}
288+
289+
return $alternatives;
290+
}
291+
292+
private function displayAlternatives(array $wrongBundles, SymfonyStyle $io)
293+
{
294+
foreach ($wrongBundles as $path => $alternatives) {
295+
$message = sprintf('Path "%s" not matching any bundle found', $path);
296+
if ($alternatives) {
297+
if (1 === \count($alternatives)) {
298+
$message .= sprintf(", did you mean \"%s\"?\n", $alternatives[0]);
299+
} else {
300+
$message .= ", did you mean one of these:\n";
301+
foreach ($alternatives as $bundle) {
302+
$message .= sprintf(" - %s\n", $bundle);
303+
}
304+
}
305+
}
306+
$io->warning(trim($message));
307+
}
308+
}
245309
}

src/Symfony/Bundle/TwigBundle/Resources/config/console.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<service id="twig.command.debug" class="Symfony\Bridge\Twig\Command\DebugCommand">
1111
<argument type="service" id="twig" />
1212
<argument>%kernel.project_dir%</argument>
13+
<argument>%kernel.bundles_metadata%</argument>
1314
<tag name="console.command" command="debug:twig" />
1415
</service>
1516

0 commit comments

Comments
 (0)
0