Skip to content

Commit fe36375

Browse files
committed
[ticket/11700] Fix extension loading with namespaces
class loader now expects all classes to be prefixed with a backslash when resolving paths PHPBB3-11700
1 parent f205c4f commit fe36375

23 files changed

Lines changed: 72 additions & 46 deletions

phpBB/phpbb/class_loader.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ class class_loader
5555
* @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface.
5656
*/
5757
public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null)
58-
{
58+
{
59+
if ($namespace[0] !== '\\')
60+
{
61+
$namespace = '\\' . $namespace;
62+
}
63+
5964
$this->namespace = $namespace;
6065
$this->path = $path;
6166
$this->php_ext = $php_ext;
@@ -105,7 +110,8 @@ public function unregister()
105110
* Resolves a phpBB class name to a relative path which can be included.
106111
*
107112
* @param string $class The class name to resolve, must be in the
108-
* namespace the loader was constructed with
113+
* namespace the loader was constructed with.
114+
* Has to begin with \
109115
* @return string|bool A relative path to the file containing the
110116
* class or false if looking it up failed.
111117
*/
@@ -144,6 +150,7 @@ public function resolve_path($class)
144150
*/
145151
public function load_class($class)
146152
{
153+
$class = '\\' . $class;
147154
if (substr($class, 0, strlen($this->namespace)) === $this->namespace)
148155
{
149156
$path = $this->resolve_path($class);

phpBB/phpbb/controller/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function render($template_file, $page_title = '', $status_code = 200)
9191

9292
page_footer(true, false, false);
9393

94-
return new \Response($this->template->assign_display('body'), $status_code);
94+
return new Response($this->template->assign_display('body'), $status_code);
9595
}
9696

9797
/**

phpBB/phpbb/controller/resolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public function getController(Request $request)
9595
* the style paths for the extension (the ext author can change them
9696
* if necessary).
9797
*/
98-
$controller_dir = explode('_', get_class($controller_object));
98+
$controller_dir = explode('\\', get_class($controller_object));
9999

100-
// 0 phpbb, 1 ext, 2 vendor, 3 extension name, ...
101-
if (!is_null($this->template) && isset($controller_dir[3]) && $controller_dir[1] === 'ext')
100+
// 0 vendor, 1 extension name, ...
101+
if (!is_null($this->template) && isset($controller_dir[1]))
102102
{
103-
$controller_style_dir = 'ext/' . $controller_dir[2] . '/' . $controller_dir[3] . '/styles';
103+
$controller_style_dir = 'ext/' . $controller_dir[0] . '/' . $controller_dir[1] . '/styles';
104104

105105
if (is_dir($controller_style_dir))
106106
{

phpBB/phpbb/event/kernel_exception_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(\phpbb\template\template $template, \phpbb\user $use
5555
* @param GetResponseForExceptionEvent $event
5656
* @return null
5757
*/
58-
public function on_kernel_exception(\GetResponseForExceptionEvent $event)
58+
public function on_kernel_exception(GetResponseForExceptionEvent $event)
5959
{
6060
page_header($this->user->lang('INFORMATION'));
6161

@@ -74,7 +74,7 @@ public function on_kernel_exception(\GetResponseForExceptionEvent $event)
7474

7575

7676
$status_code = $exception instanceof HttpException ? $exception->getStatusCode() : 500;
77-
$response = new \Response($this->template->assign_display('body'), $status_code);
77+
$response = new Response($this->template->assign_display('body'), $status_code);
7878
$event->setResponse($response);
7979
}
8080

phpBB/phpbb/event/kernel_request_subscriber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public function __construct(\phpbb\extension\finder $finder, $root_path, $php_ex
6565
* @param GetResponseEvent $event
6666
* @return null
6767
*/
68-
public function on_kernel_request(\GetResponseEvent $event)
68+
public function on_kernel_request(GetResponseEvent $event)
6969
{
7070
$request = $event->getRequest();
71-
$context = new \RequestContext();
71+
$context = new RequestContext();
7272
$context->fromRequest($request);
7373

7474
$matcher = phpbb_get_url_matcher($this->finder, $context, $this->root_path, $this->php_ext);
75-
$router_listener = new \RouterListener($matcher, $context);
75+
$router_listener = new RouterListener($matcher, $context);
7676
$router_listener->onKernelRequest($event);
7777
}
7878

phpBB/phpbb/event/kernel_terminate_subscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class kernel_terminate_subscriber implements EventSubscriberInterface
3131
* @param PostResponseEvent $event
3232
* @return null
3333
*/
34-
public function on_kernel_terminate(\PostResponseEvent $event)
34+
public function on_kernel_terminate(PostResponseEvent $event)
3535
{
3636
exit_handler();
3737
}

tests/acp_board/auth_provider/valid.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*
88
*/
99

10-
class phpbb_auth_provider_acp_board_valid extends \phpbb\auth\provider\base
10+
namespace phpbb\auth\provider\acp;
11+
12+
class board_valid extends \phpbb\auth\provider\base
1113
{
1214
public function login($username, $password)
1315
{

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
$phpbb_class_loader_mock = new \phpbb\class_loader('phpbb_mock_', $phpbb_root_path . '../tests/mock/', "php");
2020
$phpbb_class_loader_mock->register();
21-
$phpbb_class_loader_ext = new \phpbb\class_loader('phpbb_ext_', $phpbb_root_path . 'ext/', "php");
21+
$phpbb_class_loader_ext = new \phpbb\class_loader('\\', $phpbb_root_path . 'ext/', "php");
2222
$phpbb_class_loader_ext->register();
2323
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', $phpbb_root_path . 'phpbb/', "php");
2424
$phpbb_class_loader->register();

tests/class_loader/class_loader_test.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,31 @@ public function test_resolve_path()
3636

3737
$this->assertEquals(
3838
$prefix . 'class_name.php',
39-
$class_loader->resolve_path('phpbb\\class_name'),
39+
$class_loader->resolve_path('\\phpbb\\class_name'),
4040
'Top level class'
4141
);
4242
$this->assertEquals(
4343
$prefix . 'dir/class_name.php',
44-
$class_loader->resolve_path('phpbb\\dir\\class_name'),
44+
$class_loader->resolve_path('\\phpbb\\dir\\class_name'),
4545
'Class in a directory'
4646
);
4747
$this->assertEquals(
4848
$prefix . 'dir/subdir/class_name.php',
49-
$class_loader->resolve_path('phpbb\\dir\\subdir\\class_name'),
49+
$class_loader->resolve_path('\\phpbb\\dir\\subdir\\class_name'),
5050
'Class in a sub-directory'
5151
);
5252
$this->assertEquals(
5353
$prefix . 'dir2/dir2.php',
54-
$class_loader->resolve_path('phpbb\\dir2\\dir2'),
54+
$class_loader->resolve_path('\\phpbb\\dir2\\dir2'),
5555
'Class with name of dir within dir'
5656
);
5757
}
5858

5959
public function test_resolve_cached()
6060
{
6161
$cache_map = array(
62-
'class_loader_phpbb__' => array('phpbb\\a\\cached_name' => 'a/cached_name'),
63-
'class_loader___' => array('phpbb\\ext\\foo' => 'foo'),
62+
'class_loader___phpbb__' => array('\\phpbb\\a\\cached_name' => 'a/cached_name'),
63+
'class_loader___' => array('\\phpbb\\ext\\foo' => 'foo'),
6464
);
6565
$cache = new phpbb_mock_cache($cache_map);
6666

@@ -72,26 +72,26 @@ public function test_resolve_cached()
7272

7373
$this->assertEquals(
7474
$prefix . 'dir/class_name.php',
75-
$class_loader->resolve_path('phpbb\\dir\\class_name'),
75+
$class_loader->resolve_path('\\phpbb\\dir\\class_name'),
7676
'Class in a directory'
7777
);
7878

79-
$this->assertFalse($class_loader->resolve_path('phpbb\\ext\\foo'));
80-
$this->assertFalse($class_loader_ext->resolve_path('phpbb\\a\\cached_name'));
79+
$this->assertFalse($class_loader->resolve_path('\\phpbb\\ext\\foo'));
80+
$this->assertFalse($class_loader_ext->resolve_path('\\phpbb\\a\\cached_name'));
8181

8282
$this->assertEquals(
8383
$prefix . 'a/cached_name.php',
84-
$class_loader->resolve_path('phpbb\\a\\cached_name'),
84+
$class_loader->resolve_path('\\phpbb\\a\\cached_name'),
8585
'Cached class found'
8686
);
8787

8888
$this->assertEquals(
8989
$prefix . 'foo.php',
90-
$class_loader_ext->resolve_path('phpbb\\ext\\foo'),
90+
$class_loader_ext->resolve_path('\\phpbb\\ext\\foo'),
9191
'Cached class found in alternative loader'
9292
);
9393

94-
$cache_map['class_loader_phpbb__']['phpbb\\dir\\class_name'] = 'dir/class_name';
94+
$cache_map['class_loader___phpbb__']['\\phpbb\\dir\\class_name'] = 'dir/class_name';
9595
$cache->check($this, $cache_map);
9696
}
9797
}

tests/controller/controller_test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function test_controller_resolver()
5353

5454
// Autoloading classes within the tests folder does not work
5555
// so I'll include them manually.
56-
if (!class_exists('phpbb_ext_foo_controller'))
56+
if (!class_exists('foo\\controller'))
5757
{
5858
include(__DIR__.'/ext/foo/controller.php');
5959
}
60-
if (!class_exists('phpbb_controller_foo'))
60+
if (!class_exists('phpbb\\controller\\foo'))
6161
{
6262
include(__DIR__.'/phpbb/controller/foo.php');
6363
}
@@ -66,11 +66,11 @@ public function test_controller_resolver()
6666
$symfony_request = new Request();
6767
$symfony_request->attributes->set('_controller', 'foo.controller:handle');
6868

69-
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_ext_foo_controller, 'handle'));
69+
$this->assertEquals($resolver->getController($symfony_request), array(new foo\controller, 'handle'));
7070

7171
$symfony_request = new Request();
7272
$symfony_request->attributes->set('_controller', 'core_foo.controller:bar');
7373

74-
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_controller_foo, 'bar'));
74+
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb\controller\foo, 'bar'));
7575
}
7676
}

0 commit comments

Comments
 (0)