Created
October 19, 2012 08:50
-
-
Save ClemensSahs/3917026 to your computer and use it in GitHub Desktop.
possible solution: Multiple DB connections with a service locator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BarAdapter extends AbstractAdapterServiceFactory | |
{ | |
/** | |
* | |
* @return string | |
*/ | |
protected function getDbConfigKey() | |
{ | |
return 'bar'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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