Skip to content

Commit 5029457

Browse files
committed
Load page route loader by checking Symfony version
1 parent 787b886 commit 5029457

5 files changed

Lines changed: 25 additions & 29 deletions

File tree

routing/page_loader.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,23 @@
1111

1212
namespace phpbb\pages\routing;
1313

14-
use ReflectionMethod;
15-
use Symfony\Component\Config\Loader\LoaderInterface;
14+
use Symfony\Component\HttpKernel\Kernel;
1615

1716
if (!defined('IN_PHPBB'))
1817
{
1918
exit;
2019
}
2120

2221
/**
23-
* This code determines which page_loader class to use based on the phpBB version.
24-
* It checks if the Symfony LoaderInterface::load() method has a return type,
25-
* which indicates Symfony 7+ (phpBB4), otherwise falls back to phpBB3 compatibility.
26-
* The conditional is mandatory to ensure we only define the class if it does not
27-
* already exist in this request.
22+
* This code determines which page_loader class to use based on the Symfony version.
23+
* Symfony 7+ (phpBB4) uses page_loader_phpbb4, otherwise page_loader_phpbb3.
2824
*
2925
* @noinspection PhpMultipleClassDeclarationsInspection
3026
*/
3127
if (!class_exists(page_loader::class, false))
3228
{
33-
$method = new ReflectionMethod(LoaderInterface::class, 'load');
34-
3529
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
36-
if ($method->hasReturnType())
30+
if (version_compare(Kernel::VERSION, '7.0.0', '>='))
3731
{
3832
class page_loader extends page_loader_phpbb4 {}
3933
}

tests/operators/page_routing_loader_test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,16 @@ public function test_page_loader($id, $expected)
9696
// Assert the route contains the expected path
9797
self::assertSame($expected, $route->getPath());
9898
}
99+
100+
public function test_loader_is_correct_adapter()
101+
{
102+
if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '7.0.0', '>='))
103+
{
104+
self::assertInstanceOf(\phpbb\pages\routing\page_loader_phpbb4::class, $this->loader);
105+
}
106+
else
107+
{
108+
self::assertInstanceOf(\phpbb\pages\routing\page_loader_phpbb3::class, $this->loader);
109+
}
110+
}
99111
}

tests/routing/page_loader_phpbb3_test.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace phpbb\pages\tests\routing;
1212

13-
use ReflectionMethod;
14-
use Symfony\Component\Config\Loader\LoaderInterface;
13+
use Symfony\Component\HttpKernel\Kernel;
1514

1615
class page_loader_phpbb3_test extends \phpbb_database_test_case
1716
{
@@ -35,8 +34,7 @@ protected function setUp(): void
3534
{
3635
parent::setUp();
3736

38-
$method = new ReflectionMethod(LoaderInterface::class, 'load');
39-
if ($method->hasReturnType())
37+
if (version_compare(Kernel::VERSION, '7.0.0', '>='))
4038
{
4139
self::markTestSkipped('phpbb3 adapter tests only run on Symfony 3-6 (phpBB3)');
4240
}

tests/routing/page_loader_phpbb4_test.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace phpbb\pages\tests\routing;
1212

13-
use ReflectionMethod;
14-
use Symfony\Component\Config\Loader\LoaderInterface;
13+
use Symfony\Component\HttpKernel\Kernel;
1514

1615
class page_loader_phpbb4_test extends \phpbb_database_test_case
1716
{
@@ -35,8 +34,7 @@ protected function setUp(): void
3534
{
3635
parent::setUp();
3736

38-
$method = new ReflectionMethod(LoaderInterface::class, 'load');
39-
if (!$method->hasReturnType())
37+
if (version_compare(Kernel::VERSION, '7.0.0', '<'))
4038
{
4139
self::markTestSkipped('phpbb4 adapter tests only run on Symfony 7+ (phpBB4)');
4240
}

tests/routing/page_loader_version_detection_test.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace phpbb\pages\tests\routing;
1212

13-
use ReflectionMethod;
14-
use Symfony\Component\Config\Loader\LoaderInterface;
13+
use Symfony\Component\HttpKernel\Kernel;
1514

1615
class page_loader_version_detection_test extends \phpbb_test_case
1716
{
@@ -27,33 +26,28 @@ public function test_page_loader_class_exists()
2726

2827
public function test_page_loader_extends_correct_adapter()
2928
{
30-
$method = new ReflectionMethod(LoaderInterface::class, 'load');
31-
32-
if ($method->hasReturnType())
29+
if (version_compare(Kernel::VERSION, '7.0.0', '>='))
3330
{
3431
self::assertInstanceOf(\phpbb\pages\routing\page_loader_phpbb4::class,
3532
new \phpbb\pages\routing\page_loader($this->createMock(\phpbb\db\driver\driver_interface::class), 'test_table'),
36-
'page_loader should extend page_loader_phpbb4 when LoaderInterface::load() has return type (Symfony 7/phpBB4)'
33+
'page_loader should extend page_loader_phpbb4 when Symfony >= 7.0 (phpBB4)'
3734
);
3835
}
3936
else
4037
{
4138
self::assertInstanceOf(\phpbb\pages\routing\page_loader_phpbb3::class,
4239
new \phpbb\pages\routing\page_loader($this->createMock(\phpbb\db\driver\driver_interface::class), 'test_table'),
43-
'page_loader should extend page_loader_phpbb3 when LoaderInterface::load() has no return type (Symfony 3-6/phpBB3)'
40+
'page_loader should extend page_loader_phpbb3 when Symfony < 7.0 (phpBB3)'
4441
);
4542
}
4643
}
4744

4845
public function test_version_detection_logic()
4946
{
50-
$method = new ReflectionMethod(LoaderInterface::class, 'load');
51-
$hasReturnType = $method->hasReturnType();
52-
5347
$reflection = new \ReflectionClass(\phpbb\pages\routing\page_loader::class);
5448
$parent = $reflection->getParentClass();
5549

56-
if ($hasReturnType)
50+
if (version_compare(Kernel::VERSION, '7.0.0', '>='))
5751
{
5852
self::assertSame(\phpbb\pages\routing\page_loader_phpbb4::class, $parent->getName(),
5953
'Symfony 7+ detected: page_loader must extend page_loader_phpbb4'

0 commit comments

Comments
 (0)