[ticket/11700] Fix extension loading with namespaces

class loader now expects all classes to be prefixed with a backslash
when resolving paths

PHPBB3-11700
This commit is contained in:
Nils Adermann 2013-09-19 18:29:08 +02:00
parent f205c4fad4
commit fe36375a36
23 changed files with 72 additions and 46 deletions

View file

@ -55,7 +55,12 @@ class class_loader
* @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface. * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface.
*/ */
public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null) public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null)
{ {
if ($namespace[0] !== '\\')
{
$namespace = '\\' . $namespace;
}
$this->namespace = $namespace; $this->namespace = $namespace;
$this->path = $path; $this->path = $path;
$this->php_ext = $php_ext; $this->php_ext = $php_ext;
@ -105,7 +110,8 @@ class class_loader
* Resolves a phpBB class name to a relative path which can be included. * Resolves a phpBB class name to a relative path which can be included.
* *
* @param string $class The class name to resolve, must be in the * @param string $class The class name to resolve, must be in the
* namespace the loader was constructed with * namespace the loader was constructed with.
* Has to begin with \
* @return string|bool A relative path to the file containing the * @return string|bool A relative path to the file containing the
* class or false if looking it up failed. * class or false if looking it up failed.
*/ */
@ -144,6 +150,7 @@ class class_loader
*/ */
public function load_class($class) public function load_class($class)
{ {
$class = '\\' . $class;
if (substr($class, 0, strlen($this->namespace)) === $this->namespace) if (substr($class, 0, strlen($this->namespace)) === $this->namespace)
{ {
$path = $this->resolve_path($class); $path = $this->resolve_path($class);

View file

@ -91,7 +91,7 @@ class helper
page_footer(true, false, false); page_footer(true, false, false);
return new \Response($this->template->assign_display('body'), $status_code); return new Response($this->template->assign_display('body'), $status_code);
} }
/** /**

View file

@ -95,12 +95,12 @@ class resolver implements ControllerResolverInterface
* the style paths for the extension (the ext author can change them * the style paths for the extension (the ext author can change them
* if necessary). * if necessary).
*/ */
$controller_dir = explode('_', get_class($controller_object)); $controller_dir = explode('\\', get_class($controller_object));
// 0 phpbb, 1 ext, 2 vendor, 3 extension name, ... // 0 vendor, 1 extension name, ...
if (!is_null($this->template) && isset($controller_dir[3]) && $controller_dir[1] === 'ext') if (!is_null($this->template) && isset($controller_dir[1]))
{ {
$controller_style_dir = 'ext/' . $controller_dir[2] . '/' . $controller_dir[3] . '/styles'; $controller_style_dir = 'ext/' . $controller_dir[0] . '/' . $controller_dir[1] . '/styles';
if (is_dir($controller_style_dir)) if (is_dir($controller_style_dir))
{ {

View file

@ -55,7 +55,7 @@ class kernel_exception_subscriber implements EventSubscriberInterface
* @param GetResponseForExceptionEvent $event * @param GetResponseForExceptionEvent $event
* @return null * @return null
*/ */
public function on_kernel_exception(\GetResponseForExceptionEvent $event) public function on_kernel_exception(GetResponseForExceptionEvent $event)
{ {
page_header($this->user->lang('INFORMATION')); page_header($this->user->lang('INFORMATION'));
@ -74,7 +74,7 @@ class kernel_exception_subscriber implements EventSubscriberInterface
$status_code = $exception instanceof HttpException ? $exception->getStatusCode() : 500; $status_code = $exception instanceof HttpException ? $exception->getStatusCode() : 500;
$response = new \Response($this->template->assign_display('body'), $status_code); $response = new Response($this->template->assign_display('body'), $status_code);
$event->setResponse($response); $event->setResponse($response);
} }

View file

@ -65,14 +65,14 @@ class kernel_request_subscriber implements EventSubscriberInterface
* @param GetResponseEvent $event * @param GetResponseEvent $event
* @return null * @return null
*/ */
public function on_kernel_request(\GetResponseEvent $event) public function on_kernel_request(GetResponseEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
$context = new \RequestContext(); $context = new RequestContext();
$context->fromRequest($request); $context->fromRequest($request);
$matcher = phpbb_get_url_matcher($this->finder, $context, $this->root_path, $this->php_ext); $matcher = phpbb_get_url_matcher($this->finder, $context, $this->root_path, $this->php_ext);
$router_listener = new \RouterListener($matcher, $context); $router_listener = new RouterListener($matcher, $context);
$router_listener->onKernelRequest($event); $router_listener->onKernelRequest($event);
} }

View file

@ -31,7 +31,7 @@ class kernel_terminate_subscriber implements EventSubscriberInterface
* @param PostResponseEvent $event * @param PostResponseEvent $event
* @return null * @return null
*/ */
public function on_kernel_terminate(\PostResponseEvent $event) public function on_kernel_terminate(PostResponseEvent $event)
{ {
exit_handler(); exit_handler();
} }

View file

@ -7,7 +7,9 @@
* *
*/ */
class phpbb_auth_provider_acp_board_valid extends \phpbb\auth\provider\base namespace phpbb\auth\provider\acp;
class board_valid extends \phpbb\auth\provider\base
{ {
public function login($username, $password) public function login($username, $password)
{ {

View file

@ -18,7 +18,7 @@ require_once $phpbb_root_path . 'phpbb/class_loader.' . $phpEx;
$phpbb_class_loader_mock = new \phpbb\class_loader('phpbb_mock_', $phpbb_root_path . '../tests/mock/', "php"); $phpbb_class_loader_mock = new \phpbb\class_loader('phpbb_mock_', $phpbb_root_path . '../tests/mock/', "php");
$phpbb_class_loader_mock->register(); $phpbb_class_loader_mock->register();
$phpbb_class_loader_ext = new \phpbb\class_loader('phpbb_ext_', $phpbb_root_path . 'ext/', "php"); $phpbb_class_loader_ext = new \phpbb\class_loader('\\', $phpbb_root_path . 'ext/', "php");
$phpbb_class_loader_ext->register(); $phpbb_class_loader_ext->register();
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', $phpbb_root_path . 'phpbb/', "php"); $phpbb_class_loader = new \phpbb\class_loader('phpbb\\', $phpbb_root_path . 'phpbb/', "php");
$phpbb_class_loader->register(); $phpbb_class_loader->register();

View file

@ -36,22 +36,22 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
$this->assertEquals( $this->assertEquals(
$prefix . 'class_name.php', $prefix . 'class_name.php',
$class_loader->resolve_path('phpbb\\class_name'), $class_loader->resolve_path('\\phpbb\\class_name'),
'Top level class' 'Top level class'
); );
$this->assertEquals( $this->assertEquals(
$prefix . 'dir/class_name.php', $prefix . 'dir/class_name.php',
$class_loader->resolve_path('phpbb\\dir\\class_name'), $class_loader->resolve_path('\\phpbb\\dir\\class_name'),
'Class in a directory' 'Class in a directory'
); );
$this->assertEquals( $this->assertEquals(
$prefix . 'dir/subdir/class_name.php', $prefix . 'dir/subdir/class_name.php',
$class_loader->resolve_path('phpbb\\dir\\subdir\\class_name'), $class_loader->resolve_path('\\phpbb\\dir\\subdir\\class_name'),
'Class in a sub-directory' 'Class in a sub-directory'
); );
$this->assertEquals( $this->assertEquals(
$prefix . 'dir2/dir2.php', $prefix . 'dir2/dir2.php',
$class_loader->resolve_path('phpbb\\dir2\\dir2'), $class_loader->resolve_path('\\phpbb\\dir2\\dir2'),
'Class with name of dir within dir' 'Class with name of dir within dir'
); );
} }
@ -59,8 +59,8 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
public function test_resolve_cached() public function test_resolve_cached()
{ {
$cache_map = array( $cache_map = array(
'class_loader_phpbb__' => array('phpbb\\a\\cached_name' => 'a/cached_name'), 'class_loader___phpbb__' => array('\\phpbb\\a\\cached_name' => 'a/cached_name'),
'class_loader___' => array('phpbb\\ext\\foo' => 'foo'), 'class_loader___' => array('\\phpbb\\ext\\foo' => 'foo'),
); );
$cache = new phpbb_mock_cache($cache_map); $cache = new phpbb_mock_cache($cache_map);
@ -72,26 +72,26 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
$this->assertEquals( $this->assertEquals(
$prefix . 'dir/class_name.php', $prefix . 'dir/class_name.php',
$class_loader->resolve_path('phpbb\\dir\\class_name'), $class_loader->resolve_path('\\phpbb\\dir\\class_name'),
'Class in a directory' 'Class in a directory'
); );
$this->assertFalse($class_loader->resolve_path('phpbb\\ext\\foo')); $this->assertFalse($class_loader->resolve_path('\\phpbb\\ext\\foo'));
$this->assertFalse($class_loader_ext->resolve_path('phpbb\\a\\cached_name')); $this->assertFalse($class_loader_ext->resolve_path('\\phpbb\\a\\cached_name'));
$this->assertEquals( $this->assertEquals(
$prefix . 'a/cached_name.php', $prefix . 'a/cached_name.php',
$class_loader->resolve_path('phpbb\\a\\cached_name'), $class_loader->resolve_path('\\phpbb\\a\\cached_name'),
'Cached class found' 'Cached class found'
); );
$this->assertEquals( $this->assertEquals(
$prefix . 'foo.php', $prefix . 'foo.php',
$class_loader_ext->resolve_path('phpbb\\ext\\foo'), $class_loader_ext->resolve_path('\\phpbb\\ext\\foo'),
'Cached class found in alternative loader' 'Cached class found in alternative loader'
); );
$cache_map['class_loader_phpbb__']['phpbb\\dir\\class_name'] = 'dir/class_name'; $cache_map['class_loader___phpbb__']['\\phpbb\\dir\\class_name'] = 'dir/class_name';
$cache->check($this, $cache_map); $cache->check($this, $cache_map);
} }
} }

View file

@ -53,11 +53,11 @@ class phpbb_controller_controller_test extends phpbb_test_case
// Autoloading classes within the tests folder does not work // Autoloading classes within the tests folder does not work
// so I'll include them manually. // so I'll include them manually.
if (!class_exists('phpbb_ext_foo_controller')) if (!class_exists('foo\\controller'))
{ {
include(__DIR__.'/ext/foo/controller.php'); include(__DIR__.'/ext/foo/controller.php');
} }
if (!class_exists('phpbb_controller_foo')) if (!class_exists('phpbb\\controller\\foo'))
{ {
include(__DIR__.'/phpbb/controller/foo.php'); include(__DIR__.'/phpbb/controller/foo.php');
} }
@ -66,11 +66,11 @@ class phpbb_controller_controller_test extends phpbb_test_case
$symfony_request = new Request(); $symfony_request = new Request();
$symfony_request->attributes->set('_controller', 'foo.controller:handle'); $symfony_request->attributes->set('_controller', 'foo.controller:handle');
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_ext_foo_controller, 'handle')); $this->assertEquals($resolver->getController($symfony_request), array(new foo\controller, 'handle'));
$symfony_request = new Request(); $symfony_request = new Request();
$symfony_request->attributes->set('_controller', 'core_foo.controller:bar'); $symfony_request->attributes->set('_controller', 'core_foo.controller:bar');
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_controller_foo, 'bar')); $this->assertEquals($resolver->getController($symfony_request), array(new phpbb\controller\foo, 'bar'));
} }
} }

