Welcome Guest, Not a member yet? Register   Sign In
How to use WhichBrowser as a library??
#1

There is this plugin(WhichBrowser->www.whichbrowser.net) i want to use in my project. Sorry i can't use composer because my host doesn't allow it. This is how it goes by:
Code:
<?php
// libraries/parse.php

//uncomment this and it prints(No direct script access allowed!)

/*
if(!defined(BASEPATH))
exit('No direct script access allowed!');
*/
use WhichBrowser\Parser;

require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';

//class Parse{

class Parse extends Parser{

/**/
//public function __construct(){
//self::Parser($_SERVER['HTTP_USER_AGENT']);

//require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';


//    }
/**/

}

in my controller's index method:
Code:
$this->load->library('parse', $_SERVER['HTTP_USER_AGENT']);
echo 'You are using: '.$this->parse->toString(); // prints 'You are usng an unknown browser' instead of 'You are using Opera Mini 8.0 on Nokia C3-00 running Series40'
anyway to fix this??
Be Simple Angel
Reply
#2

I would create in a CodeIgniter style a library Which_browser. In its constructor I would put
Code:
require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';

Then I would see what methods this new library would need and I would instantiate the external class wherever it is necessary for implementation of these methods.
Reply
#3

(03-28-2016, 01:54 AM)ivantcholakov Wrote: I would create in a CodeIgniter style a library Which_browser. In its constructor I would put
Code:
require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';

Then I would see what methods this new library would need and I would instantiate the external class wherever it is necessary for implementation of these methods.

thanks for your reply. But can you hit a working sample code for me to understan you clearly?
Be Simple Angel
Reply
#4

It is easy, but ok, I'll do it. Wait.
Reply
#5

For most simple usage the buildin user agent library is excelent. Have you tried using this or do you need a better detection rate in bot names for example?


https://www.codeigniter.com/userguide3/l...agent.html
Reply
#6

@waptik

File: APPPATH/libraries/Which_browser.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
* @author Ivan Tcholakov <[email protected]>, 2016
* @license The MIT License, http://opensource.org/licenses/MIT
*/

class Which_browser {

    protected $php_min = '5.4';

    protected $wb_parser;

    protected $with_clause = false;
    protected $wb_parser_2;

    public function __construct($config = array()) {

        if (!is_php($this->php_min)) {
            throw new Exception('Which_browser: Requires PHP '.$this->php_min.' or above.');
        }

        // A special code fragment for @waptik.
        // Better remove it and rely on Composer.
        if (!class_exists('WhichBrowser\Parser', true)) {
            require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';
        }
        //

        $this->initialize($config);
    }

    public function initialize($config = array()) {

        if (!is_array($config)) {
            $config = array();
        }

        $raw_info = isset($config['raw_info']) && is_array($config['raw_info'])
            ? $config['raw_info']
            : array();

        if (empty($raw_info)) {

            if (function_exists('getallheaders')) {
                $raw_info = getallheaders();
            } else {
                $raw_info = isset($_SERVER['HTTP_USER_AGENT'])
                    ? $_SERVER['HTTP_USER_AGENT']
                    : array();
            }
        }

        //$this->wb_parser = new WhichBrowser\Parser($this->_get_raw_info($raw_info));
        // Still keeping the PHP 5.2 syntax:
        $reflection = new ReflectionClass('WhichBrowser\Parser');
        $this->wb_parser = $reflection->newInstance($raw_info);

        return $this;
    }

    // Normal usage:
    // $this->load->library('which_browser');
    // $result = $this->which_browser->get();
    // echo $result->toString();
    public function get() {

        if ($this->with_clause) {

            $this->with_clause = false;
            return $this->wb_parser_2;
        }

        return $this->wb_parser;
    }

    // Usage for testing, an alternative parser is initialized in this case.:
    // $this->load->library('which_browser');
    // $result = $this->which_browser->with(getallheaders())->get();
    // echo $result->toString();
    public function with($raw_info = array()) {

        if (!is_array($raw_info)) {
            $raw_info = array();
        }

        $this->with_clause = true;

        //$this->wb_parser_2 = new WhichBrowser\Parser($raw_info);
        // Still keeping the PHP 5.2 syntax:
        $reflection = new ReflectionClass('WhichBrowser\Parser');
        $this->wb_parser_2 = $reflection->newInstance($raw_info);

        return $this;
    }

}
Reply
#7

(03-28-2016, 05:26 AM)ivantcholakov Wrote: @waptik

File: APPPATH/libraries/Which_browser.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
* @author Ivan Tcholakov <[email protected]>, 2016
* @license The MIT License, http://opensource.org/licenses/MIT
*/

class Which_browser {

    protected $php_min = '5.4';

    protected $wb_parser;

    protected $with_clause = false;
    protected $wb_parser_2;

    public function __construct($config = array()) {

        if (!is_php($this->php_min)) {
            throw new Exception('Which_browser: Requires PHP '.$this->php_min.' or above.');
        }

        // A special code fragment for @waptik.
        // Better remove it and rely on Composer.
        if (!class_exists('WhichBrowser\Parser', true)) {
            require_once APPPATH.'third_party/Parser-2.0.16/bootstrap.php';
        }
        //

        $this->initialize($config);
    }

    public function initialize($config = array()) {

        if (!is_array($config)) {
            $config = array();
        }

        $raw_info = isset($config['raw_info']) && is_array($config['raw_info'])
            ? $config['raw_info']
            : array();

        if (empty($raw_info)) {

            if (function_exists('getallheaders')) {
                $raw_info = getallheaders();
            } else {
                $raw_info = isset($_SERVER['HTTP_USER_AGENT'])
                    ? $_SERVER['HTTP_USER_AGENT']
                    : array();
            }
        }

        //$this->wb_parser = new WhichBrowser\Parser($this->_get_raw_info($raw_info));
        // Still keeping the PHP 5.2 syntax:
        $reflection = new ReflectionClass('WhichBrowser\Parser');
        $this->wb_parser = $reflection->newInstance($raw_info);

        return $this;
    }

    // Normal usage:
    // $this->load->library('which_browser');
    // $result = $this->which_browser->get();
    // echo $result->toString();
    public function get() {

        if ($this->with_clause) {

            $this->with_clause = false;
            return $this->wb_parser_2;
        }

        return $this->wb_parser;
    }

    // Usage for testing, an alternative parser is initialized in this case.:
    // $this->load->library('which_browser');
    // $result = $this->which_browser->with(getallheaders())->get();
    // echo $result->toString();
    public function with($raw_info = array()) {

        if (!is_array($raw_info)) {
            $raw_info = array();
        }

        $this->with_clause = true;

        //$this->wb_parser_2 = new WhichBrowser\Parser($raw_info);
        // Still keeping the PHP 5.2 syntax:
        $reflection = new ReflectionClass('WhichBrowser\Parser');
        $this->wb_parser_2 = $reflection->newInstance($raw_info);

        return $this;
    }

}

Thanks man
Be Simple Angel
Reply
#8

@ivan it works perfectly.
So should i use the same approch in any plugin i want to use?
Be Simple Angel
Reply
#9

I think, it depends on the API that is provided. For simple packages this approach is good.

For something more complex a unique approach would be needed maybe, I have not faced such case yet.
Reply
#10

Here is a simpler approach for this package: https://github.com/felixgirault/multiplayer

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*/

class Multiplayer extends \Multiplayer\Multiplayer {

    public function __construct($config = array()) {

        if (!is_array($config)) {
            $config = array();
        }

        parent::__construct($config);
    }

}

It depends on how the package fits to CodeIgniter libraries way.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB