Skip to content

Commit 1c06b83

Browse files
bug #36914 Parse and render anonymous classes correctly on php 8 (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- Parse and render anonymous classes correctly on php 8 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #36872 | License | MIT | Doc PR | N/A The format of the value that `get_class()` returns for anonymous classes has changed in php 8. This PR attempts to detect both formats, with the help of the PHP80 polyfill where possible. Commits ------- 9d702fd94b Parse and render anonymous classes correctly on php 8
2 parents 79957a4 + 195731b commit 1c06b83

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Kernel.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,7 @@ public function getBundles()
228228
public function getBundle($name)
229229
{
230230
if (!isset($this->bundles[$name])) {
231-
$class = static::class;
232-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
233-
234-
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the "registerBundles()" method of your "%s.php" file?', $name, $class));
231+
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the "registerBundles()" method of your "%s.php" file?', $name, get_debug_type($this)));
235232
}
236233

237234
return $this->bundles[$name];
@@ -474,7 +471,7 @@ protected function build(ContainerBuilder $container)
474471
protected function getContainerClass()
475472
{
476473
$class = static::class;
477-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
474+
$class = false !== strpos($class, "@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
478475
$class = $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
479476

480477
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {

Tests/KernelTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,27 @@ public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
640640
$this->assertGreaterThan($preReBoot, $kernel->getStartTime());
641641
}
642642

643+
public function testAnonymousKernelGeneratesValidContainerClass(): void
644+
{
645+
$kernel = new class('test', true) extends Kernel {
646+
public function registerBundles(): iterable
647+
{
648+
return [];
649+
}
650+
651+
public function registerContainerConfiguration(LoaderInterface $loader): void
652+
{
653+
}
654+
655+
public function getContainerClass(): string
656+
{
657+
return parent::getContainerClass();
658+
}
659+
};
660+
661+
$this->assertRegExp('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*TestDebugContainer$/', $kernel->getContainerClass());
662+
}
663+
643664
/**
644665
* Returns a mock for the BundleInterface.
645666
*/

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/http-foundation": "^4.4|^5.0",
2323
"symfony/polyfill-ctype": "^1.8",
2424
"symfony/polyfill-php73": "^1.9",
25+
"symfony/polyfill-php80": "^1.15",
2526
"psr/log": "~1.0"
2627
},
2728
"require-dev": {

0 commit comments

Comments
 (0)