View file

@ -1,3 +1,3 @@
services: services:
foo.controller: foo.controller:
class: phpbb_ext_foo_controller class: foo\controller

View file

@ -1,8 +1,10 @@
<?php <?php
namespace foo;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class phpbb_ext_foo_controller class controller
{ {
/** /**
* Handle method * Handle method

View file

@ -1,5 +1,7 @@
<?php <?php
class phpbb_ext_bar_my_hidden_class namespace bar\my;
class hidden_class
{ {
} }

View file

@ -11,7 +11,7 @@ class foo_info
public function module() public function module()
{ {
return array( return array(
'filename' => 'phpbb_ext_foo_acp_fail_module', 'filename' => 'foo\acp\fail_module',
'title' => 'Foobar', 'title' => 'Foobar',
'version' => '3.1.0-dev', 'version' => '3.1.0-dev',
'modes' => array( 'modes' => array(

View file

@ -78,7 +78,7 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
$this->phpbb_extension_manager->enable('foo/bar'); $this->phpbb_extension_manager->enable('foo/bar');
$crawler = self::request('GET', 'app.php/foo/baz', array(), false); $crawler = self::request('GET', 'app.php/foo/baz', array(), false);
$this->assert_response_html(500); $this->assert_response_html(500);
$this->assertContains('Missing value for argument #1: test in class phpbb_ext_foo_bar_controller:baz', $crawler->filter('body')->text()); $this->assertContains('Missing value for argument #1: test in class foo\bar\controller\controller:baz', $crawler->filter('body')->text());
$this->phpbb_extension_manager->purge('foo/bar'); $this->phpbb_extension_manager->purge('foo/bar');
} }

View file

@ -69,7 +69,7 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
$modules->update_module_data($parent_data, true); $modules->update_module_data($parent_data, true);
$module_data = array( $module_data = array(
'module_basename' => 'phpbb_ext_foo_bar_acp_main_module', 'module_basename' => 'foo\\bar\\acp\\main_module',
'module_enabled' => 1, 'module_enabled' => 1,
'module_display' => 1, 'module_display' => 1,
'parent_id' => $parent_data['module_id'], 'parent_id' => $parent_data['module_id'],
@ -90,7 +90,7 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
{ {
$this->login(); $this->login();
$this->admin_login(); $this->admin_login();
$crawler = self::request('GET', 'adm/index.php?i=phpbb_ext_foo_bar_acp_main_module&mode=mode&sid=' . $this->sid); $crawler = self::request('GET', 'adm/index.php?i=foo\\bar\\acp\\main_module&mode=mode&sid=' . $this->sid);
$this->assertContains("Bertie rulez!", $crawler->filter('#main')->text()); $this->assertContains("Bertie rulez!", $crawler->filter('#main')->text());
$this->phpbb_extension_manager->purge('foo/bar'); $this->phpbb_extension_manager->purge('foo/bar');
} }

View file

@ -8,6 +8,8 @@
* *
*/ */
namespace foo\bar\acp;
/** /**
* @ignore * @ignore
*/ */
@ -16,12 +18,12 @@ if (!defined('IN_PHPBB'))
exit; exit;
} }
class phpbb_ext_foo_bar_acp_main_info class main_info
{ {
function module() function module()
{ {
return array( return array(
'filename' => 'phpbb_ext_foo_bar_acp_main_module', 'filename' => 'foo\bar\acp\main_module',
'title' => 'ACP_FOOBAR_TITLE', 'title' => 'ACP_FOOBAR_TITLE',
'version' => '1.0.0', 'version' => '1.0.0',
'modes' => array( 'modes' => array(

View file

@ -8,6 +8,8 @@
* *
*/ */
namespace foo\bar\acp;
/** /**
* @ignore * @ignore
*/ */
@ -16,7 +18,7 @@ if (!defined('IN_PHPBB'))
exit; exit;
} }
class phpbb_ext_foo_bar_acp_main_module class main_module
{ {
var $u_action; var $u_action;

View file

@ -1,6 +1,6 @@
services: services:
foo_bar.controller: foo_bar.controller:
class: phpbb_ext_foo_bar_controller class: foo\bar\controller\controller
arguments: arguments:
- @controller.helper - @controller.helper
- @template - @template

View file

@ -1,7 +1,10 @@
<?php <?php
namespace foo\bar\controller;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class phpbb_ext_foo_bar_controller class controller
{ {
protected $template; protected $template;

View file

@ -8,6 +8,8 @@
* *
*/ */
namespace foo\bar\event;
/** /**
* @ignore * @ignore
*/ */
@ -22,7 +24,7 @@ if (!defined('IN_PHPBB'))
*/ */
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class phpbb_ext_foo_bar_event_permission implements EventSubscriberInterface class permission implements EventSubscriberInterface
{ {
static public function getSubscribedEvents() static public function getSubscribedEvents()
{ {

View file

@ -8,6 +8,8 @@
* *
*/ */
namespace foo\bar\event;
/** /**
* @ignore * @ignore
*/ */
@ -22,7 +24,7 @@ if (!defined('IN_PHPBB'))
*/ */
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class phpbb_ext_foo_bar_event_user_setup implements EventSubscriberInterface class user_setup implements EventSubscriberInterface
{ {
static public function getSubscribedEvents() static public function getSubscribedEvents()
{ {

View file

@ -1,6 +1,8 @@
<?php <?php
class phpbb_ext_foo_bar_ext extends \phpbb\extension\base namespace foo\bar;
class ext extends \phpbb\extension\base
{ {
} }