Skip to content

Commit 2a229a1

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

File tree

4 files changed

+220
-59
lines changed

4 files changed

+220
-59
lines changed

routing/page_loader.php

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,39 @@
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 ReflectionMethod;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
1716

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

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)
30+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
31+
if ($method->hasReturnType())
5332
{
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;
33+
class page_loader extends page_loader_phpbb4 {}
6934
}
70-
71-
/**
72-
* {@inheritdoc}
73-
*
74-
* @api
75-
*/
76-
public function supports($resource, $type = null)
35+
else
7736
{
78-
return $type === 'phpbb_pages_route';
37+
class page_loader extends page_loader_phpbb3 {}
7938
}
39+
// phpcs:enable PSR1.Classes.ClassDeclaration.MultipleClasses
8040
}
41+
// phpcs:enable PSR1.Files.SideEffects

routing/page_loader_core.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
/**
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+
* @api
46+
*/
47+
public function loadRoutes(): RouteCollection
48+
{
49+
$collection = new RouteCollection();
50+
51+
$sql = 'SELECT page_id, page_route
52+
FROM ' . $this->pages_table;
53+
$result = $this->db->sql_query($sql);
54+
while ($row = $this->db->sql_fetchrow($result))
55+
{
56+
$route = new Route('/' . $row['page_route']);
57+
$route->setDefault('_controller', 'phpbb.pages.controller:display');
58+
$route->setDefault('route', $row['page_route']);
59+
$collection->add('phpbb_pages_dynamic_route_' . $row['page_id'], $route);
60+
}
61+
$this->db->sql_freeresult();
62+
63+
return $collection;
64+
}
65+
66+
/**
67+
* Checks if the loader supports the specified type.
68+
*
69+
* @param mixed $type The type to check support for.
70+
* @return bool True if the type is supported, false otherwise.
71+
*/
72+
public function supportsType($type): bool
73+
{
74+
return $type === 'phpbb_pages_route';
75+
}
76+
}

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) 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->loadRoutes();
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->supportsType($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) 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->loadRoutes();
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->supportsType($type);
59+
}
60+
}

0 commit comments

Comments
 (0)