[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.
*/
public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null)
{
{
if ($namespace[0] !== '\\')
{
$namespace = '\\' . $namespace;
}
$this->namespace = $namespace;
$this->path = $path;
$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.
*
* @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
* class or false if looking it up failed.
*/
@ -144,6 +150,7 @@ class class_loader
*/
public function load_class($class)
{
$class = '\\' . $class;
if (substr($class, 0, strlen($this->namespace)) === $this->namespace)
{
$path = $this->resolve_path($class);

View file

@ -91,7 +91,7 @@ class helper
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
* 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, ...
if (!is_null($this->template) && isset($controller_dir[3]) && $controller_dir[1] === 'ext')
// 0 vendor, 1 extension name, ...
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))
{

View file

@ -55,7 +55,7 @@ class kernel_exception_subscriber implements EventSubscriberInterface
* @param GetResponseForExceptionEvent $event
* @return null
*/
public function on_kernel_exception(\GetResponseForExceptionEvent $event)
public function on_kernel_exception(GetResponseForExceptionEvent $event)
{
page_header($this->user->lang('INFORMATION'));
@ -74,7 +74,7 @@ class kernel_exception_subscriber implements EventSubscriberInterface
$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);
}

View file

@ -65,14 +65,14 @@ class kernel_request_subscriber implements EventSubscriberInterface
* @param GetResponseEvent $event
* @return null
*/
public function on_kernel_request(\GetResponseEvent $event)
public function on_kernel_request(GetResponseEvent $event)
{
$request = $event->getRequest();
$context = new \RequestContext();
$context = new RequestContext();
$context->fromRequest($request);
$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);
}

View file

@ -31,7 +31,7 @@ class kernel_terminate_subscriber implements EventSubscriberInterface
* @param PostResponseEvent $event
* @return null
*/
public function on_kernel_terminate(\PostResponseEvent $event)
public function on_kernel_terminate(PostResponseEvent $event)
{
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)
{

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->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 = new \phpbb\class_loader('phpbb\\', $phpbb_root_path . 'phpbb/', "php");
$phpbb_class_loader->register();

View file

@ -36,22 +36,22 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
$this->assertEquals(
$prefix . 'class_name.php',
$class_loader->resolve_path('phpbb\\class_name'),
$class_loader->resolve_path('\\phpbb\\class_name'),
'Top level class'
);
$this->assertEquals(
$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'
);
$this->assertEquals(
$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'
);
$this->assertEquals(
$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'
);
}
@ -59,8 +59,8 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
public function test_resolve_cached()
{
$cache_map = array(
'class_loader_phpbb__' => array('phpbb\\a\\cached_name' => 'a/cached_name'),
'class_loader___' => array('phpbb\\ext\\foo' => 'foo'),
'class_loader___phpbb__' => array('\\phpbb\\a\\cached_name' => 'a/cached_name'),
'class_loader___' => array('\\phpbb\\ext\\foo' => 'foo'),
);
$cache = new phpbb_mock_cache($cache_map);
@ -72,26 +72,26 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase
$this->assertEquals(
$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'
);
$this->assertFalse($class_loader->resolve_path('phpbb\\ext\\foo'));
$this->assertFalse($class_loader_ext->resolve_path('phpbb\\a\\cached_name'));
$this->assertFalse($class_loader->resolve_path('\\phpbb\\ext\\foo'));
$this->assertFalse($class_loader_ext->resolve_path('\\phpbb\\a\\cached_name'));
$this->assertEquals(
$prefix . 'a/cached_name.php',
$class_loader->resolve_path('phpbb\\a\\cached_name'),
$class_loader->resolve_path('\\phpbb\\a\\cached_name'),
'Cached class found'
);
$this->assertEquals(
$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'
);
$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);
}
}

View file

@ -53,11 +53,11 @@ class phpbb_controller_controller_test extends phpbb_test_case
// Autoloading classes within the tests folder does not work
// so I'll include them manually.
if (!class_exists('phpbb_ext_foo_controller'))
if (!class_exists('foo\\controller'))
{
include(__DIR__.'/ext/foo/controller.php');
}
if (!class_exists('phpbb_controller_foo'))
if (!class_exists('phpbb\\controller\\foo'))
{
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->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->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:
foo.controller:
class: phpbb_ext_foo_controller
class: foo\controller

View file

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

View file

@ -1,5 +1,7 @@
<?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()
{
return array(
'filename' => 'phpbb_ext_foo_acp_fail_module',
'filename' => 'foo\acp\fail_module',
'title' => 'Foobar',
'version' => '3.1.0-dev',
'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');
$crawler = self::request('GET', 'app.php/foo/baz', array(), false);
$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');
}

View file

@ -69,7 +69,7 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
$modules->update_module_data($parent_data, true);
$module_data = array(
'module_basename' => 'phpbb_ext_foo_bar_acp_main_module',
'module_basename' => 'foo\\bar\\acp\\main_module',
'module_enabled' => 1,
'module_display' => 1,
'parent_id' => $parent_data['module_id'],
@ -90,7 +90,7 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
{
$this->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->phpbb_extension_manager->purge('foo/bar');
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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