Skip to content

Commit 529bbdb

Browse files
authored
Merge pull request #190 from iMattPro/compatibility
Symfony 7 and phpBB 4 compatibility
2 parents be9d879 + 5029457 commit 529bbdb

10 files changed

Lines changed: 498 additions & 60 deletions

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<directory suffix=".php">./language/</directory>
2929
<directory suffix=".php">./migrations/</directory>
3030
<directory suffix=".php">./tests/</directory>
31+
<file>./routing/page_loader_phpbb4.php</file>
3132
</exclude>
3233
</whitelist>
3334
</filter>

routing/page_loader.php

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,38 @@
33
*
44
* Pages extension for the phpBB Forum Software package.
55
*
6-
* @copyright (c) 2015 phpBB Limited <https://www.phpbb.com>
6+
* @copyright (c) 2015, 2025 phpBB Limited <https://www.phpbb.com>
77
* @license GNU General Public License, version 2 (GPL-2.0)
88
*
99
*/
10+
// phpcs:disable PSR1.Files.SideEffects
1011

1112
namespace phpbb\pages\routing;
1213

13-
use phpbb\db\driver\driver_interface;
14-
use Symfony\Component\Config\Loader\Loader;
15-
use Symfony\Component\Routing\Route;
16-
use Symfony\Component\Routing\RouteCollection;
14+
use Symfony\Component\HttpKernel\Kernel;
15+
16+
if (!defined('IN_PHPBB'))
17+
{
18+
exit;
19+
}
1720

1821
/**
19-
* Loads routes defined in Page's database.
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.
24+
*
25+
* @noinspection PhpMultipleClassDeclarationsInspection
2026
*/
21-
class page_loader extends Loader
27+
if (!class_exists(page_loader::class, false))
2228
{
23-
/** @var driver_interface */
24-
protected $db;
25-
26-
/** @var string */
27-
protected $pages_table;
28-
29-
/**
30-
* Constructor
31-
*
32-
* @param driver_interface $db Database connection
33-
* @param string $pages_table Table name
34-
* @access public
35-
*/
36-
public function __construct(driver_interface $db, $pages_table)
29+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
30+
if (version_compare(Kernel::VERSION, '7.0.0', '>='))
3731
{
38-
$this->db = $db;
39-
$this->pages_table = $pages_table;
32+
class page_loader extends page_loader_phpbb4 {}
4033
}
41-
42-
/**
43-
* Loads routes defined in Page's database.
44-
*
45-
* @param string $resource Resource (not used, but required by parent interface)
46-
* @param string|null $type The resource type
47-
*
48-
* @return RouteCollection A RouteCollection instance
49-
*
50-
* @api
51-
*/
52-
public function load($resource, $type = null)
53-
{
54-
$collection = new RouteCollection();
55-
56-
$sql = 'SELECT page_id, page_route
57-
FROM ' . $this->pages_table;
58-
$result = $this->db->sql_query($sql);
59-
while ($row = $this->db->sql_fetchrow($result))
60-
{
61-
$route = new Route('/' . $row['page_route']);
62-
$route->setDefault('_controller', 'phpbb.pages.controller:display');
63-
$route->setDefault('route', $row['page_route']);
64-
$collection->add('phpbb_pages_dynamic_route_' . $row['page_id'], $route);
65-
}
66-
$this->db->sql_freeresult();
67-
68-
return $collection;
69-
}
70-
71-
/**
72-
* {@inheritdoc}
73-
*
74-
* @api
75-
*/
76-
public function supports($resource, $type = null)
34+
else
7735
{
78-
return $type === 'phpbb_pages_route';
36+
class page_loader extends page_loader_phpbb3 {}
7937
}
38+
// phpcs:enable PSR1.Classes.ClassDeclaration.MultipleClasses
8039
}
40+
// phpcs:enable PSR1.Files.SideEffects

routing/page_loader_core.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* Pages extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2015, 2025 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\pages\routing;
12+
13+
use phpbb\db\driver\driver_interface;
14+
use Symfony\Component\Routing\Route;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* Core loader logic for loading routes from the database.
19+
*/
20+
class page_loader_core
21+
{
22+
/** @var driver_interface */
23+
protected $db;
24+
25+
/** @var string */
26+
protected $pages_table;
27+
28+
/**
29+
* Constructor for the page_loader_core class.
30+
*
31+
* @param driver_interface $db The database driver instance.
32+
* @param string $pages_table The name of the pages table in the database.
33+
*/
34+
public function __construct(driver_interface $db, string $pages_table)
35+
{
36+
$this->db = $db;
37+
$this->pages_table = $pages_table;
38+
}
39+
40+
/**
41+
* Loads routes defined in Page's database.
42+
*
43+
* @return RouteCollection A RouteCollection instance
44+
*/
45+
public function load_routes(): RouteCollection
46+
{
47+
$collection = new RouteCollection();
48+
49+
$sql = 'SELECT page_id, page_route
50+
FROM ' . $this->pages_table;
51+
$result = $this->db->sql_query($sql);
52+
while ($row = $this->db->sql_fetchrow($result))
53+
{
54+
$route = new Route('/' . $row['page_route']);
55+
$route->setDefault('_controller', 'phpbb.pages.controller:display');
56+
$route->setDefault('route', $row['page_route']);
57+
$collection->add('phpbb_pages_dynamic_route_' . $row['page_id'], $route);
58+
}
59+
$this->db->sql_freeresult($result);
60+
61+
return $collection;
62+
}
63+
64+
/**
65+
* Checks if the loader supports the specified type.
66+
*
67+
* @param mixed $type The type to check support for.
68+
* @return bool True if the type is supported, false otherwise.
69+
*/
70+
public function supports_type($type): bool
71+
{
72+
return $type === 'phpbb_pages_route';
73+
}
74+
}

