diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php new file mode 100644 index 00000000000..5f48e88de6f --- /dev/null +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -0,0 +1,297 @@ + $config) { + if ( $this->hasAdapter($key) ) { + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter with key(%s) is allready registered",$key)); + } elseif ( $this->hasAdapterConfig($key) ) { + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter config with key(%s) is allready defined",$key)); + } + + $this->_dbAdapterConfig[ $key ] = $config; + } + } + + /** + * @param ServiceLocatorInterface $serviceLocator + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->_serviceLocator = $serviceLocator; + } + + /** + * @return ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->_serviceLocator; + } + + /** + * @param array $config + * @throws DbAdapterManagerAdapterNotExist + */ + public function getAdapterConfig($adapterKey) + { + if ( !$this->hasAdapterConfig($adapterKey) ) { + throw new DbAdapterManagerAdapterNotExist(sprintf("adapter config with the key (%s) not exist",$adapterKey)); + } + return $this->_dbAdapterConfig[ $adapterKey ]; + } + + /** + * @param array $config + * @return bool + */ + public function hasAdapterConfig($adapterKey) + { + return ( isset($this->_dbAdapterConfig[ $adapterKey ]) ); + } + + /** + * @param string $key + * @param Adapter $adapter + * @throws DbAdapterManagerAdapterAlreadyRegistered + */ + public function addAdapter($key, Adapter $adapter) + { + if ( $this->hasAdapter($key) ) { + if ( $this->_dbAdapter[$key] === $adapter ) { + return true; + } + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); + } elseif ( $this->hasAdapterConfig($key) ) { + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); + } + + $this->_dbAdapter[ $key ] = $adapter; + } + + /** + * + * @param string $key + * @return bool + */ + public function hasAdapter($key) + { + return ( isset($this->_dbAdapter[$key]) ); + } + + /** + * @param string $key + * @throws DbAdapterManagerAdapterNotExist || DbAdapterManagerAdapterCoundInit + * @return Adapter + */ + public function getAdapter($key) + { + if ( !$this->hasAdapter($key) ) { + if ( !$this->hasAdapterConfig($key) ) { + throw new DbAdapterManagerAdapterNotExist(sprintf("adapter key (%s) not exist",$key)); + } + + try { + $this->initAdapter($key); + } catch (\Exception $exception) { + throw new DbAdapterManagerAdapterCoundInit(sprintf("adapter cound init for key (%s)",$key),0,$exception); + } + + if ( !$this->hasAdapter($key) ) { + throw new DbAdapterManagerAdapterCoundInit(sprintf("adapter cound init for key (%s)",$key)); + } + } + + return $this->_dbAdapter[ $key ]; + } + + /** + * @param string $key + * @throws DbAdapterManagerAdapterConfigNotValid + * @return Adapter + */ + protected function initAdapter($key) + { + $config = $this->_dbAdapterConfig[ $key ]; + + if ( is_string($config) ) { + $this->_dbAdapter[ $key ] = $this->getAdapter($config); + } elseif (!is_array($config) || + !array_key_exists('driver', $config) + ) { + throw new DbAdapterManagerAdapterConfigNotValid(sprintf("adapter config on key (%s) is not an valid key or array", $key)); + } else { + try { + $this->_dbAdapter[ $key ] = $this->adapterFactory( $config, $this->getServiceLocator() ); + } catch (Exception $exception) { + if ( $exception instanceof DbAdapterManagerAdapterCoundInit ) { + $previous = $exception->getPrevious(); + } else { + $previous = $exception; + } + throw new DbAdapterManagerAdapterCoundInit(sprintf("adapter cound init for key", $key),0,$previous ); + } + } + + return $this->_dbAdapter[ $key ]; + } + + /** + * return a platform object from a config + * + * @param array $config + * @param ServiceLocatorInterface $serviceLocator + * @return Platform || null + */ + protected function getPlatformObjectFromConfig ($config, ServiceLocatorInterface $serviceLocator=null) + { + if ( !isset($config['platform']) ) { + goto RETURN_NULL; + } + + if ( is_string ($config['platform']) ) { + if( class_exists($config['platform']) ) { + $platform = new $config['platform'](); + } else { + if ( $serviceLocator === null ) { + $serviceLocator = $this->getServiceLocator(); + } + $platform = $serviceLocator->get($config['platform']); + } + } else { + $platform = $config['platform']; + } + + if ( is_object($platform) ) { + goto RETURN_OBJECT; + } + + RETURN_NULL: + return null; + + RETURN_OBJECT: + return $platform; + } + + /** + * return a driver object from a config + * + * @param array $config + * @param ServiceLocatorInterface $serviceLocator + * @return Platform || null + */ + protected function getDriverObjectFromConfig ($config, ServiceLocatorInterface $serviceLocator=null) + { + if ( !isset($config['driver']) ) { + // @todo: throw a error or return null ? + goto RETURN_NULL; + } + + if ( is_array($config['driver']) || is_object($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($driver) ) { + throw new DbAdapterManagerAdapterConfigNotValid("database config['driver'] string is not a confirmed class/service name"); + } + } else { + throw new DbAdapterManagerAdapterConfigNotValid("database config['driver'] must be a array or string of class/service name"); + } + + goto RETURN_OBJECT; + + RETURN_NULL: + return null; + + RETURN_OBJECT: + return $driver; + } + + /** + * @param array $config + * @param ServiceLocatorInterface $serviceLocator + * @throws DbAdapterManagerAdapterConfigNotValid + * @return Adapter + */ + public function adapterFactory($config, ServiceLocatorInterface $serviceLocator=null) + { + if ( $serviceLocator === null ) { + $serviceLocator = $this->getServiceLocator(); + } + + $driver = null; + $platform = null; + $queryResultPrototype = null; + + $driver = $this->getDriverObjectFromConfig($config,$serviceLocator); + $platform = $this->getPlatformObjectFromConfig($config,$serviceLocator); + + 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; + } + + try { + $adapter = new Adapter($driver, + $platform, + $queryResultPrototype + ); + } catch (\Exception $exception) { + throw new DbAdapterManagerAdapterCoundInit("adapter cound init",0,$exception); + } + + return $adapter; + } +} diff --git a/library/Zend/Mvc/Service/DbAdapterManagerFactory.php b/library/Zend/Mvc/Service/DbAdapterManagerFactory.php new file mode 100644 index 00000000000..10f254ccfdb --- /dev/null +++ b/library/Zend/Mvc/Service/DbAdapterManagerFactory.php @@ -0,0 +1,38 @@ +get('Config'); + + $adapterService = new DbAdapterManager(); + $adapterService->addAdapterConfig($configArray['db_adapter_manager']); + + return $adapterService; + } +} diff --git a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAlreadyRegistered.php b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAlreadyRegistered.php new file mode 100644 index 00000000000..5a66e07acb0 --- /dev/null +++ b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAlreadyRegistered.php @@ -0,0 +1,7 @@ +mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); + + $this->sampleConfig = array( + 'sqlliteDb'=>array( + 'driver'=>array( + 'driver'=>'Pdo_Sqlite' + ), + 'platform'=>$this->mockPlatform + ), + 'mysqliDb'=>array( + 'driver'=>array( + 'driver'=>'Mysqli' + ), + 'platform'=>$this->mockPlatform + ), + 'sqlliteDb2'=>'sqlliteDb', + 'sqlliteDb3'=>'sqlliteDb2', + 'wrongAlias'=>'dev_null', + 'wrongConfig'=>array( + 'driver'=>array() + ), + ); + + } + + public function testAddAdapterConfig() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + foreach ( array_keys($this->sampleConfig) as $dbKey ) { + $this->assertTrue( $dam->hasAdapterConfig($dbKey) ); + } + } + + public function testAddAdapter() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + if (extension_loaded('pdo')) { + $adapter = $dam->getAdapter('sqlliteDb'); + + $dam->addAdapter('sqlliteDb_new', $adapter); + + $this->assertTrue( $dam->hasAdapter('sqlliteDb_new') ); + $this->assertSame( $dam->getAdapter('sqlliteDb_new'), $dam->getAdapter('sqlliteDb') ); + unset($adapter); + } + } + + public function testInitDbAdapter() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + if (extension_loaded('pdo')) { + $adapter = $dam->getAdapter('sqlliteDb'); + $this->assertInstanceOf('Zend\Db\Adapter\Driver\Pdo\Pdo', $adapter->driver); + + $this->assertTrue( $dam->hasAdapter('sqlliteDb') ); + unset($adapter); + } + + if (extension_loaded('mysqli')) { + $adapter = $dam->getAdapter('mysqliDb'); + $this->assertInstanceOf('Zend\Db\Adapter\Driver\Mysqli\Mysqli', $adapter->driver); + + $this->assertTrue( $dam->hasAdapter('mysqliDb') ); + unset($adapter); + } + } + + public function testInitDbAdapterAlias() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + if (extension_loaded('pdo')) { + $adapter1 = $dam->getAdapter('sqlliteDb'); + $adapter2 = $dam->getAdapter('sqlliteDb2'); + $adapter3 = $dam->getAdapter('sqlliteDb3'); + + $this->assertSame($adapter1, $adapter2); + $this->assertSame($adapter1, $adapter3); + unset($adapter); + } + } + + /** + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist + */ + public function testAdapterNotExist() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + $dam->getAdapter( 'dev_null' ); + } + + /** + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit + */ + public function testWrongAlias() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + $dam->getAdapter( 'wrongAlias' ); + } + + /** + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit + */ + public function testWrongConfig() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + $dam->getAdapter( 'wrongConfig' ); + } + + /** + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAlreadyRegistered + */ + public function testAdapterDuplicatKey() + { + $dam = new DbAdapterManager(); + $dam->addAdapterConfig( $this->sampleConfig ); + + $adapter = $dam->getAdapter('sqlliteDb'); + $dam->addAdapter('mysqliDb',$adapter); + } +}