Thursday, 5 September 2013

Best Practice for wrapping non-class code

Best Practice for wrapping non-class code

What is a good pattern for including and/or wrapping non-classes inside of
a class? For example, I need to use this php file
https://github.com/widop/phpbb3/blob/master/common.php to login to my
phpbb board. Specifically, I need this file to bootload phpbb and then I
will use the $user and $auth variables to login a user. In my code I have
an AuthClient class.
I'm trying to figure out best practice to include common.php from phpbb
and use it inside my class:
class AuthClient implements IAuthClient{
protected $user;
protected $auth;
public function __construct(){
/** Bootloading PHPBB */
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$this->user = $user;
$this->auth = $auth;
$this->user->session_begin();
$this->auth->acl($user->data);
}
public function login($user_id, $admin, $autologin){
$result = $this->user->session_create($user_id, $admin, $autologin,
true);
}
public function logout(){
$this->user->session_kill();
$this->user->session_begin();
}
}
=========Edited Based On Feedback======================
I believe I've made an improvement, but it is still not working.
Getting an error: "Cannot redeclare class auth"
This references the non-namespaced class loaded by bootstrap.php
https://github.com/widop/phpbb3/blob/master/common.ph
bootstrap.php
- ewwww
define('IN_PHPBB', true);
$phpbb_root_path = base_path() . "/phpbb3/";
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once(base_path() . '/phpbb3/common.php');
LoginController.php - Laravel
use myproject\models\User;
use myproject\models\phpbbb\Phpbb;
use myproject\models\phpbb\AuthClient;
use myproject\models\phpbb\User as PhpbbUser;
require_once(base_path() . '/app/models/Phpbb/bootstrap.php');
class LoginController extends BaseController{
public function login(){
//...login in main application
//Login in phpbb - more ewww
global $user;
global $auth;
$phpbb = new AuthClient($user, $auth);
$phpbb->login();
}
}
AuthClient.php
<?php
namespace myproject\models\phpbb;
use myproject\models\phpbbb\Phpbb;
class AuthClient{
protected $user;
protected $auth;
public function __construct($user, $auth){
$this->user = $user;
$this->auth = $auth;
}
public function login($user_id, $admin, $autologin){
$this->user->session_begin();
$this->auth->acl($this->user->data);
$result = $this->user->session_create($user_id, $admin, $autologin,
true);
}
public function logout(){
$this->user->session_kill();
$this->user->session_begin();
}
}

No comments:

Post a Comment