2015-08-17 17:00:26 -07:00
< ? php
namespace Doctrine\Common\Persistence ;
2018-11-23 12:29:20 +00:00
use InvalidArgumentException ;
use ReflectionClass ;
use function explode ;
use function sprintf ;
use function strpos ;
2015-08-17 17:00:26 -07:00
/**
* Abstract implementation of the ManagerRegistry contract .
*/
abstract class AbstractManagerRegistry implements ManagerRegistry
{
2018-11-23 12:29:20 +00:00
/** @var string */
2015-08-17 17:00:26 -07:00
private $name ;
2018-11-23 12:29:20 +00:00
/** @var string[] */
2015-08-17 17:00:26 -07:00
private $connections ;
2018-11-23 12:29:20 +00:00
/** @var string[] */
2015-08-17 17:00:26 -07:00
private $managers ;
2018-11-23 12:29:20 +00:00
/** @var string */
2015-08-17 17:00:26 -07:00
private $defaultConnection ;
2018-11-23 12:29:20 +00:00
/** @var string */
2015-08-17 17:00:26 -07:00
private $defaultManager ;
2018-11-23 12:29:20 +00:00
/** @var string */
2015-08-17 17:00:26 -07:00
private $proxyInterfaceName ;
/**
2018-11-23 12:29:20 +00:00
* @ param string $name
* @ param string [] $connections
* @ param string [] $managers
* @ param string $defaultConnection
* @ param string $defaultManager
* @ param string $proxyInterfaceName
2015-08-17 17:00:26 -07:00
*/
public function __construct ( $name , array $connections , array $managers , $defaultConnection , $defaultManager , $proxyInterfaceName )
{
2018-11-23 12:29:20 +00:00
$this -> name = $name ;
$this -> connections = $connections ;
$this -> managers = $managers ;
$this -> defaultConnection = $defaultConnection ;
$this -> defaultManager = $defaultManager ;
2015-08-17 17:00:26 -07:00
$this -> proxyInterfaceName = $proxyInterfaceName ;
}
/**
* Fetches / creates the given services .
*
* A service in this context is connection or a manager instance .
*
* @ param string $name The name of the service .
*
* @ return object The instance of the given service .
*/
abstract protected function getService ( $name );
/**
* Resets the given services .
*
* A service in this context is connection or a manager instance .
*
* @ param string $name The name of the service .
*
* @ return void
*/
abstract protected function resetService ( $name );
/**
* Gets the name of the registry .
*
* @ return string
*/
public function getName ()
{
return $this -> name ;
}
/**
* { @ inheritdoc }
*/
public function getConnection ( $name = null )
{
2018-11-23 12:29:20 +00:00
if ( $name === null ) {
2015-08-17 17:00:26 -07:00
$name = $this -> defaultConnection ;
}
2018-11-23 12:29:20 +00:00
if ( ! isset ( $this -> connections [ $name ])) {
throw new InvalidArgumentException ( sprintf ( 'Doctrine %s Connection named "%s" does not exist.' , $this -> name , $name ));
2015-08-17 17:00:26 -07:00
}
return $this -> getService ( $this -> connections [ $name ]);
}
/**
* { @ inheritdoc }
*/
public function getConnectionNames ()
{
return $this -> connections ;
}
/**
* { @ inheritdoc }
*/
public function getConnections ()
{
2017-04-13 15:53:35 +01:00
$connections = [];
2015-08-17 17:00:26 -07:00
foreach ( $this -> connections as $name => $id ) {
$connections [ $name ] = $this -> getService ( $id );
}
return $connections ;
}
/**
* { @ inheritdoc }
*/
public function getDefaultConnectionName ()
{
return $this -> defaultConnection ;
}
/**
* { @ inheritdoc }
*/
public function getDefaultManagerName ()
{
return $this -> defaultManager ;
}
/**
* { @ inheritdoc }
*
2018-11-23 12:29:20 +00:00
* @ throws InvalidArgumentException
2015-08-17 17:00:26 -07:00
*/
public function getManager ( $name = null )
{
2018-11-23 12:29:20 +00:00
if ( $name === null ) {
2015-08-17 17:00:26 -07:00
$name = $this -> defaultManager ;
}
2018-11-23 12:29:20 +00:00
if ( ! isset ( $this -> managers [ $name ])) {
throw new InvalidArgumentException ( sprintf ( 'Doctrine %s Manager named "%s" does not exist.' , $this -> name , $name ));
2015-08-17 17:00:26 -07:00
}
return $this -> getService ( $this -> managers [ $name ]);
}
/**
* { @ inheritdoc }
*/
public function getManagerForClass ( $class )
{
// Check for namespace alias
if ( strpos ( $class , ':' ) !== false ) {
2018-11-23 12:29:20 +00:00
[ $namespaceAlias , $simpleClassName ] = explode ( ':' , $class , 2 );
$class = $this -> getAliasNamespace ( $namespaceAlias ) . '\\' . $simpleClassName ;
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
$proxyClass = new ReflectionClass ( $class );
2017-04-13 15:53:35 +01:00
2015-08-17 17:00:26 -07:00
if ( $proxyClass -> implementsInterface ( $this -> proxyInterfaceName )) {
2018-11-23 12:29:20 +00:00
$parentClass = $proxyClass -> getParentClass ();
if ( ! $parentClass ) {
2017-04-13 15:53:35 +01:00
return null ;
}
$class = $parentClass -> getName ();
2015-08-17 17:00:26 -07:00
}
foreach ( $this -> managers as $id ) {
$manager = $this -> getService ( $id );
2018-11-23 12:29:20 +00:00
if ( ! $manager -> getMetadataFactory () -> isTransient ( $class )) {
2015-08-17 17:00:26 -07:00
return $manager ;
}
}
}
/**
* { @ inheritdoc }
*/
public function getManagerNames ()
{
return $this -> managers ;
}
/**
* { @ inheritdoc }
*/
public function getManagers ()
{
2017-04-13 15:53:35 +01:00
$dms = [];
2015-08-17 17:00:26 -07:00
foreach ( $this -> managers as $name => $id ) {
$dms [ $name ] = $this -> getService ( $id );
}
return $dms ;
}
/**
* { @ inheritdoc }
*/
public function getRepository ( $persistentObjectName , $persistentManagerName = null )
{
return $this -> getManager ( $persistentManagerName ) -> getRepository ( $persistentObjectName );
}
/**
* { @ inheritdoc }
*/
public function resetManager ( $name = null )
{
2018-11-23 12:29:20 +00:00
if ( $name === null ) {
2015-08-17 17:00:26 -07:00
$name = $this -> defaultManager ;
}
2018-11-23 12:29:20 +00:00
if ( ! isset ( $this -> managers [ $name ])) {
throw new InvalidArgumentException ( sprintf ( 'Doctrine %s Manager named "%s" does not exist.' , $this -> name , $name ));
2015-08-17 17:00:26 -07:00
}
// force the creation of a new document manager
// if the current one is closed
$this -> resetService ( $this -> managers [ $name ]);
2017-04-13 15:53:35 +01:00
return $this -> getManager ( $name );
2015-08-17 17:00:26 -07:00
}
}