Skip to content

Instantly share code, notes, and snippets.

@ClemensSahs
Created October 19, 2012 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClemensSahs/3917026 to your computer and use it in GitHub Desktop.
Save ClemensSahs/3917026 to your computer and use it in GitHub Desktop.
possible solution: Multiple DB connections with a service locator
namespace Zend\Db\Adapter;
use Zend\Db\Adapter\Exception\InvalidArgumentException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* @category Zend
* @package Zend_Db
* @subpackage Adapter
*/
abstract class AbstractAdapterServiceFactory implements FactoryInterface
{
/**
* Create db adapter service
*
* @param ServiceLocatorInterface $serviceLocator
* @return Adapter
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
$dbConfigKey = $this->getDbConfigKey();
if ( false === isset($config['db'][$dbConfigKey]) ) {
throw new \RuntimeException(sprintf("database config with key '%s' is not found", $dbConfigKey) );
}
return $this->factory($config['db'][$dbConfigKey],$serviceLocator);
}
/**
*
* @return string
*/
protected function getDbConfigKey()
{
return "default";
}
protected function factory($config,ServiceLocatorInterface $serviceLocator)
{
$driver = null;
$platform = null;
$queryResultPrototype = null;
if ( isset($config['driver']) ) {
if ( is_array($config['driver']) ) {
$driver = $config['driver'];
} elseif ( is_string($config['driver']) ) {
if( class_exists($config['driver']) ) {
$driver = new $config['driver']();
} else {
$driver = $serviceLocator->get($config['driver']);
}
if ( !is_object($platform) ) {
throw new InvalidArgumentException("database config['driver'] string is not a confirmed class/service name");
}
} else {
throw new InvalidArgumentException("database config['driver'] must be a array or string of class/service name");
}
}
if ( isset($config['platform']) ) {
if( class_exists($config['platform']) ) {
$platform = new $config['platform']();
} else {
$platform = $serviceLocator->get($config['platform']);
}
}
if ( !is_object($platform) ) {
$platform = null;
}
if ( isset($config['queryResultPrototype']) ) {
if( class_exists($config['queryResultPrototype']) ) {
$queryResultPrototype = new $config['queryResultPrototype']();
} else {
$queryResultPrototype = $serviceLocator->get($config['queryResultPrototype']);
}
}
if ( !is_object($queryResultPrototype) ) {
$queryResultPrototype = null;
}
return new Adapter($driver,$platform,$queryResultPrototype);
}
}
class BarAdapter extends AbstractAdapterServiceFactory
{
/**
*
* @return string
*/
protected function getDbConfigKey()
{
return 'bar';
}
}
class FooAdapter extends AbstractAdapterServiceFactory
{
/**
*
* @return string
*/
protected function getDbConfigKey()
{
return 'foo';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment