8000 bug #29481 [TwigBridge] Deprecating legacy Twig paths in DebugCommand… · gido/symfony@8816b56 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8816b56

Browse files
bug symfony#29481 [TwigBridge] Deprecating legacy Twig paths in DebugCommand and simplifications (yceruto)
This PR was squashed before being merged into the 4.2 branch (closes symfony#29481). Discussion ---------- [TwigBridge] Deprecating legacy Twig paths in DebugCommand and simplifications | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I've also added some missing tests. Commits ------- 96169b8 [TwigBridge] Deprecating legacy Twig paths in DebugCommand and simplifications
2 parents 7ad8341 + 96169b8 commit 8816b56

File tree

5 files changed

+77
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ private function displayGeneralText(SymfonyStyle $io, string $filter = null)
208208
$io->table(array('Namespace', 'Paths'), $this->buildTableRows($paths));
209209
}
210210

211-
if ($wronBundles = $this->findWrongBundleOverrides()) {
212-
foreach ($this->buildWarningMessages($wronBundles) as $message) {
211+
if ($wrongBundles = $this->findWrongBundleOverrides()) {
212+
foreach ($this->buildWarningMessages($wrongBundles) as $message) {
213213
$io->warning($message);
214214
}
215215
}
@@ -253,13 +253,7 @@ private function getLoaderPaths(string $name = null): array
253253
}
254254

255255
foreach ($namespaces as $namespace) {
256-
$paths = array_map(function ($path) {
257-
if (null !== $this->projectDir && 0 === strpos($path, $this->projectDir)) {
258-
$path = ltrim(substr($path, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
259-
}
260-
261-
return $path;
262-
}, $loader->getPaths($namespace));
256+
$paths = array_map(array($this, 'getRelativePath'), $loader->getPaths($namespace));
263257

264258
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
265259
$namespace = '(None)';
@@ -368,53 +362,38 @@ private function findWrongBundleOverrides(): array
368362

369363
if ($this->rootDir && $this->projectDir) {
370364
$folders = glob($this->rootDir.'/Resources/*/views', GLOB_ONLYDIR);
371-
$relativePath = ltrim(substr($this->rootDir.'/Resources/', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
372-
$bundleNames = array_reduce(
373-
$folders,
374-
8000 function ($carry, $absolutePath) use ($relativePath) {
375-
if (0 === strpos($absolutePath, $this->projectDir)) {
376-
$name = basename(\dirname($absolutePath));
377-
$path = $relativePath.$name;
378-
$carry[$name] = $path;
379-
}
365+
$relativePath = ltrim(substr($this->rootDir.\DIRECTORY_SEPARATOR.'Resources/', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
366+
$bundleNames = array_reduce($folders, function ($carry, $absolutePath) use ($relativePath) {
367+
if (0 === strpos($absolutePath, $this->projectDir)) {
368+
$name = basename(\dirname($absolutePath));
369+
$path = ltrim($relativePath.$name, \DIRECTORY_SEPARATOR);
370+
$carry[$name] = $path;
371+
372+
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $absolutePath, $this->twigDefaultPath.'/bundles/'.$name), E_USER_DEPRECATED);
373+
}
380374

381-
return $carry;
382-
},
383-
$bundleNames
384-
);
375+
return $carry;
376+
}, $bundleNames);
385377
}
386378

387379
if ($this->twigDefaultPath && $this->projectDir) {
388380
$folders = glob($this->twigDefaultPath.'/bundles/*', GLOB_ONLYDIR);
389-
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
390-
$bundleNames = array_reduce(
391-
$folders,
392-
function ($carry, $absolutePath) use ($relativePath) {
393-
if (0 === strpos($absolutePath, $this->projectDir)) {
394-
$path = ltrim(substr($absolutePath, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
395-
$name = ltrim(substr($path, \strlen($relativePath)), \DIRECTORY_SEPARATOR);
396-
$carry[$name] = $path;
397-
}
398-
399-
return $carry;
400-
},
401-
$bundleNames
402-
);
403-
}
404-
405-
if (\count($bundleNames)) {
406-
$notFoundBundles = array_diff_key($bundleNames, $this->bundlesMetadata);
407-
if (\count($notFoundBundles)) {
408-
$alternatives = array();
409-
foreach ($notFoundBundles as $notFoundBundle => $path) {
410-
$alternatives[$path] = array();
411-
foreach ($this->bundlesMetadata as $name => $bundle) {
412-
$lev = levenshtein($notFoundBundle, $name);
413-
if ($lev <= \strlen($notFoundBundle) / 3 || false !== strpos($name, $notFoundBundle)) {
414-
$alternatives[$path][] = $name;
415-
}
416-
}
381+
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles/', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
382+
$bundleNames = array_reduce($folders, function ($carry, $absolutePath) use ($relativePath) {
383+
if (0 === strpos($absolutePath, $this->projectDir)) {
384+
$name = basename($absolutePath);
385+
$path = ltrim($relativePath.$name, \DIRECTORY_SEPARATOR);
386+
$carry[$name] = $path;
417387
}
388+
389+
return $carry;
390+
}, $bundleNames);
391+
}
392+
393+
if ($notFoundBundles = array_diff_key($bundleNames, $this->bundlesMetadata)) {
394+
$alternatives = array();
395+
foreach ($notFoundBundles as $notFoundBundle => $path) {
396+
$alternatives[$path] = $this->findAlternatives($notFoundBundle, array_keys($this->bundlesMetadata));
418397
}
419398
}
420399

Original file line numberDiff line numberDiff line change
@@ -42,6 +42,52 @@ public function testFilterAndJsonFormatOptions()
4242
$this->assertEquals($expected, json_decode($tester->getDisplay(true), true));
4343
}
4444

45+
public function testWarningsWrongBundleOverriding()
46+
{
47+
$bundleMetadata = array(
48+
'TwigBundle' => 'vendor/twig-bundle/',
49+
'WebProfilerBundle' => 'vendor/web-profiler-bundle/',
50+
);
51+
$defaultPath = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'templates';
52+
53+
$tester = $this->createCommandTester(array(), $bundleMetadata, $defaultPath);
54+
$ret = $tester->execute(array('--filter' => 'unknown', '--format' => 'json'), array('decorated' => false));
55+
56+
$expected = array('warnings' => array(
57+
'Path "templates/bundles/UnknownBundle" not matching any bundle found',
58+
'Path "templates/bundles/WebProfileBundle" not matching any bundle found, did you mean "WebProfilerBundle"?',
59+
));
60+
61+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
62+
$this->assertEquals($expected, json_decode($tester->getDisplay(true), true));
63+
}
64+
65+
/**
66+
* @group legacy
67+
* @expectedDeprecation Templates directory "%sResources/BarBundle/views" is deprecated since Symfony 4.2, use "%stemplates/bundles/BarBundle" instead.
68+
*/
69+
public function testDeprecationForWrongBundleOverridingInLegacyPath()
70+
{
71+
$bundleMetadata = array(
72+
'TwigBundle' => 'vendor/twig-bundle/',
73+
'WebProfilerBundle' => 'vendor/web-profiler-bundle/',
74+
);
75+
$defaultPath = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'templates';
76+
$rootDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
77+
78+
$tester = $this->createCommandTester(array(), $bundleMetadata, $defaultPath, $rootDir);
79+
$ret = $tester->execute(array('--filter' => 'unknown', '--format' => 'json'), array('decorated' => false));
80+
81+
$expected = array('warnings' => array(
82+
'Path "Resources/BarBundle" not matching any bundle found',
83+
'Path "templates/bundles/UnknownBundle" not matching any bundle found',
84+
'Path "templates/bundles/WebProfileBundle" not matching any bundle found, did you mean "WebProfilerBundle"?',
85+
));
86+
87+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
88+
$this->assertEquals($expected, json_decode($tester->getDisplay(true), true));
89+
}
90+
4591
/**
4692
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
4793
* @expectedExceptionMessage Malformed namespaced template name "@foo" (expecting "@namespace/template_name").
@@ -233,7 +279,7 @@ public function getDebugTemplateNameTestData()
233279
);
234280
}
235281

236-
private function createCommandTester(array $paths = array()): CommandTester
282+
private function createCommandTester(array $paths = array(), array $bundleMetadata = array(), string $defaultPath = null, string $rootDir = null): CommandTester
237283
{
238284
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
239285
$loader = new FilesystemLoader(array(), $projectDir);
@@ -246,7 +292,7 @@ private function createCommandTester(array $paths = array()): CommandTester
246292
}
247293

248294
$application = new Application();
249-
$application->add(new DebugCommand(new Environment($loader), $projectDir));
295+
$application->add(new DebugCommand(new Environment($loader), $projectDir, $bundleMetadata, $defaultPath, $rootDir));
250296
$command = $application->find('debug:twig');
251297

252298
return new CommandTester($command);