Skip to content

Commit f66d5f6

Browse files
Merge branch '4.2'
* 4.2: [TwigBridge] Deprecating legacy Twig paths in DebugCommand and simplifications [Cache] Fixed Memcached adapter doClear()to call flush() Fixes sprintf(): Too few arguments in Translator fix TransChoiceTokenParser deprecation message [DoctrineBridge] Conflict with Messenger <4.2 [Contracts] extract LocaleAwareInterface out of TranslatorInterface
2 parents cc02ea9 + 2e928d6 commit f66d5f6

File tree

8 files changed

+79
-67
lines changed

8 files changed

+79
-67
lines changed

Command/DebugCommand.php

Lines changed: 29 additions & 50 deletions
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-
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

Tests/Command/DebugCommandTest.php

Lines changed: 48 additions & 2 deletions
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);

Tests/Extension/Fixtures/StubTranslator.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,4 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
1919
{
2020
return '[trans]'.$id.'[/trans]';
2121
}
22-
23-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
24-
{
25-
return '[trans]'.$id.'[/trans]';
26-
}
27-
28-
public function setLocale($locale)
29-
{
30-
}
31-
32-
public function getLocale()
33-
{
34-
}
3522
}

Tests/Fixtures/Resources/BarBundle/views/base.html.twig

Whitespace-only changes.

Tests/Fixtures/templates/bundles/UnknownBundle/base.html.twig

Whitespace-only changes.

Tests/Fixtures/templates/bundles/WebProfileBundle/Profiler/base.html.twig

Whitespace-only changes.

TokenParser/TransChoiceTokenParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function parse(Token $token)
4040
$lineno = $token->getLine();
4141
$stream = $this->parser->getStream();
4242

43-
@trigger_error(sprintf('The "transchoice" tag is deprecated since Symfony 4.2, use the "trans" one instead with a "%count%" parameter in %s line %d.', $stream->getSourceContext()->getName(), $lineno), E_USER_DEPRECATED);
43+
@trigger_error(sprintf('The "transchoice" tag is deprecated since Symfony 4.2, use the "trans" one instead with a "%%count%%" parameter in %s line %d.', $stream->getSourceContext()->getName(), $lineno), E_USER_DEPRECATED);
4444

4545
$vars = new ArrayExpression(array(), $lineno);
4646

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.0.2",
2121
"twig/twig": "^1.35|^2.4.4"
2222
},
2323
"require-dev": {

0 commit comments

Comments
 (0)