From a9b31767b2a894c2aea8a00716a6a01c338e9d11 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 6 Nov 2012 12:05:25 +0100 Subject: [PATCH 01/19] add AdapterManager in to Zend\Db\Adapter namespace --- library/Zend/Db/Adapter/AdapterManager.php | 218 ++++++++++++++++++ .../Zend/Db/Adapter/AdapterManagerFactory.php | 39 ++++ 2 files changed, 257 insertions(+) create mode 100644 library/Zend/Db/Adapter/AdapterManager.php create mode 100644 library/Zend/Db/Adapter/AdapterManagerFactory.php diff --git a/library/Zend/Db/Adapter/AdapterManager.php b/library/Zend/Db/Adapter/AdapterManager.php new file mode 100644 index 00000000000..64ebdca1585 --- /dev/null +++ b/library/Zend/Db/Adapter/AdapterManager.php @@ -0,0 +1,218 @@ +$config) { + if ( $this->hasAdapter($key) ) { + throw new \RuntimeException(sprintf("adapter with key(%s) is allready registered",$key)); + } elseif ( $this->hasAdapterConfig($key) ) { + throw new \RuntimeException(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 \RuntimeException + */ + public function hasAdapterConfig ($adapterKey) + { + return ( isset($this->_dbAdapterConfig[ $adapterKey ]) ); + } + + /** + * + * @param string $key + * @param Adapter $adapter + * @throws \RuntimeException + * @return boolean + */ + public function addAdapter ($key, Adapter $adapter) + { + if ( $this->hasAdapter($key) ) { + if ( $this->_dbAdapter[$key] === $adapter ) { + return true; + } + throw new \RuntimeException(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 \RuntimeException + * @return Adapter + */ + public function getAdapter ($key) + { + if ( !$this->hasAdapter($key) ) { + if ( !$this->hasAdapterConfig($key) ) { + throw new \RuntimeException(sprintf("adapter key (%s) not exist",$key)); + } + + $this->initAdapter($key); + if ( !$this->hasAdapter($key) ) { + throw new \RuntimeException(sprintf("adapter cound init for key (%s)",$key)); + } + } + + return $this->_dbAdapter[ $key ]; + } + + /** + * + * @param string $key + * @throws \RuntimeException + * @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 \RuntimeException("adapter config on key (%s) is not an valid key or array"); + } else { + $this->_dbAdapter[ $key ] = $this->factoryAdapter( $config, $this->getServiceLocator() ); + } + + return $this->_dbAdapter[ $key ]; + } + + /** + * + * @param array $config + * @param ServiceLocatorInterface $serviceLocator + * @throws InvalidArgumentException + * @return Adapter + */ + public function factoryAdapter($config, ServiceLocatorInterface $serviceLocator=null) + { + if ( $serviceLocator === null ) { + $serviceLocator = $this->getServiceLocator(); + } + + $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 + ); + } +} \ No newline at end of file diff --git a/library/Zend/Db/Adapter/AdapterManagerFactory.php b/library/Zend/Db/Adapter/AdapterManagerFactory.php new file mode 100644 index 00000000000..697277e60e5 --- /dev/null +++ b/library/Zend/Db/Adapter/AdapterManagerFactory.php @@ -0,0 +1,39 @@ +get('Config'); + + $adapterService = new AdapterManager(); + $adapterService->addDbAdapterConfig($configArray['db']); + + return $adapterService; + } +} \ No newline at end of file From 52ef22c43cf2030777023b67b6e021afc4300807 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 6 Nov 2012 13:50:08 +0100 Subject: [PATCH 02/19] fix cs --- library/Zend/Db/Adapter/AdapterManager.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/library/Zend/Db/Adapter/AdapterManager.php b/library/Zend/Db/Adapter/AdapterManager.php index 64ebdca1585..7b1386f5ec5 100644 --- a/library/Zend/Db/Adapter/AdapterManager.php +++ b/library/Zend/Db/Adapter/AdapterManager.php @@ -26,13 +26,11 @@ class AdapterManager protected $_dbAdapterConfig = array(); /** - * * @var ServiceLocatorInterface */ protected $_serviceLocator; /** - * * @param array $config * @throws \RuntimeException */ @@ -50,7 +48,6 @@ public function addDbAdapterConfig (array $configArray) } /** - * * @param ServiceLocatorInterface $serviceLocator */ public function setServiceLocator( ServiceLocatorInterface $serviceLocator) @@ -59,7 +56,6 @@ public function setServiceLocator( ServiceLocatorInterface $serviceLocator) } /** - * * @return ServiceLocatorInterface */ public function getServiceLocator() @@ -68,7 +64,6 @@ public function getServiceLocator() } /** - * * @param array $config * @throws \RuntimeException */ @@ -78,7 +73,6 @@ public function hasAdapterConfig ($adapterKey) } /** - * * @param string $key * @param Adapter $adapter * @throws \RuntimeException @@ -107,7 +101,6 @@ public function hasAdapter ($key) } /** - * * @param string $key * @throws \RuntimeException * @return Adapter @@ -129,7 +122,6 @@ public function getAdapter ($key) } /** - * * @param string $key * @throws \RuntimeException * @return Adapter @@ -152,7 +144,6 @@ protected function initAdapter ($key) } /** - * * @param array $config * @param ServiceLocatorInterface $serviceLocator * @throws InvalidArgumentException From 7a161fccebe1b7c1a0150370412d3316f95204a7 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 8 Nov 2012 15:08:56 +0100 Subject: [PATCH 03/19] cs fix agin --- library/Zend/Db/Adapter/AdapterManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Zend/Db/Adapter/AdapterManager.php b/library/Zend/Db/Adapter/AdapterManager.php index 7b1386f5ec5..0dd83bd0380 100644 --- a/library/Zend/Db/Adapter/AdapterManager.php +++ b/library/Zend/Db/Adapter/AdapterManager.php @@ -18,7 +18,7 @@ class AdapterManager implements ServiceLocatorAwareInterface { /** - * + * * @var Adapter[] */ protected $_dbAdapter = array(); @@ -91,7 +91,7 @@ public function addAdapter ($key, Adapter $adapter) } /** - * + * * @param string $key * @return bool */ From 3d69677405825ea6d5bd24d1fd2a27d4ad98fcf4 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 8 Nov 2012 15:38:07 +0100 Subject: [PATCH 04/19] eof line cs fix --- library/Zend/Db/Adapter/AdapterManager.php | 2 +- library/Zend/Db/Adapter/AdapterManagerFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Zend/Db/Adapter/AdapterManager.php b/library/Zend/Db/Adapter/AdapterManager.php index 0dd83bd0380..3a0d156d0f2 100644 --- a/library/Zend/Db/Adapter/AdapterManager.php +++ b/library/Zend/Db/Adapter/AdapterManager.php @@ -206,4 +206,4 @@ public function factoryAdapter($config, ServiceLocatorInterface $serviceLocator= $queryResultPrototype ); } -} \ No newline at end of file +} diff --git a/library/Zend/Db/Adapter/AdapterManagerFactory.php b/library/Zend/Db/Adapter/AdapterManagerFactory.php index 697277e60e5..4a192fab8f5 100644 --- a/library/Zend/Db/Adapter/AdapterManagerFactory.php +++ b/library/Zend/Db/Adapter/AdapterManagerFactory.php @@ -36,4 +36,4 @@ public function createService(ServiceLocatorInterface $serviceLocator) return $adapterService; } -} \ No newline at end of file +} From 4a6a63b5c5db7fb765cb2826180e61c61820f046 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Mon, 21 Jan 2013 11:32:41 +0100 Subject: [PATCH 05/19] - move in to Zend/Mvc/Service - patch some coding standards - rename config key from 'db' to 'db_adapter_manager' --- .../Service/DbAdapterManager.php} | 49 ++++++++++--------- .../Service/DbAdapterManagerFactory.php} | 7 ++- 2 files changed, 28 insertions(+), 28 deletions(-) rename library/Zend/{Db/Adapter/AdapterManager.php => Mvc/Service/DbAdapterManager.php} (76%) rename library/Zend/{Db/Adapter/AdapterManagerFactory.php => Mvc/Service/DbAdapterManagerFactory.php} (83%) diff --git a/library/Zend/Db/Adapter/AdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php similarity index 76% rename from library/Zend/Db/Adapter/AdapterManager.php rename to library/Zend/Mvc/Service/DbAdapterManager.php index 3a0d156d0f2..9558bc5bfea 100644 --- a/library/Zend/Db/Adapter/AdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -8,14 +8,15 @@ * @package Zend_Db */ -namespace Zend\Db\Adapter; +namespace Zend\Mvc\Service; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\Db\Adapter\Adapter; +use Zend\Mvc\Exception\RuntimeException; +use Zend\Mvc\Exception\InvalidArgumentException; -class AdapterManager - implements ServiceLocatorAwareInterface +class DbAdapterManager implements ServiceLocatorAwareInterface { /** * @@ -32,15 +33,15 @@ class AdapterManager /** * @param array $config - * @throws \RuntimeException + * @throws RuntimeException */ - public function addDbAdapterConfig (array $configArray) + public function addDbAdapterConfig(array $configArray) { - foreach ( $configArray as $key=>$config) { + foreach ($configArray as $key => $config) { if ( $this->hasAdapter($key) ) { - throw new \RuntimeException(sprintf("adapter with key(%s) is allready registered",$key)); + throw new RuntimeException(sprintf("adapter with key(%s) is allready registered",$key)); } elseif ( $this->hasAdapterConfig($key) ) { - throw new \RuntimeException(sprintf("adapter config with key(%s) is allready defined",$key)); + throw new RuntimeException(sprintf("adapter config with key(%s) is allready defined",$key)); } $this->_dbAdapterConfig[ $key ] = $config; @@ -50,7 +51,7 @@ public function addDbAdapterConfig (array $configArray) /** * @param ServiceLocatorInterface $serviceLocator */ - public function setServiceLocator( ServiceLocatorInterface $serviceLocator) + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->_serviceLocator = $serviceLocator; } @@ -65,9 +66,9 @@ public function getServiceLocator() /** * @param array $config - * @throws \RuntimeException + * @throws RuntimeException */ - public function hasAdapterConfig ($adapterKey) + public function hasAdapterConfig($adapterKey) { return ( isset($this->_dbAdapterConfig[ $adapterKey ]) ); } @@ -75,16 +76,16 @@ public function hasAdapterConfig ($adapterKey) /** * @param string $key * @param Adapter $adapter - * @throws \RuntimeException + * @throws RuntimeException * @return boolean */ - public function addAdapter ($key, Adapter $adapter) + public function addAdapter($key, Adapter $adapter) { if ( $this->hasAdapter($key) ) { if ( $this->_dbAdapter[$key] === $adapter ) { return true; } - throw new \RuntimeException(sprintf("adapter key (%s) allready exist",$key)); + throw new RuntimeException(sprintf("adapter key (%s) allready exist",$key)); } $this->_dbAdapter[ $key ] = $adapter; @@ -95,26 +96,26 @@ public function addAdapter ($key, Adapter $adapter) * @param string $key * @return bool */ - public function hasAdapter ($key) + public function hasAdapter($key) { return ( isset($this->_dbAdapter[$key]) ); } /** * @param string $key - * @throws \RuntimeException + * @throws RuntimeException * @return Adapter */ - public function getAdapter ($key) + public function getAdapter($key) { if ( !$this->hasAdapter($key) ) { if ( !$this->hasAdapterConfig($key) ) { - throw new \RuntimeException(sprintf("adapter key (%s) not exist",$key)); + throw new RuntimeException(sprintf("adapter key (%s) not exist",$key)); } $this->initAdapter($key); if ( !$this->hasAdapter($key) ) { - throw new \RuntimeException(sprintf("adapter cound init for key (%s)",$key)); + throw new RuntimeException(sprintf("adapter cound init for key (%s)",$key)); } } @@ -123,10 +124,10 @@ public function getAdapter ($key) /** * @param string $key - * @throws \RuntimeException + * @throws RuntimeException * @return Adapter */ - protected function initAdapter ($key) + protected function initAdapter($key) { $config = $this->_dbAdapterConfig[ $key ]; @@ -135,9 +136,9 @@ protected function initAdapter ($key) } elseif (!is_array($config) || !array_key_exists('driver', $config) ) { - throw new \RuntimeException("adapter config on key (%s) is not an valid key or array"); + throw new RuntimeException("adapter config on key (%s) is not an valid key or array"); } else { - $this->_dbAdapter[ $key ] = $this->factoryAdapter( $config, $this->getServiceLocator() ); + $this->_dbAdapter[ $key ] = $this->adapterFactory( $config, $this->getServiceLocator() ); } return $this->_dbAdapter[ $key ]; @@ -149,7 +150,7 @@ protected function initAdapter ($key) * @throws InvalidArgumentException * @return Adapter */ - public function factoryAdapter($config, ServiceLocatorInterface $serviceLocator=null) + public function adapterFactory($config, ServiceLocatorInterface $serviceLocator=null) { if ( $serviceLocator === null ) { $serviceLocator = $this->getServiceLocator(); diff --git a/library/Zend/Db/Adapter/AdapterManagerFactory.php b/library/Zend/Mvc/Service/DbAdapterManagerFactory.php similarity index 83% rename from library/Zend/Db/Adapter/AdapterManagerFactory.php rename to library/Zend/Mvc/Service/DbAdapterManagerFactory.php index 4a192fab8f5..b25530c788e 100644 --- a/library/Zend/Db/Adapter/AdapterManagerFactory.php +++ b/library/Zend/Mvc/Service/DbAdapterManagerFactory.php @@ -8,7 +8,7 @@ * @package Zend_Db */ -namespace Zend\Db\Adapter; +namespace Zend\Mvc\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -18,8 +18,7 @@ * @package Zend_Db * @subpackage Adapter */ -class AdapterManagerFactory - implements FactoryInterface +class DbAdapterManagerFactory implements FactoryInterface { /** * Create db adapter service @@ -32,7 +31,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) $configArray = $serviceLocator->get('Config'); $adapterService = new AdapterManager(); - $adapterService->addDbAdapterConfig($configArray['db']); + $adapterService->addDbAdapterConfig($configArray['db_adapter_manager']); return $adapterService; } From d7f1b3aa0ea128ebe0b5c1ac6482ef3bebbfd8e7 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Mon, 21 Jan 2013 18:33:45 +0100 Subject: [PATCH 06/19] add exceptions --- ...dapterManagerAdapterAllreadyRegistered.php | 7 +++++++ .../DbAdapterManagerAdapterConfigNotValid.php | 7 +++++++ .../DbAdapterManagerAdapterCoundInit.php | 7 +++++++ .../DbAdapterManagerAdapterNotExist.php | 7 +++++++ .../Service/Exception/ExceptionInterface.php | 20 +++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php create mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php create mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterCoundInit.php create mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterNotExist.php create mode 100644 library/Zend/Mvc/Service/Exception/ExceptionInterface.php diff --git a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php new file mode 100644 index 00000000000..1e74ed774f3 --- /dev/null +++ b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php @@ -0,0 +1,7 @@ + Date: Mon, 21 Jan 2013 18:34:28 +0100 Subject: [PATCH 07/19] add exceptions add getAdapterConfig --- library/Zend/Mvc/Service/DbAdapterManager.php | 77 ++++++++++++++----- .../Mvc/Service/DbAdapterManagerFactory.php | 4 +- 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index 9558bc5bfea..5d74a9a7882 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -13,8 +13,12 @@ use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\Db\Adapter\Adapter; -use Zend\Mvc\Exception\RuntimeException; -use Zend\Mvc\Exception\InvalidArgumentException; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAllreadyRegistered; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotVaild; + +use Exception; class DbAdapterManager implements ServiceLocatorAwareInterface { @@ -33,15 +37,15 @@ class DbAdapterManager implements ServiceLocatorAwareInterface /** * @param array $config - * @throws RuntimeException + * @throws DbAdapterManagerAdapterAllreadyRegistered */ - public function addDbAdapterConfig(array $configArray) + public function addAdapterConfig(array $configArray) { foreach ($configArray as $key => $config) { if ( $this->hasAdapter($key) ) { - throw new RuntimeException(sprintf("adapter with key(%s) is allready registered",$key)); + throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter with key(%s) is allready registered",$key)); } elseif ( $this->hasAdapterConfig($key) ) { - throw new RuntimeException(sprintf("adapter config with key(%s) is allready defined",$key)); + throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter config with key(%s) is allready defined",$key)); } $this->_dbAdapterConfig[ $key ] = $config; @@ -66,7 +70,19 @@ public function getServiceLocator() /** * @param array $config - * @throws RuntimeException + * @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) { @@ -76,8 +92,7 @@ public function hasAdapterConfig($adapterKey) /** * @param string $key * @param Adapter $adapter - * @throws RuntimeException - * @return boolean + * @throws DbAdapterManagerAdapterAllreadyRegistered */ public function addAdapter($key, Adapter $adapter) { @@ -85,7 +100,7 @@ public function addAdapter($key, Adapter $adapter) if ( $this->_dbAdapter[$key] === $adapter ) { return true; } - throw new RuntimeException(sprintf("adapter key (%s) allready exist",$key)); + throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); } $this->_dbAdapter[ $key ] = $adapter; @@ -103,19 +118,24 @@ public function hasAdapter($key) /** * @param string $key - * @throws RuntimeException + * @throws DbAdapterManagerAdapterNotExist || DbAdapterManagerAdapterCoundInit * @return Adapter */ public function getAdapter($key) { if ( !$this->hasAdapter($key) ) { if ( !$this->hasAdapterConfig($key) ) { - throw new RuntimeException(sprintf("adapter key (%s) not exist",$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); } - $this->initAdapter($key); if ( !$this->hasAdapter($key) ) { - throw new RuntimeException(sprintf("adapter cound init for key (%s)",$key)); + throw new DbAdapterManagerAdapterCoundInit(sprintf("adapter cound init for key (%s)",$key)); } } @@ -124,7 +144,7 @@ public function getAdapter($key) /** * @param string $key - * @throws RuntimeException + * @throws DbAdapterManagerAdapterConfigNotVaild * @return Adapter */ protected function initAdapter($key) @@ -136,9 +156,18 @@ protected function initAdapter($key) } elseif (!is_array($config) || !array_key_exists('driver', $config) ) { - throw new RuntimeException("adapter config on key (%s) is not an valid key or array"); + throw new DbAdapterManagerAdapterConfigNotVaild(sprintf("adapter config on key (%s) is not an valid key or array", $key)); } else { - $this->_dbAdapter[ $key ] = $this->adapterFactory( $config, $this->getServiceLocator() ); + 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 ]; @@ -147,7 +176,7 @@ protected function initAdapter($key) /** * @param array $config * @param ServiceLocatorInterface $serviceLocator - * @throws InvalidArgumentException + * @throws DbAdapterManagerAdapterConfigNotVaild * @return Adapter */ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator=null) @@ -171,10 +200,10 @@ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator= } if ( !is_object($platform) ) { - throw new InvalidArgumentException("database config['driver'] string is not a confirmed class/service name"); + throw new DbAdapterManagerAdapterConfigNotVaild("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"); + throw new DbAdapterManagerAdapterConfigNotVaild("database config['driver'] must be a array or string of class/service name"); } } @@ -202,9 +231,15 @@ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator= $queryResultPrototype = null; } - return new Adapter($driver, + 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 index b25530c788e..10f254ccfdb 100644 --- a/library/Zend/Mvc/Service/DbAdapterManagerFactory.php +++ b/library/Zend/Mvc/Service/DbAdapterManagerFactory.php @@ -30,8 +30,8 @@ public function createService(ServiceLocatorInterface $serviceLocator) { $configArray = $serviceLocator->get('Config'); - $adapterService = new AdapterManager(); - $adapterService->addDbAdapterConfig($configArray['db_adapter_manager']); + $adapterService = new DbAdapterManager(); + $adapterService->addAdapterConfig($configArray['db_adapter_manager']); return $adapterService; } From 657c5b852246c745e5a5153df4145f81319208f1 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Mon, 21 Jan 2013 18:34:38 +0100 Subject: [PATCH 08/19] add unitTest --- .../Mvc/Service/DbAdapterManagerTest.php | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php new file mode 100644 index 00000000000..a30f11f8ff8 --- /dev/null +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -0,0 +1,152 @@ +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', + 'wrongAliase'=>'dev_null', + 'wrongConfig'=>array( + 'driver'=>array() + ), + ); + + } + + public function testAddAdapterConfig() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $this->sampleConfig ); + + foreach ( array_keys($this->sampleConfig) as $dbKey ) { + $this->assertTrue( $dam->hasAdapterConfig($dbKey) ); + } + } + + public function testAddAdapter() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $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->addDbAdapterConfig( $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 testInitDbAdapterAliase() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $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 DbAdapterManagerAdapterNotExist + */ + public function testWrongAliase() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $this->sampleConfig ); + + $dam->getAdapter( 'wrongAliase' ); + } + + /** + * @expectedException DbAdapterManagerAdapterCoundInit + */ + public function testWrongConfig() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $this->sampleConfig ); + + $dam->getAdapter( 'wrongConfig' ); + } + + /** + * @expectedException DbAdapterManagerAdapterAllreadyRegistered + */ + public function testAdapterDuplicatKey() + { + $dam = new DbAdapterManager(); + $dam->addDbAdapterConfig( $this->sampleConfig ); + + $adapter = $dam->getAdapter('sqlliteDb'); + $dam->getAdapter('sqlliteDb',$adapter); + } +} From 72a9eec1ce7d65761134e7a693f03d17ec54f7cf Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Mon, 21 Jan 2013 20:34:03 +0100 Subject: [PATCH 09/19] -change renamed methode "addDbAdapterConfig" -clean lines with ending space --- .../Mvc/Service/DbAdapterManagerTest.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index a30f11f8ff8..5205719dbed 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -27,8 +27,7 @@ class DbAdapterManagerTest extends TestCase public function setUp() { $this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); - - + $this->sampleConfig = array( 'sqlliteDb'=>array( 'driver'=>array( @@ -55,7 +54,7 @@ public function setUp() public function testAddAdapterConfig() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); foreach ( array_keys($this->sampleConfig) as $dbKey ) { $this->assertTrue( $dam->hasAdapterConfig($dbKey) ); @@ -65,7 +64,7 @@ public function testAddAdapterConfig() public function testAddAdapter() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); if (extension_loaded('pdo')) { $adapter = $dam->getAdapter('sqlliteDb'); @@ -81,12 +80,12 @@ public function testAddAdapter() public function testInitDbAdapter() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $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); } @@ -103,7 +102,7 @@ public function testInitDbAdapter() public function testInitDbAdapterAliase() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); if (extension_loaded('pdo')) { $adapter1 = $dam->getAdapter('sqlliteDb'); @@ -122,7 +121,7 @@ public function testInitDbAdapterAliase() public function testWrongAliase() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); $dam->getAdapter( 'wrongAliase' ); } @@ -133,7 +132,7 @@ public function testWrongAliase() public function testWrongConfig() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); $dam->getAdapter( 'wrongConfig' ); } @@ -144,7 +143,7 @@ public function testWrongConfig() public function testAdapterDuplicatKey() { $dam = new DbAdapterManager(); - $dam->addDbAdapterConfig( $this->sampleConfig ); + $dam->addAdapterConfig( $this->sampleConfig ); $adapter = $dam->getAdapter('sqlliteDb'); $dam->getAdapter('sqlliteDb',$adapter); From d2d1514c3f6288734fcfcb7f74e39502f32827d9 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 11:09:18 +0100 Subject: [PATCH 10/19] move logic "get Platform object" in to a seperate methode --- library/Zend/Mvc/Service/DbAdapterManager.php | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index 5d74a9a7882..dd11970a626 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -10,14 +10,15 @@ namespace Zend\Mvc\Service; + use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\Db\Adapter\Adapter; +use Zend\Db\Sql\Platform\Platform; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAllreadyRegistered; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotVaild; - use Exception; class DbAdapterManager implements ServiceLocatorAwareInterface @@ -173,6 +174,43 @@ protected function initAdapter($key) 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; + } + /** * @param array $config * @param ServiceLocatorInterface $serviceLocator @@ -207,17 +245,7 @@ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator= } } - 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; - } + $platform = $this->getPlatformObejctFromConfig($config,$serviceLocator); if ( isset($config['queryResultPrototype']) ) { if( class_exists($config['queryResultPrototype']) ) { From a48cfb0f9ddda7c28ac03c49c2c677d19b774d29 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 11:22:48 +0100 Subject: [PATCH 11/19] - move logic "get driver object" in to a seperate methode - fix a typo --- library/Zend/Mvc/Service/DbAdapterManager.php | 60 +++++++++++++------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index dd11970a626..c3c711deede 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -211,6 +211,45 @@ protected function getPlatformObjectFromConfig ($config, ServiceLocatorInterface 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 DbAdapterManagerAdapterConfigNotVaild("database config['driver'] string is not a confirmed class/service name"); + } + } else { + throw new DbAdapterManagerAdapterConfigNotVaild("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 @@ -227,25 +266,8 @@ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator= $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 DbAdapterManagerAdapterConfigNotVaild("database config['driver'] string is not a confirmed class/service name"); - } - } else { - throw new DbAdapterManagerAdapterConfigNotVaild("database config['driver'] must be a array or string of class/service name"); - } - } - - $platform = $this->getPlatformObejctFromConfig($config,$serviceLocator); + $driver = $this->getDriverObjectFromConfig($config,$serviceLocator); + $platform = $this->getPlatformObjectFromConfig($config,$serviceLocator); if ( isset($config['queryResultPrototype']) ) { if( class_exists($config['queryResultPrototype']) ) { From 885eb806453b36d83845ec92e16674b1e014f73c Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 11:27:32 +0100 Subject: [PATCH 12/19] expectedException with full class name --- tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index 5205719dbed..2c71ff2a8c2 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -13,7 +13,6 @@ use PHPUnit_Framework_TestCase as TestCase; use Zend\Mvc\Service\DbAdapterManager; -use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAllreadyRegistered; class DbAdapterManagerTest extends TestCase { @@ -116,7 +115,7 @@ public function testInitDbAdapterAliase() } /** - * @expectedException DbAdapterManagerAdapterNotExist + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist */ public function testWrongAliase() { @@ -127,7 +126,7 @@ public function testWrongAliase() } /** - * @expectedException DbAdapterManagerAdapterCoundInit + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit */ public function testWrongConfig() { @@ -138,7 +137,7 @@ public function testWrongConfig() } /** - * @expectedException DbAdapterManagerAdapterAllreadyRegistered + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAllreadyRegistered */ public function testAdapterDuplicatKey() { From d0787df4adcfffea28499cae91abc7c4a97f4e5e Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 14:45:28 +0100 Subject: [PATCH 13/19] repair unit test - fix typo - change the expected Exception for "testWrongAlias" --- tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index 2c71ff2a8c2..4633192cf20 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -42,7 +42,7 @@ public function setUp() ), 'sqlliteDb2'=>'sqlliteDb', 'sqlliteDb3'=>'sqlliteDb2', - 'wrongAliase'=>'dev_null', + 'wrongAlias'=>'dev_null', 'wrongConfig'=>array( 'driver'=>array() ), @@ -98,7 +98,7 @@ public function testInitDbAdapter() } } - public function testInitDbAdapterAliase() + public function testInitDbAdapterAlias() { $dam = new DbAdapterManager(); $dam->addAdapterConfig( $this->sampleConfig ); @@ -115,14 +115,14 @@ public function testInitDbAdapterAliase() } /** - * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist + * @expectedException Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit */ - public function testWrongAliase() + public function testWrongAlias() { $dam = new DbAdapterManager(); $dam->addAdapterConfig( $this->sampleConfig ); - $dam->getAdapter( 'wrongAliase' ); + $dam->getAdapter( 'wrongAlias' ); } /** From 5fc361aba799290d35eac07b6ce48e9f96ab485e Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 14:47:13 +0100 Subject: [PATCH 14/19] add test for not exist adapter --- tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index 4633192cf20..f697d90a997 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -114,6 +114,17 @@ public function testInitDbAdapterAlias() } } + /** + * @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 */ From 0d9582452d83e77160391553ddd28e8351087f05 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 15:21:08 +0100 Subject: [PATCH 15/19] fix testAdapterDuplicatKey --- tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index f697d90a997..497c1ccc71a 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -156,6 +156,6 @@ public function testAdapterDuplicatKey() $dam->addAdapterConfig( $this->sampleConfig ); $adapter = $dam->getAdapter('sqlliteDb'); - $dam->getAdapter('sqlliteDb',$adapter); + $dam->getAdapter('mysqliDb',$adapter); } } From 2326fad540da038dd17bac5f5cdbbce4cd76b2c7 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Tue, 22 Jan 2013 17:00:05 +0100 Subject: [PATCH 16/19] fix unit Test - fix issue find by "testAdapterDuplicatKey" - fix typo --- library/Zend/Mvc/Service/DbAdapterManager.php | 15 +++++++++------ .../DbAdapterManagerAdapterAllreadyRegistered.php | 7 ------- .../DbAdapterManagerAdapterAlreadyRegistered.php | 7 +++++++ .../ZendTest/Mvc/Service/DbAdapterManagerTest.php | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) delete mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php create mode 100644 library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAlreadyRegistered.php diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index c3c711deede..454cacb10d8 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -15,7 +15,7 @@ use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\Db\Adapter\Adapter; use Zend\Db\Sql\Platform\Platform; -use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAllreadyRegistered; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAlreadyRegistered; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotVaild; @@ -38,15 +38,15 @@ class DbAdapterManager implements ServiceLocatorAwareInterface /** * @param array $config - * @throws DbAdapterManagerAdapterAllreadyRegistered + * @throws DbAdapterManagerAdapterAlreadyRegistered */ public function addAdapterConfig(array $configArray) { foreach ($configArray as $key => $config) { if ( $this->hasAdapter($key) ) { - throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter with key(%s) is allready registered",$key)); + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter with key(%s) is allready registered",$key)); } elseif ( $this->hasAdapterConfig($key) ) { - throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter config with key(%s) is allready defined",$key)); + throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter config with key(%s) is allready defined",$key)); } $this->_dbAdapterConfig[ $key ] = $config; @@ -93,7 +93,7 @@ public function hasAdapterConfig($adapterKey) /** * @param string $key * @param Adapter $adapter - * @throws DbAdapterManagerAdapterAllreadyRegistered + * @throws DbAdapterManagerAdapterAlreadyRegistered */ public function addAdapter($key, Adapter $adapter) { @@ -101,7 +101,10 @@ public function addAdapter($key, Adapter $adapter) if ( $this->_dbAdapter[$key] === $adapter ) { return true; } - throw new DbAdapterManagerAdapterAllreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); + 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; diff --git a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php deleted file mode 100644 index 1e74ed774f3..00000000000 --- a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAllreadyRegistered.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Wed, 23 Jan 2013 00:38:00 +0100 Subject: [PATCH 17/19] fix not throwed exception in "testAdapterDuplicatKey" --- tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index 8fe07c96ef7..7795ade112f 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -156,6 +156,6 @@ public function testAdapterDuplicatKey() $dam->addAdapterConfig( $this->sampleConfig ); $adapter = $dam->getAdapter('sqlliteDb'); - $dam->getAdapter('mysqliDb',$adapter); + $dam->addAdapter('mysqliDb',$adapter); } } From c6974129f986e48b9c3ba0d15a6f86eca184377d Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Wed, 23 Jan 2013 01:20:50 +0100 Subject: [PATCH 18/19] fix cs - clean tailing spaces - fix typo --- library/Zend/Mvc/Service/DbAdapterManager.php | 14 +++++++------- .../DbAdapterManagerAdapterConfigNotValid.php | 2 +- .../ZendTest/Mvc/Service/DbAdapterManagerTest.php | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index 454cacb10d8..ab58e4811b4 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -18,7 +18,7 @@ use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterAlreadyRegistered; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit; use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist; -use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotVaild; +use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotValid; use Exception; class DbAdapterManager implements ServiceLocatorAwareInterface @@ -148,7 +148,7 @@ public function getAdapter($key) /** * @param string $key - * @throws DbAdapterManagerAdapterConfigNotVaild + * @throws DbAdapterManagerAdapterConfigNotValid * @return Adapter */ protected function initAdapter($key) @@ -160,7 +160,7 @@ protected function initAdapter($key) } elseif (!is_array($config) || !array_key_exists('driver', $config) ) { - throw new DbAdapterManagerAdapterConfigNotVaild(sprintf("adapter config on key (%s) is not an valid key or array", $key)); + 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() ); @@ -238,10 +238,10 @@ protected function getDriverObjectFromConfig ($config, ServiceLocatorInterface $ } if ( !is_object($driver) ) { - throw new DbAdapterManagerAdapterConfigNotVaild("database config['driver'] string is not a confirmed class/service name"); + throw new DbAdapterManagerAdapterConfigNotValid("database config['driver'] string is not a confirmed class/service name"); } } else { - throw new DbAdapterManagerAdapterConfigNotVaild("database config['driver'] must be a array or string of class/service name"); + throw new DbAdapterManagerAdapterConfigNotValid("database config['driver'] must be a array or string of class/service name"); } goto RETURN_OBJECT; @@ -256,7 +256,7 @@ protected function getDriverObjectFromConfig ($config, ServiceLocatorInterface $ /** * @param array $config * @param ServiceLocatorInterface $serviceLocator - * @throws DbAdapterManagerAdapterConfigNotVaild + * @throws DbAdapterManagerAdapterConfigNotValid * @return Adapter */ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator=null) @@ -292,7 +292,7 @@ public function adapterFactory($config, ServiceLocatorInterface $serviceLocator= } catch (\Exception $exception) { throw new DbAdapterManagerAdapterCoundInit("adapter cound init",0,$exception); } - + return $adapter; } } diff --git a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php index 6cb096b44fe..baf1615e9bf 100644 --- a/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php +++ b/library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php @@ -2,6 +2,6 @@ namespace Zend\Mvc\Service\Exception; -class DbAdapterManagerAdapterConfigNotVaild extends \RuntimeException implements ExceptionInterface +class DbAdapterManagerAdapterConfigNotValid extends \RuntimeException implements ExceptionInterface { } diff --git a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php index 7795ade112f..ee07ac44f60 100644 --- a/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php +++ b/tests/ZendTest/Mvc/Service/DbAdapterManagerTest.php @@ -92,7 +92,7 @@ public function testInitDbAdapter() 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); } @@ -107,7 +107,7 @@ public function testInitDbAdapterAlias() $adapter1 = $dam->getAdapter('sqlliteDb'); $adapter2 = $dam->getAdapter('sqlliteDb2'); $adapter3 = $dam->getAdapter('sqlliteDb3'); - + $this->assertSame($adapter1, $adapter2); $this->assertSame($adapter1, $adapter3); unset($adapter); From c447f71df34dd0e9723e327d768cce5bb7d94b60 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Wed, 23 Jan 2013 11:01:25 +0100 Subject: [PATCH 19/19] fix coding standard --- library/Zend/Mvc/Service/DbAdapterManager.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/Zend/Mvc/Service/DbAdapterManager.php b/library/Zend/Mvc/Service/DbAdapterManager.php index ab58e4811b4..5f48e88de6f 100644 --- a/library/Zend/Mvc/Service/DbAdapterManager.php +++ b/library/Zend/Mvc/Service/DbAdapterManager.php @@ -102,8 +102,7 @@ public function addAdapter($key, Adapter $adapter) return true; } throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); - } - elseif ( $this->hasAdapterConfig($key) ) { + } elseif ( $this->hasAdapterConfig($key) ) { throw new DbAdapterManagerAdapterAlreadyRegistered(sprintf("adapter key (%s) allready exist",$key)); }