Code Monkey home page Code Monkey logo

php-xconn's Introduction

php-xconn (Cross Connector)

Simply handles multiple connections across multiple user adapters.

Features

  • Connects only when necessary and only once during runtime
  • No dependencies - write any adapter for any extension or just proxy the interface itself
  • Different instances for one adapter (e.g. several different memcached instances)
  • Supports closure approach
  • Easy to configure

Required

  • PHP 5.4+

Usage

use xconn\Connector;

Option 1. Closure

/* Touch connection and pass closure. You may pass data scope inside through 'use' */
$foo = Connector::touch('memcached', function($m) {
    return $m->get('bar');
}));

/* More logic inside */
$users = Connector::touch('production_database', function ($db) {
    $list = [];
    $result = $db->query("SELECT user_name FROM users");
    while ($data = $db->fetch($result)) {
        $list[] = $data['user_name'];
    }
    return $list;
});

Option 2. Get adapter instance

/* Simply take an instance */
$db = Connector::take('local_database');

/* And use it */
$result = $db->query('SELECT foo FROM bar');

Option 3. Proxy original interface

/* Proxy adapted connection */
$m = Connector::proxy('memcached');

/* Use original interface. ! Adapters' aliases will not work */
print_r($m->getVersion());

Adapters

Each adapter should extend common Adapter class and define abstract methods. The $config will be passed from the configuration file. Use $this->connection as instance inside your adapter methods.

    protected $connection = null;
    abstract protected function openConnection($config);
    abstract protected function closeConnection();

Memcached example

<?php namespace xconn\adapters;

final class Memcached extends \xconn\Adapter {

    /* memcached compare-and-save token */
    public $cas_token;
    
    /* @return Instance|bool */
    protected function openConnection($config) {
    
        $instance = new \Memcached();
        
        if (!$instance->addServer($config['host'], $config['port'])) {
            return false;
        }

        return $instance;
    }

    /* Correctly close the connection */
    protected function closeConnection() {

        if (!$this->connection) {
            return false;
        }

        $this->connection->quit();
    }
    
    /* Put your aliases here */
    public function get($key) {
        return $this->connection->get($key, null, $this->cas_token);
    }
    
    /* .... */
    

Configuration example

All variables will be passed as $config to Adapter\openConnection;

[production_database]
adapter = MySQL
host = null
user = root
port = 0
pass = pass
name = dbname
socket = /tmp/mysql.sock

[memcached]
adapter = memcached
prefix = myprefix
host = localhost
port = 11211

php-xconn's People

Contributors

wake-up-neo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.