[ticket/291] Add portal helper class
B3P-291
This commit is contained in:
@@ -23,9 +23,9 @@ services:
|
|||||||
- @template
|
- @template
|
||||||
- @user
|
- @user
|
||||||
- @path_helper
|
- @path_helper
|
||||||
|
- @board3.portal.helper
|
||||||
- %core.root_path%
|
- %core.root_path%
|
||||||
- .%core.php_ext%
|
- .%core.php_ext%
|
||||||
- @board3.module_collection
|
|
||||||
- %board3.config.table%
|
- %board3.config.table%
|
||||||
- %board3.modules.table%
|
- %board3.modules.table%
|
||||||
|
|
||||||
@@ -45,3 +45,9 @@ services:
|
|||||||
- @service_container
|
- @service_container
|
||||||
tags:
|
tags:
|
||||||
- { name: service_collection, tag: board3.module }
|
- { name: service_collection, tag: board3.module }
|
||||||
|
|
||||||
|
board3.portal.helper:
|
||||||
|
class: board3\portal\includes\helper
|
||||||
|
arguments:
|
||||||
|
- @auth
|
||||||
|
- @board3.module_collection
|
||||||
|
|||||||
@@ -13,51 +13,51 @@ class main
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Auth object
|
* Auth object
|
||||||
* @var phpbb_auth
|
* @var \phpbb\auth\auth
|
||||||
*/
|
*/
|
||||||
private $auth;
|
protected $auth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* phpBB Config object
|
* phpBB Config object
|
||||||
* @var phpbb_config_db
|
* @var \phpbb\config\config
|
||||||
*/
|
*/
|
||||||
private $config;
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template object
|
* Template object
|
||||||
* @var phpbb_template
|
* @var \phpbb\template
|
||||||
*/
|
*/
|
||||||
private $template;
|
protected $template;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User object
|
* User object
|
||||||
* @var phpbb_user
|
* @var \phpbb\user
|
||||||
*/
|
*/
|
||||||
private $user;
|
protected $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* phpBB root path
|
* phpBB root path
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $phpbb_root_path;
|
protected $phpbb_root_path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP file extension
|
* PHP file extension
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $php_ext;
|
protected $php_ext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Portal root path
|
* Portal root path
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $root_path;
|
protected $root_path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Portal includes path
|
* Portal includes path
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $includes_path;
|
protected $includes_path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* phpBB path helper
|
* phpBB path helper
|
||||||
@@ -66,28 +66,27 @@ class main
|
|||||||
protected $path_helper;
|
protected $path_helper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Board3 Modules service collection
|
* Portal Helper object
|
||||||
* @var phpbb\di\service_collection
|
* @var \board3\portal\includes\helper
|
||||||
*/
|
*/
|
||||||
protected $modules;
|
protected $portal_helper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* NOTE: The parameters of this method must match in order and type with
|
* NOTE: The parameters of this method must match in order and type with
|
||||||
* the dependencies defined in the services.yml file for this service.
|
* the dependencies defined in the services.yml file for this service.
|
||||||
* @param phpbb_auth $auth Auth object
|
* @param \phpbb\auth\auth $auth Auth object
|
||||||
* @param phpbb_config_db $config phpBB Config object
|
* @param \phpbb\config\config $config phpBB Config object
|
||||||
* @param phpbb_template $template Template object
|
* @param \phpbb\template $template Template object
|
||||||
* @param phpbb_user $user User object
|
* @param \phpbb\user $user User object
|
||||||
* @param \phpbb\path_helper $path_helper phpBB path helper
|
* @param \phpbb\path_helper $path_helper phpBB path helper
|
||||||
|
* @param \board3\portal\includes\portal_helper $portal_helper Portal helper class
|
||||||
* @param string $phpbb_root_path phpBB root path
|
* @param string $phpbb_root_path phpBB root path
|
||||||
* @param string $php_ext PHP file extension
|
* @param string $php_ext PHP file extension
|
||||||
* @param \phpbb\di\service_collection $modules Board3 Modules service
|
|
||||||
* collection
|
|
||||||
* @param string $config_table Board3 config table
|
* @param string $config_table Board3 config table
|
||||||
* @param string $modules_table Board3 modules table
|
* @param string $modules_table Board3 modules table
|
||||||
*/
|
*/
|
||||||
public function __construct($auth, $config, $template, $user, $path_helper, $phpbb_root_path, $php_ext, $modules, $config_table, $modules_table)
|
public function __construct($auth, $config, $template, $user, $path_helper, $portal_helper, $phpbb_root_path, $php_ext, $config_table, $modules_table)
|
||||||
{
|
{
|
||||||
global $portal_root_path;
|
global $portal_root_path;
|
||||||
|
|
||||||
@@ -98,7 +97,7 @@ class main
|
|||||||
$this->path_helper = $path_helper;
|
$this->path_helper = $path_helper;
|
||||||
$this->phpbb_root_path = $phpbb_root_path;
|
$this->phpbb_root_path = $phpbb_root_path;
|
||||||
$this->php_ext = $php_ext;
|
$this->php_ext = $php_ext;
|
||||||
$this->register_modules($modules);
|
$this->portal_helper = $portal_helper;
|
||||||
|
|
||||||
$this->includes_path = $phpbb_root_path . 'ext/board3/portal/includes/';
|
$this->includes_path = $phpbb_root_path . 'ext/board3/portal/includes/';
|
||||||
$this->root_path = $phpbb_root_path . 'ext/board3/portal/';
|
$this->root_path = $phpbb_root_path . 'ext/board3/portal/';
|
||||||
@@ -114,25 +113,6 @@ class main
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register list of Board3 Portal modules
|
|
||||||
*
|
|
||||||
* @param \phpbb\di\service_collection $modules Board3 Modules service
|
|
||||||
* collection
|
|
||||||
* @return null
|
|
||||||
*/
|
|
||||||
protected function register_modules($modules)
|
|
||||||
{
|
|
||||||
foreach ($modules as $current_module)
|
|
||||||
{
|
|
||||||
$class_name = '\\' . get_class($current_module);
|
|
||||||
if (!isset($this->modules[$class_name]))
|
|
||||||
{
|
|
||||||
$this->modules[$class_name] = $current_module;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension front handler method. This is called automatically when your extension is accessed
|
* Extension front handler method. This is called automatically when your extension is accessed
|
||||||
* through index.php?ext=example/foobar
|
* through index.php?ext=example/foobar
|
||||||
@@ -180,27 +160,10 @@ class main
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Do not try to load non-existent modules
|
// Do not try to load non-existent modules
|
||||||
if (!isset($this->modules[$row['module_classname']]))
|
if (!($module = $this->portal_helper->get_module($row['module_classname'])))
|
||||||
{
|
|
||||||
if (file_exists("{$this->includes_path}modules/portal_{$row['module_classname']}{$this->php_ext}"))
|
|
||||||
{
|
|
||||||
include("{$this->includes_path}modules/portal_{$row['module_classname']}{$this->php_ext}");
|
|
||||||
}
|
|
||||||
|
|
||||||
$class_name = 'portal_' . $row['module_classname'] . '_module';
|
|
||||||
if (class_exists($class_name))
|
|
||||||
{
|
|
||||||
$module = new $class_name();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$module = $this->modules[$row['module_classname']];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check for permissions before loading anything
|
* Check for permissions before loading anything
|
||||||
@@ -317,7 +280,7 @@ class main
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if user should be able to access this page
|
// check if user should be able to access this page
|
||||||
private function check_permission()
|
protected function check_permission()
|
||||||
{
|
{
|
||||||
if (!isset($this->config['board3_enable']) || !$this->config['board3_enable'] || !$this->auth->acl_get('u_view_portal'))
|
if (!isset($this->config['board3_enable']) || !$this->config['board3_enable'] || !$this->auth->acl_get('u_view_portal'))
|
||||||
{
|
{
|
||||||
|
|||||||
75
includes/helper.php
Normal file
75
includes/helper.php
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package Board3 Portal v2.1
|
||||||
|
* @copyright (c) 2014 Board3 Group ( www.board3.de )
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace board3\portal\includes;
|
||||||
|
|
||||||
|
class helper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Auth object
|
||||||
|
* @var \phpbb\auth\auth
|
||||||
|
*/
|
||||||
|
private $auth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Board3 Modules service collection
|
||||||
|
* @var \phpbb\di\service_collection
|
||||||
|
*/
|
||||||
|
protected $modules;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* NOTE: The parameters of this method must match in order and type with
|
||||||
|
* the dependencies defined in the services.yml file for this service.
|
||||||
|
* @param \phpbb\auth\auth $auth Auth object
|
||||||
|
* @param \phpbb\di\service_collection $modules Board3 Modules service
|
||||||
|
* collection
|
||||||
|
*/
|
||||||
|
public function __construct($auth, $modules)
|
||||||
|
{
|
||||||
|
$this->auth = $auth;
|
||||||
|
$this->register_modules($modules);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register list of Board3 Portal modules
|
||||||
|
*
|
||||||
|
* @param \phpbb\di\service_collection $modules Board3 Modules service
|
||||||
|
* collection
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
protected function register_modules($modules)
|
||||||
|
{
|
||||||
|
foreach ($modules as $current_module)
|
||||||
|
{
|
||||||
|
$class_name = '\\' . get_class($current_module);
|
||||||
|
if (!isset($this->modules[$class_name]))
|
||||||
|
{
|
||||||
|
$this->modules[$class_name] = $current_module;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get module specified by module class name
|
||||||
|
*
|
||||||
|
* @param string $module_name Module class name
|
||||||
|
*/
|
||||||
|
public function get_module($module_name)
|
||||||
|
{
|
||||||
|
if (isset($this->modules[$module_name]))
|
||||||
|
{
|
||||||
|
return $this->modules[$module_name];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
tests/unit/includes/helper_test.php
Normal file
53
tests/unit/includes/helper_test.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package testing
|
||||||
|
* @copyright (c) Board3 Group ( www.board3.de )
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
class board3_includes_helper_test extends \board3\portal\tests\testframework\test_case
|
||||||
|
{
|
||||||
|
protected $portal_helper;
|
||||||
|
|
||||||
|
protected $modules;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$config = new \phpbb\config\config(array());
|
||||||
|
$this->modules = array(
|
||||||
|
'\board3\portal\modules\link_us' => new \board3\portal\modules\link_us($config, new \board3\portal\tests\mock\template($this), new \board3\portal\tests\mock\user),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->portal_helper = $this->get_portal_helper($this->modules);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function get_portal_helper($modules)
|
||||||
|
{
|
||||||
|
$this->portal_helper = new \board3\portal\includes\helper(array(), $modules);
|
||||||
|
|
||||||
|
return $this->portal_helper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function data_get_module()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(false, '\board3\portal\modules\user_menu'),
|
||||||
|
array('\board3\portal\modules\link_us', '\board3\portal\modules\link_us'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_get_module
|
||||||
|
*/
|
||||||
|
public function test_get_module($expected, $module_name)
|
||||||
|
{
|
||||||
|
if (!empty($expected))
|
||||||
|
{
|
||||||
|
$expected = $this->modules[$expected];
|
||||||
|
}
|
||||||
|
$this->assertEquals($expected, $this->portal_helper->get_module($module_name));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user