From d866f1db51a991172de94d07c1b7f079df302fdd Mon Sep 17 00:00:00 2001 From: David Maicher Date: Sun, 8 Mar 2020 15:46:46 +0100 Subject: [PATCH] Add DBAL Connection Registry --- composer.json | 3 +- composer.lock | 63 +++++++++++++-- lib/Doctrine/DBAL/ConnectionRegistry.php | 40 ++++++++++ lib/Doctrine/DBAL/Psr11ConnectionRegistry.php | 69 ++++++++++++++++ .../DBAL/Psr11ConnectionRegistryTest.php | 78 +++++++++++++++++++ 5 files changed, 245 insertions(+), 8 deletions(-) create mode 100644 lib/Doctrine/DBAL/ConnectionRegistry.php create mode 100644 lib/Doctrine/DBAL/Psr11ConnectionRegistry.php create mode 100644 tests/Doctrine/Tests/DBAL/Psr11ConnectionRegistryTest.php diff --git a/composer.json b/composer.json index 76f0ddb3716..5a2f90d7ed8 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "php": "^7.2", "doctrine/cache": "^1.0", "doctrine/event-manager": "^1.0", - "ocramius/package-versions": "^1.4" + "ocramius/package-versions": "^1.4", + "psr/container": "^1.0" }, "require-dev": { "doctrine/coding-standard": "^7.0", diff --git a/composer.lock b/composer.lock index 35d5ae4f4f1..d2aae3ec1bb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1d91a8d87cf22c7dbee3c58f8777ecf5", + "content-hash": "2039385095b850c164abd1b891dc03d2", "packages": [ { "name": "doctrine/cache", @@ -203,6 +203,55 @@ ], "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)", "time": "2019-11-15T16:17:10+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" } ], "packages-dev": [ @@ -769,7 +818,7 @@ "homepage": "https://nette.org/contributors" } ], - "description": "🔍 Nette Finder: find files and directories with an intuitive API.", + "description": "? Nette Finder: find files and directories with an intuitive API.", "homepage": "https://nette.org", "keywords": [ "filesystem", @@ -829,7 +878,7 @@ "homepage": "https://nette.org/contributors" } ], - "description": "🍸 Nette NEON: encodes and decodes NEON file format.", + "description": "? Nette NEON: encodes and decodes NEON file format.", "homepage": "http://ne-on.org", "keywords": [ "export", @@ -2585,8 +2634,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "Collection of value objects that represent the types of the PHP type system", @@ -3123,8 +3172,8 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" } ], "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", diff --git a/lib/Doctrine/DBAL/ConnectionRegistry.php b/lib/Doctrine/DBAL/ConnectionRegistry.php new file mode 100644 index 00000000000..8071182bfba --- /dev/null +++ b/lib/Doctrine/DBAL/ConnectionRegistry.php @@ -0,0 +1,40 @@ + An array of Connection instances. + */ + public function getConnections() : array; + + /** + * Gets all connection names. + * + * @return array An array of connection names. + */ + public function getConnectionNames() : array; +} diff --git a/lib/Doctrine/DBAL/Psr11ConnectionRegistry.php b/lib/Doctrine/DBAL/Psr11ConnectionRegistry.php new file mode 100644 index 00000000000..05a4197d88e --- /dev/null +++ b/lib/Doctrine/DBAL/Psr11ConnectionRegistry.php @@ -0,0 +1,69 @@ +container = $container; + $this->defaultConnectionName = $defaultConnectionName; + $this->connectionNames = $connectionNames; + } + + public function getDefaultConnectionName() : string + { + return $this->defaultConnectionName; + } + + public function getConnection(?string $name = null) : Connection + { + $name = $name ?? $this->defaultConnectionName; + + if (! $this->container->has($name)) { + throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name)); + } + + return $this->container->get($name); + } + + /** + * @inheritDoc + */ + public function getConnections() : array + { + $connections = []; + + foreach ($this->connectionNames as $connectionName) { + $connections[$connectionName] = $this->container->get($connectionName); + } + + return $connections; + } + + /** + * @inheritDoc + */ + public function getConnectionNames() : array + { + return $this->connectionNames; + } +} diff --git a/tests/Doctrine/Tests/DBAL/Psr11ConnectionRegistryTest.php b/tests/Doctrine/Tests/DBAL/Psr11ConnectionRegistryTest.php new file mode 100644 index 00000000000..31eb17f61a6 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Psr11ConnectionRegistryTest.php @@ -0,0 +1,78 @@ + */ + private $connections; + + protected function setUp() : void + { + $this->connections = [ + 'foo' => $this->createMock(Connection::class), + 'bar' => $this->createMock(Connection::class), + ]; + + $container = $this->createMock(ContainerInterface::class); + $container->expects($this->any()) + ->method('has') + ->willReturnCallback(function (string $name) { + return isset($this->connections[$name]); + }); + + $container->expects($this->any()) + ->method('get') + ->willReturnCallback(function (string $name) { + return $this->connections[$name]; + }); + + $this->registry = new Psr11ConnectionRegistry($container, 'bar', array_keys($this->connections)); + } + + public function testGetDefaultConnection() : void + { + $this->assertSame($this->connections['bar'], $this->registry->getConnection()); + } + + public function testGetConnectionByName() : void + { + $this->assertSame($this->connections['foo'], $this->registry->getConnection('foo')); + $this->assertSame($this->connections['bar'], $this->registry->getConnection('bar')); + } + + public function testGetNotExistentConnection() : void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Connection with name "something" does not exist.'); + $this->registry->getConnection('something'); + } + + public function testGetDefaultConnectionName() : void + { + $this->assertSame('bar', $this->registry->getDefaultConnectionName()); + } + + public function getGetConnections() : void + { + $this->assertSame($this->connections, $this->registry->getConnections()); + } + + public function testGetConnectionNames() : void + { + $this->assertSame(array_keys($this->connections), $this->registry->getConnectionNames()); + } +}