This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
add AdapterManager in to Zend\Db\Adapter namespace #2903
Closed
ClemensSahs
wants to merge
19
commits into
zendframework:master
from
ClemensSahs:feature/multi-database
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a9b3176
add AdapterManager in to Zend\Db\Adapter namespace
ClemensSahs 52ef22c
fix cs
ClemensSahs 7a161fc
cs fix agin
ClemensSahs 3d69677
eof line cs fix
ClemensSahs 4a6a63b
- move in to Zend/Mvc/Service
ClemensSahs d7f1b3a
add exceptions
ClemensSahs 18e02a9
add exceptions
ClemensSahs 657c5b8
add unitTest
ClemensSahs 72a9eec
-change renamed methode "addDbAdapterConfig"
ClemensSahs d2d1514
move logic "get Platform object" in to a seperate methode
ClemensSahs a48cfb0
- move logic "get driver object" in to a seperate methode
ClemensSahs 885eb80
expectedException with full class name
ClemensSahs d0787df
repair unit test
ClemensSahs 5fc361a
add test for not exist adapter
ClemensSahs 0d95824
fix testAdapterDuplicatKey
ClemensSahs 2326fad
fix unit Test
ClemensSahs e0a060c
fix not throwed exception in "testAdapterDuplicatKey"
ClemensSahs c697412
fix cs
ClemensSahs c447f71
fix coding standard
ClemensSahs File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,297 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
* @package Zend_Db | |||
*/ | |||
|
|||
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\DbAdapterManagerAdapterAlreadyRegistered; | |||
use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterCoundInit; | |||
use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterNotExist; | |||
use Zend\Mvc\Service\Exception\DbAdapterManagerAdapterConfigNotValid; | |||
use Exception; | |||
|
|||
class DbAdapterManager implements ServiceLocatorAwareInterface | |||
{ | |||
/** | |||
* | |||
* @var Adapter[] | |||
*/ | |||
protected $_dbAdapter = array(); | |||
|
|||
protected $_dbAdapterConfig = array(); | |||
|
|||
/** | |||
* @var ServiceLocatorInterface | |||
*/ | |||
protected $_serviceLocator; | |||
|
|||
/** | |||
* @param array $config | |||
* @throws DbAdapterManagerAdapterAlreadyRegistered | |||
*/ | |||
public function addAdapterConfig(array $configArray) | |||
{ | |||
foreach ($configArray as $key => $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; | |||
} | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
* @package Zend_Db | |||
*/ | |||
|
|||
namespace Zend\Mvc\Service; | |||
|
|||
use Zend\ServiceManager\FactoryInterface; | |||
use Zend\ServiceManager\ServiceLocatorInterface; | |||
|
|||
/** | |||
* @category Zend | |||
* @package Zend_Db | |||
* @subpackage Adapter | |||
*/ | |||
class DbAdapterManagerFactory implements FactoryInterface | |||
{ | |||
/** | |||
* Create db adapter service | |||
* | |||
* @param ServiceLocatorInterface $serviceLocator | |||
* @return Adapter | |||
*/ | |||
public function createService(ServiceLocatorInterface $serviceLocator) | |||
{ | |||
$configArray = $serviceLocator->get('Config'); | |||
|
|||
$adapterService = new DbAdapterManager(); | |||
$adapterService->addAdapterConfig($configArray['db_adapter_manager']); | |||
|
|||
return $adapterService; | |||
} | |||
} |
7 changes: 7 additions & 0 deletions
7
library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterAlreadyRegistered.php
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
<?php | |||
|
|||
namespace Zend\Mvc\Service\Exception; | |||
|
|||
class DbAdapterManagerAdapterAlreadyRegistered extends \RuntimeException implements ExceptionInterface | |||
{ | |||
} |
7 changes: 7 additions & 0 deletions
7
library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterConfigNotValid.php
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
<?php | |||
|
|||
namespace Zend\Mvc\Service\Exception; | |||
|
|||
class DbAdapterManagerAdapterConfigNotValid extends \RuntimeException implements ExceptionInterface | |||
{ | |||
} |
7 changes: 7 additions & 0 deletions
7
library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterCoundInit.php
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
<?php | |||
|
|||
namespace Zend\Mvc\Service\Exception; | |||
|
|||
class DbAdapterManagerAdapterCoundInit extends \RuntimeException implements ExceptionInterface | |||
{ | |||
} |
7 changes: 7 additions & 0 deletions
7
library/Zend/Mvc/Service/Exception/DbAdapterManagerAdapterNotExist.php
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
<?php | |||
|
|||
namespace Zend\Mvc\Service\Exception; | |||
|
|||
class DbAdapterManagerAdapterNotExist extends \RuntimeException implements ExceptionInterface | |||
{ | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
* @package Zend_Mvc | |||
*/ | |||
|
|||
namespace Zend\Mvc\Service\Exception; | |||
|
|||
/** | |||
* @category Zend | |||
* @package Zend_Mvc | |||
* @subpackage Exception | |||
*/ | |||
interface ExceptionInterface | |||
{ | |||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't prefix non-public members with underscores; no longer needed with ZF2 CS.