Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DataSource component to be open stack based #71

Open
dragoonis opened this issue Feb 25, 2013 · 4 comments
Open

Refactor DataSource component to be open stack based #71

dragoonis opened this issue Feb 25, 2013 · 4 comments
Assignees
Milestone

Comments

@dragoonis
Copy link
Member

by open stack I mean not closely tied to doctrine\dbal by default, but have a ConnectionManager for dbal or whatever components we have setup.

I want DataSource to be glue between the app's app/config/datasource.php config file and the chosen library.

So for dbal we need a handler to give you a 'connection' setup per key

For laravel's db layer there's a conection manager too which i have a gist for.

It should be a clean component with some a ConnectionManagerInterface which each library will be setup with

@dragoonis
Copy link
Member Author

General Container

namespace PPI\DataSource;
interface ConnectionContainerInferface {
    public function getConnectionByName($name);
}
namespace PPI\DataSource\Container;
class Laravel implements ConnectionContainerInferface {

    protected $connFactory;
    protected $resolver;


    public function __construct(array $connections) {
        $connectionFactory = new Illuminate\Database\Connectors\ConnectionFactory;
        $resolver = new Illuminate\Database\ConnectionResolver();
        foreach($connections as $name => $conn) {
            $resolver->addConnection($name, $connectionFactory->make($conn));
        }
        $resolver->setDefaultConnection('default');

        $this->resolver = $resolver;
        $this->connFactory = $connectionFactory;

    }

    public function getConnectionByName($name) {
        if(!$this->resolver->hasConnection($name)) {
             throw new \Exception('No connection found named: ' . $name);
        }
        return $this->connection($name);
    }

}

class DoctrineDBAL implements ConnectionContainerInferface {

    public function __construct(array $connections) {}

    public function getConnectionByName() {}

}

Framework DataSource ConnectionManager

namespace PPI\DataSource;
class ConnectionManager {
    protected $connections;
    protected $libraryToConnMap;
    public function __construct($connections, $libraryToConnMap) {
        $this->libraryToConnMap = $libraryToConnMap;
        $this->connections = $connections;
    }

    public function getConnection($name) {
        $library = $this->connections[$name]['library'];
        $connContainer = $this->libraryToConnMap[$library]->getConnectionByName($name);
        return $connContainer;
    }


}

Framework Bootup Code

$connections = include 'app/config/datasource.php';
$allConnections = $laravelConns = array();
$libraryToConnMap = array();
foreach($connections as $name => $conn) {
    if($conn['library'] === 'laravel') {
        $laravelConns[$name] = $conn;
    }
    if($conn['library'] === 'doctrinedbal') {
        $doctrineDBALConns[$name] = $conn;
    }
}

$libraryToConnMap['laravel'] = new \PPI\DataSource\Container\Laravel($laravelConns);
$libraryToConnMap['doctrinedbal'] = new \PPI\DataSource\Container\DoctrineDBAL($doctrineDBALConns);
$connManager = new \PPI\DataSource\ConnectionManager($connections, $libraryToConnMap);

Userland Service Creation Code

'sessions.storage' => function($sm) {
    $connection = $sm->get('datasource')->getConnectionByName('sessions'));
    return new \Application\Storage\Sessions($connection);
}

Userland Storage Class Code

namespace Application\Storage;
class Sessions {
    protected $connection;
    public function __construct($connection) {
        $this->connection = $connection;
    }
    public function getAllRows() {
        return $this->connection->getAll();
    }    
}

Userland Datasource Config File

$conns = array();
$conns['default'] = array(
    'library'    => 'doctrine'
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',

);

$conns['sessions'] = array(
    'library' => 'laravel'
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'sessions',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
);

dragoonis added a commit that referenced this issue May 2, 2013
@dragoonis
Copy link
Member Author

This has now been completed by commit #ed0f85a5e8c3356d29c6623831a04e5f0174fbc1

@dragoonis
Copy link
Member Author

Re-Opening this until Documentation has been completed.

@dragoonis dragoonis reopened this May 2, 2013
@ghost ghost assigned dragoonis Jun 30, 2013
dragoonis added a commit that referenced this issue May 2, 2015
dragoonis added a commit that referenced this issue May 2, 2015
@vitorbrandao vitorbrandao modified the milestones: 2.1.1, 2.1.0 May 5, 2015
@vitorbrandao
Copy link
Member

Waiting for @dragoonis to provide documentation. Moved milestone from 2.1.0 to 2.1.1.

dragoonis added a commit that referenced this issue Aug 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants