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

Skip to content

Commit 94629b8

Browse files
Pascal MontoyaPascal Montoya
Pascal Montoya
authored and
Pascal Montoya
committed
Use %twig.default_path% parameter and search in old folder structure too
1 parent a997038 commit 94629b8

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ class DebugCommand extends Command
3232
private $twig;
3333
private $projectDir;
3434
private $bundlesMetadata;
35+
private $twigDefaultPath;
36+
private $rootDir;
3537

36-
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array())
38+
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = array(), string $twigDefaultPath = null, string $rootDir = null)
3739
{
3840
parent::__construct();
3941

4042
$this->twig = $twig;
4143
$this->projectDir = $projectDir;
4244
$this->bundlesMetadata = $bundlesMetadata;
45+
$this->twigDefaultPath = $twigDefaultPath;
46+
$this->rootDir = $rootDir;
4347
}
4448

4549
protected function configure()
@@ -85,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8589
$data['tests'] = array_keys($data['tests']);
8690
$data['loader_paths'] = $this->getLoaderPaths();
8791
$io->writeln(json_encode($data));
88-
$this->displayAlternatives($this->findWrongBundleOverrides($data['loader_paths']), $io);
92+
$this->displayAlternatives($this->findWrongBundleOverrides(), $io);
8993

9094
return 0;
9195
}
@@ -111,8 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
111115
}
112116

113117
$rows = array();
114-
$loaderPaths = $this->getLoaderPaths();
115-
foreach ($loaderPaths as $namespace => $paths) {
118+
foreach ($this->getLoaderPaths() as $namespace => $paths) {
116119
if (count($paths) > 1) {
117120
$rows[] = array('', '');
118121
}
@@ -127,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
127130
array_pop($rows);
128131
$io->section('Loader Paths');
129132
$io->table(array('Namespace', 'Paths'), $rows);
130-
$this->displayAlternatives($this->findWrongBundleOverrides($loaderPaths), $io);
133+
$this->displayAlternatives($this->findWrongBundleOverrides(), $io);
131134

132135
return 0;
133136
}
@@ -248,25 +251,45 @@ private function getPrettyMetadata($type, $entity)
248251
}
249252
}
250253

251-
private function findWrongBundleOverrides($loaderPaths)
254+
private function findWrongBundleOverrides(): array
252255
{
253256
$alternatives = array();
254-
$paths = array_unique($loaderPaths['(None)']);
255257
$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) {
258+
259+
if ($this->rootDir) {
260+
$folders = glob($this->rootDir.'/Resources/*/views', GLOB_ONLYDIR);
261+
$relativePath = ltrim(substr($this->rootDir.'/Resources/', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
262+
$bundleNames = array_reduce(
263+
$folders,
264+
function ($carry, $absolutePath) use ($relativePath) {
265+
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
266+
$name = basename(\dirname($absolutePath));
267+
$path = $relativePath.$name;
268+
$carry[$name] = $path;
269+
}
270+
271+
return $carry;
272+
},
273+
$bundleNames
274+
);
275+
}
276+
277+
if ($this->twigDefaultPath) {
278+
$folders = glob($this->twigDefaultPath.'/bundles/*', GLOB_ONLYDIR);
279+
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
280+
$bundleNames = array_reduce(
281+
$folders,
282+
function ($carry, $absolutePath) use ($relativePath) {
261283
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
262284
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
263285
$name = ltrim(substr($path, \strlen($relativePath)), DIRECTORY_SEPARATOR);
264286
$carry[$name] = $path;
265287
}
266288

267289
return $carry;
268-
}, $bundleNames);
269-
}
290+
},
291+
$bundleNames
292+
);
270293
}
271294

272295
if (\count($bundleNames)) {
@@ -289,7 +312,7 @@ private function findWrongBundleOverrides($loaderPaths)
289312
return $alternatives;
290313
}
291314

292-
private function displayAlternatives(array $wrongBundles, SymfonyStyle $io)
315+
private function displayAlternatives(array $wrongBundles, SymfonyStyle $io): void
293316
{
294317
foreach ($wrongBundles as $path => $alternatives) {
295318
$message = sprintf('Path "%s" not matching any bundle found', $path);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<argument type="service" id="twig" />
1212
<argument>%kernel.project_dir%</argument>
1313
<argument>%kernel.bundles_metadata%</argument>
14+
<argument>%twig.default_path%</argument>
15+
<argument>%kernel.root_dir%</argument>
1416
<tag name="console.command" command="debug:twig" />
1517
</service>
1618

0 commit comments

Comments
 (0)