Skip to content

Commit b2c11e3

Browse files
committed
Use a compatibility adapter for page_loader class
1 parent be9d879 commit b2c11e3

File tree

4 files changed

+159
-59
lines changed

4 files changed

+159
-59
lines changed

routing/page_loader.php

Lines changed: 19 additions & 59 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) 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\Config\Loader\LoaderInterface;
1715

1816
/**
19-
* Loads routes defined in Page's database.
17+
* This code determines which page_loader class to use based on the phpBB version.
18+
* It checks if the Symfony LoaderInterface::load() method has a return type,
19+
* which indicates Symfony 7+ (phpBB4), otherwise falls back to phpBB3 compatibility.
20+
* The conditional is mandatory to ensure we only define the class if it does not
21+
* already exist in this request.
22+
*
23+
* @noinspection PhpMultipleClassDeclarationsInspection
2024
*/
21-
class page_loader extends Loader
25+
if (!class_exists(page_loader::class, false))
2226
{
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)
37-
{
38-
$this->db = $db;
39-
$this->pages_table = $pages_table;
40-
}
27+
$method = new \ReflectionMethod(LoaderInterface::class, 'load');
4128

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)
29+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
30+
if ($method->hasReturnType())
5331
{
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;
32+
class page_loader extends page_loader_phpbb4 {}
6933
}
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
*
4+
* Pages extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2015 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+
public function __construct(driver_interface $db, string $pages_table)
29+
{
30+
$this->db = $db;
31+
$this->pages_table = $pages_table;
32+
}
33+
34+
public function loadRoutes(): RouteCollection
35+
{
36+
$collection = new RouteCollection();
37+
38+
$sql = 'SELECT page_id, page_route
39+
FROM ' . $this->pages_table;
40+
$result = $this->db->sql_query($sql);
41+
while ($row = $this->db->sql_fetchrow($result))
42+
{
43+
$route = new Route('/' . $row['page_route']);
44+
$route->setDefault('_controller', 'phpbb.pages.controller:display');
45+
$route->setDefault('route', $row['page_route']);
46+
$collection->add('phpbb_pages_dynamic_route_' . $row['page_id'], $route);
47+
}
48+
$this->db->sql_freeresult();
49+
50+
return $collection;
51+
}
52+
53+
public function supportsType($type): bool
54+
{
55+
return $type === 'phpbb_pages_route';
56+
}
57+
}

routing/page_loader_phpbb3.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\routing;
12+
13+
use InvalidArgumentException;
14+
use phpbb\db\driver\driver_interface;
15+
use Symfony\Component\Config\Loader\Loader;
16+
17+
/**
18+
* phpBB 3 and Symfony 3 through 6 adapter for page loader.
19+
*/
20+
class page_loader_phpbb3 extends Loader
21+
{
22+
/** @var page_loader_core */
23+
protected $core;
24+
25+
public function __construct(driver_interface $db, string $pages_table)
26+
{
27+
$this->core = new page_loader_core($db, $pages_table);
28+
}
29+
30+
public function load($resource, $type = null)
31+
{
32+
if (!is_string($type) && $type !== null)
33+
{
34+
throw new InvalidArgumentException('Type must be string or null');
35+
}
36+
return $this->core->loadRoutes();
37+
}
38+
39+
public function supports($resource, $type = null): bool
40+
{
41+
return $this->core->supportsType($type);
42+
}
43+
}

routing/page_loader_phpbb4.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\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 $core;
24+
25+
public function __construct(driver_interface $db, string $pages_table)
26+
{
27+
$this->core = new page_loader_core($db, $pages_table);
28+
parent::__construct();
29+
}
30+
31+
public function load($resource, ?string $type = null): RouteCollection
32+
{
33+
return $this->core->loadRoutes();
34+
}
35+
36+
public function supports($resource, $type = null): bool
37+
{
38+
return $this->core->supportsType($type);
39+
}
40+
}

0 commit comments

Comments
 (0)