routing/page_loader_phpbb3.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
*
4+
* Pages extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2015, 2025 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\pages\routing;
12+
13+
use InvalidArgumentException;
14+
use phpbb\db\driver\driver_interface;
15+
use Symfony\Component\Config\Loader\Loader;
16+
use Symfony\Component\Routing\RouteCollection;
17+
18+
/**
19+
* phpBB 3 and Symfony 3 through 6 adapter for page loader.
20+
*/
21+
class page_loader_phpbb3 extends Loader
22+
{
23+
/** @var page_loader_core */
24+
protected $core;
25+
26+
/**
27+
* Constructor for the page_loader_phpbb3 class.
28+
*
29+
* @param driver_interface $db The database driver instance.
30+
* @param string $pages_table The name of the pages table in the database.
31+
*/
32+
public function __construct(driver_interface $db, string $pages_table)
33+
{
34+
$this->core = new page_loader_core($db, $pages_table);
35+
}
36+
37+
/**
38+
* Loads routes from the database.
39+
*
40+
* @param mixed $resource The resource to load routes from.
41+
* @param string|null $type The type of the resource, or null if not specified.
42+
* @return RouteCollection The collection of loaded routes.
43+
*/
44+
public function load($resource, $type = null)
45+
{
46+
if (!is_string($type) && $type !== null)
47+
{
48+
throw new InvalidArgumentException('Type must be string or null');
49+
}
50+
return $this->core->load_routes();
51+
}
52+
53+
/**
54+
* Determines if the given resource is supported based on its type.
55+
*
56+
* @param mixed $resource The resource to be checked.
57+
* @param string|null $type The type of the resource, or null for default processing.
58+
* @return bool True if the resource type is supported, false otherwise.
59+
*/
60+
public function supports($resource, $type = null): bool
61+
{
62+
return $this->core->supports_type($type);
63+
}
64+
}

routing/page_loader_phpbb4.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
*
4+
* Pages extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2015, 2025 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\pages\routing;
12+
13+
use phpbb\db\driver\driver_interface;
14+
use Symfony\Component\Config\Loader\Loader;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* phpBB 4 and Symfony 7 adapter for page loader.
19+
*/
20+
class page_loader_phpbb4 extends Loader
21+
{
22+
/** @var page_loader_core */
23+
protected page_loader_core $core;
24+
25+
/**
26+
* Constructor for the page_loader_phpbb4 class.
27+
*
28+
* @param driver_interface $db The database driver instance.
29+
* @param string $pages_table The name of the pages table in the database.
30+
*/
31+
public function __construct(driver_interface $db, string $pages_table)
32+
{
33+
$this->core = new page_loader_core($db, $pages_table);
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* Loads a set of routes from a specified resource.
39+
*
40+
* @param mixed $resource The resource to load routes from.
41+
* @param string|null $type The type of the resource, or null if not specified.
42+
* @return RouteCollection The collection of loaded routes.
43+
*/
44+
public function load(mixed $resource, ?string $type = null): RouteCollection
45+
{
46+
return $this->core->load_routes();
47+
}
48+
49+
/**
50+
* Checks if the loader supports the specified resource and type.
51+
*
52+
* @param mixed $resource The resource to check support for.
53+
* @param string|null $type The type of the resource, or null if not specified.
54+
* @return bool True if the loader supports the resource and type, false otherwise.
55+
*/
56+
public function supports(mixed $resource, ?string $type = null): bool
57+
{
58+
return $this->core->supports_type($type);
59+
}
60+
}

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
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
*
4+
* Pages extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2025 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\pages\tests\routing;
12+
13+
class page_loader_core_test extends \phpbb_database_test_case
14+
{
15+
protected static function setup_extensions()
16+
{
17+
return array('phpbb/pages');
18+
}
19+
20+
/** @var \phpbb\db\driver\driver_interface */
21+
protected $db;
22+
23+
/** @var \phpbb\pages\routing\page_loader_core */
24+
protected $loader;
25+
26+
public function getDataSet()
27+
{
28+
return $this->createXMLDataSet(__DIR__ . '/../operators/fixtures/page.xml');
29+
}
30+
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
$this->db = $this->new_dbal();
35+
$this->loader = new \phpbb\pages\routing\page_loader_core($this->db, 'phpbb_pages');
36+
}
37+
38+
public function test_load_routes_returns_collection()
39+
{
40+
$collection = $this->loader->load_routes();
41+
self::assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
42+
}
43+
44+
public static function route_data()
45+
{
46+
return array(
47+
array(1, '/page_1'),
48+
array(2, '/page_2'),
49+
array(3, '/page_3'),
50+
array(4, '/page_4'),
51+
);
52+
}
53+
54+
/**
55+
* @dataProvider route_data
56+
*/
57+
public function test_routes_have_correct_paths($id, $expected)
58+
{
59+
$collection = $this->loader->load_routes();
60+
$route = $collection->get('phpbb_pages_dynamic_route_' . $id);
61+
self::assertInstanceOf('Symfony\Component\Routing\Route', $route);
62+
self::assertSame($expected, $route->getPath());
63+
}
64+
65+
public function test_supports_type()
66+
{
67+
self::assertTrue($this->loader->supports_type('phpbb_pages_route'));
68+
self::assertFalse($this->loader->supports_type('other_type'));
69+
self::assertFalse($this->loader->supports_type(null));
70+
}
71+
}

0 commit comments

Comments
 (0)