8000 Use %twig.default_path% parameter and search in old folder structure too · symfony/symfony@a3159d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a3159d4

Browse files
author
Pascal Montoya
committed
Use %twig.default_path% parameter and search in old folder structure too
1 parent 9f96cd8 commit a3159d4

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

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

Lines changed: 41 additions & 22 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, string $rootDir)
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,26 +251,42 @@ 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) {
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-
}
266258

267-
return $carry;
268-
}, $bundleNames);
269-
}
270-
}
259+
$folders = glob($this->rootDir.'/Resources/*/views', GLOB_ONLYDIR);
260+
$relativePath = ltrim(substr($this->rootDir.'/Resources/', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
261+
$bundleNames = array_reduce(
262+
$folders,
263+
function ($carry, $absolutePath) use ($relativePath) {
264+
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
265+
$name = basename(dirname($absolutePath));
266+
$path = $relativePath.$name;
267+
$carry[$name] = $path;
268+
}
269+
270+
return $carry;
271+
},
272+
$bundleNames
273+
);
274+
275+
$folders = glob($this->twigDefaultPath.'/bundles/*', GLOB_ONLYDIR);
276+
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles', \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
277+
$bundleNames = array_reduce(
278+
$folders,
279+
function ($carry, $absolutePath) use ($relativePath) {
280+
if (null !== $this->projectDir && 0 === strpos($absolutePath, $this->projectDir)) {
281+
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), DIRECTORY_SEPARATOR);
282+
$name = ltrim(substr($path, \strlen($relativePath)), DIRECTORY_SEPARATOR);
283+
$carry[$name] = $path;
284+
}
285+
286+
return $carry;
287+
},
288+
$bundleNames
289+
);
271290

272291
if (\count($bundleNames)) {
273292
$loadedBundles = $this->bundlesMetadata;
@@ -289,7 +308,7 @@ private function findWrongBundleOverrides($loaderPaths)
289308
return $alternatives;
290309
}
291310

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" ?>
22

33
<container xmlns="http://symfony.com/schema/dic/services"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
88
<defaults public="false" />
@@ -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)
0