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

Skip to content

Commit d148fa7

Browse files
feature #26919 [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths (pmontoya, Pascal Montoya)
This PR was merged into the 4.2-dev branch. Discussion ---------- [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26898 | License | MIT Report unknown folders in templates/bundles in debug:twig and make suggestions if any Developped with @bricejulia Commits ------- acfb325 refs #26898 Remove unnecessary loop Trim messages on build rather than on display da42b3e refs #26898 Check if projectDir is not null before loop da0c589 refs #26898 Move changelog entry to 4.1.0 to 4.2.0 7d9467a Add an entry on json export format bab9d99 Use %twig.default_path% parameter and search in old folder structure too 1758de2 [TwigBridge] Added bundle name suggestion on wrongly overrided templates paths
2 parents f065873 + acfb325 commit d148fa7

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* add bundle name suggestion on wrongly overridden templates paths
8+
49
4.1.0
510
-----
611

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

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

3232
private $twig;
3333
private $projectDir;
34+
private $bundlesMetadata;
35+
private $twigDefaultPath;
36+
private $rootDir;
3437

35-
public function __construct(Environment $twig, string $projectDir = null)
38+
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array(), string $twigDefaultPath = null, string $rootDir = null)
3639
{
3740
parent::__construct();
3841

3942
$this->twig = $twig;
4043
$this->projectDir = $projectDir;
44+
$this->bundlesMetadata = $bundlesMetadata;
45+
$this->twigDefaultPath = $twigDefaultPath;
46+
$this->rootDir = $rootDir;
4147
}
4248

4349
protected function configure()
@@ -82,6 +88,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8288
}
8389
$data['tests'] = array_keys($data['tests']);
8490
$data['loader_paths'] = $this->getLoaderPaths();
91+
if ($wrongBundles = $this->findWrongBundleOverrides()) {
92+
$data['warnings'] = $this->buildWarningMessages($wrongBundles);
93+
}
94+
8595
$io->writeln(json_encode($data));
8696

8797
return 0;
@@ -123,6 +133,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
123133
array_pop($rows);
124134
$io->section('Loader Paths');
125135
$io->table(array('Namespace', 'Paths'), $rows);
136+
$messages = $this->buildWarningMessages($this->findWrongBundleOverrides());
137+
foreach ($messages as $message) {
138+
$io->warning($message);
139+
}
126140

127141
return 0;
128142
}
@@ -242,4 +256,85 @@ private function getPrettyMetadata($type, $entity)
242256
return $meta ? '('.implode(', ', $meta).')' : '';
243257
}
244258
}
259+
260+
private function findWrongBundleOverrides(): array
261+
{
262+
$alternatives = array();
263+
$bundleNames = array();
264+
265+
if ($this->rootDir && $this->projectDir) {
266+
$folders = glob($this->rootDir.'/Resources/*/views', GLOB_ONLYDIR);
267+
$relativePath = ltrim(substr($this->rootDir.'/Resources/', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
268+
$bundleNames = array_reduce(
269+
$folders,
270+
function ($carry, $absolutePath) use ($relativePath) {
271+
if (0 === strpos($absolutePath, $this->projectDir)) {
272+
$name = basename(\dirname($absolutePath));
273+
$path = $relativePath.$name;
274+
$carry[$name] = $path;
275+
}
276+
277+
return $carry;
278+
},
279+
$bundleNames
280+
);
281+
}
282+
283+
if ($this->twigDefaultPath && $this->projectDir) {
284+
$folders = glob($this->twigDefaultPath.'/bundles/*', GLOB_ONLYDIR);
285+
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
286+
$bundleNames = array_reduce(
287+
$folders,
288+
function ($carry, $absolutePath) use ($relativePath) {
289+
if (0 === strpos($absolutePath, $this->projectDir)) {
290+
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
291+
$name = ltrim(substr($path, \strlen($relativePath)), DIRECTORY_SEPARATOR);
292+
$carry[$name] = $path;
293+
}
294+
295+
return $carry;
296+
},
297+
$bundleNames
298+
);
299+
}
300+
301+
if (\count($bundleNames)) {
302+
$notFoundBundles = array_diff_key($bundleNames, $this->bundlesMetadata);
303+
if (\count($notFoundBundles)) {
304+
$alternatives = array();
305+
foreach ($notFoundBundles as $notFoundBundle => $path) {
306+
$alternatives[$path] = array();
307+
foreach ($this->bundlesMetadata as $name => $bundle) {
308+
$lev = levenshtein($notFoundBundle, $name);
309+
if ($lev <= \strlen($notFoundBundle) / 3 || false !== strpos($name, $notFoundBundle)) {
310+
$alternatives[$path][] = $name;
311+
}
312+
}
313+
}
314+
}
315+
}
316+
317+
return $alternatives;
318+
}
319+
320+
private function buildWarningMessages(array $wrongBundles): array
321+
{
322+
$messages = array();
323+
foreach ($wrongBundles as $path => $alternatives) {
324+
$message = sprintf('Path "%s" not matching any bundle found', $path);
325+
if ($alternatives) {
326+
if (1 === \count($alternatives)) {
327+
$message .= sprintf(", did you mean \"%s\"?\n", $alternatives[0]);
328+
} else {
329+
$message .= ", did you mean one of these:\n";
330+
foreach ($alternatives as $bundle) {
331+
$message .= sprintf(" - %s\n", $bundle);
332+
}
333+
}
334+
}
335+
$messages[] = trim($message);
336+
}
337+
338+
return $messages;
339+
}
245340
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
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>
14+
<argument>%twig.default_path%</argument>
15+
<argument>%kernel.root_dir%</argument>
1316
<tag name="console.command" command="debug:twig" />
1417
</service>
1518

0 commit comments

Comments
 (0)