Skip to content

Commit

Permalink
[TwigBridge] Added bundle name suggestion on wrongly overrided templa…
Browse files Browse the repository at this point in the history
…tes paths
  • Loading branch information
Pascal Montoya committed Jun 18, 2018
1 parent 28d754d commit 1758de2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* add a `workflow_metadata` function
* add bundle name suggestion on wrongly overrided templates paths

3.4.0
-----
Expand Down
68 changes: 66 additions & 2 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Expand Up @@ -31,13 +31,15 @@ class DebugCommand extends Command

private $twig;
private $projectDir;
private $bundlesMetadata;

public function __construct(Environment $twig, string $projectDir = null)
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array())
{
parent::__construct();

$this->twig = $twig;
$this->projectDir = $projectDir;
$this->bundlesMetadata = $bundlesMetadata;
}

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

return 0;
}
Expand All @@ -108,7 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$rows = array();
foreach ($this->getLoaderPaths() as $namespace => $paths) {
$loaderPaths = $this->getLoaderPaths();
foreach ($loaderPaths as $namespace => $paths) {
if (count($paths) > 1) {
$rows[] = array('', '');
}
Expand All @@ -123,6 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
array_pop($rows);
$io->section('Loader Paths');
$io->table(array('Namespace', 'Paths'), $rows);
$this->displayAlternatives($this->findWrongBundleOverrides($loaderPaths), $io);

return 0;
}
Expand Down Expand Up @@ -242,4 +247,63 @@ private function getPrettyMetadata($type, $entity)
return $meta ? '('.implode(', ', $meta).')' : '';
}
}

private function findWrongBundleOverrides($loaderPaths)
{
$alternatives = array();
$paths = array_unique($loaderPaths['(None)']);
$bundleNames = array();
foreach ($paths as $path) {
$relativePath = $path.'/bundles/';
if (file_exists($dir = $this->projectDir.'/'.$relativePath)) {
$folders = glob($dir.'*', GLOB_ONLYDIR);
$bundleNames = array_reduce($folders, function ($carry, $absolutePath) use ($relativePath) {
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
$name = ltrim(substr($path, \strlen($relativePath)), DIRECTORY_SEPARATOR);
$carry[$name] = $path;
}

return $carry;
}, $bundleNames);
}
}

if (\count($bundleNames)) {
$loadedBundles = $this->bundlesMetadata;
$notFoundBundles = array_diff_key($bundleNames, $loadedBundles);
if (\count($notFoundBundles)) {
$alternatives = array();
foreach ($notFoundBundles as $notFoundBundle => $path) {
$alternatives[$path] = array();
foreach ($loadedBundles as $name => $bundle) {
$lev = levenshtein($notFoundBundle, $name);
if ($lev <= \strlen($notFoundBundle) / 3 || false !== strpos($name, $notFoundBundle)) {
$alternatives[$path][] = $name;
}
}
}
}
}

return $alternatives;
}

private function displayAlternatives(array $wrongBundles, SymfonyStyle $io)
{
foreach ($wrongBundles as $path => $alternatives) {
$message = sprintf('Path "%s" not matching any bundle found', $path);
if ($alternatives) {
if (1 === \count($alternatives)) {
$message .= sprintf(", did you mean \"%s\"?\n", $alternatives[0]);
} else {
$message .= ", did you mean one of these:\n";
foreach ($alternatives as $bundle) {
$message .= sprintf(" - %s\n", $bundle);
}
}
}
$io->warning(trim($message));
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/console.xml
Expand Up @@ -10,6 +10,7 @@
<service id="twig.command.debug" class="Symfony\Bridge\Twig\Command\DebugCommand">
<argument type="service" id="twig" />
<argument>%kernel.project_dir%</argument>
<argument>%kernel.bundles_metadata%</argument>
<tag name="console.command" command="debug:twig" />
</service>

Expand Down

0 comments on commit 1758de2

Please sign in to comment.