diff --git a/composer.json b/composer.json index 1e9793f06d..569c564261 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "php": "^7.3", "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 b12b7a09b4..fe75b1a9f1 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": "ea6ec5389b21f5bda19d375bad21d81f", + "content-hash": "0b2935c62211fc19189ce4af91753395", "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", @@ -3189,8 +3238,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", @@ -3256,5 +3305,6 @@ "platform-dev": [], "platform-overrides": { "php": "7.3.0" - } + }, + "plugin-api-version": "1.1.0" } diff --git a/lib/Doctrine/DBAL/Registry/ConnectionRegistry.php b/lib/Doctrine/DBAL/Registry/ConnectionRegistry.php new file mode 100644 index 0000000000..b3877f8d42 --- /dev/null +++ b/lib/Doctrine/DBAL/Registry/ConnectionRegistry.php @@ -0,0 +1,41 @@ + 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/Registry/Psr11ConnectionRegistry.php b/lib/Doctrine/DBAL/Registry/Psr11ConnectionRegistry.php new file mode 100644 index 0000000000..2d289f86b5 --- /dev/null +++ b/lib/Doctrine/DBAL/Registry/Psr11ConnectionRegistry.php @@ -0,0 +1,71 @@ +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/Registry/Psr11ConnectionRegistryTest.php b/tests/Doctrine/Tests/DBAL/Registry/Psr11ConnectionRegistryTest.php new file mode 100644 index 0000000000..0e458e7644 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Registry/Psr11ConnectionRegistryTest.php @@ -0,0 +1,83 @@ + */ + private $connections; + + protected function setUp() : void + { + /** @var Connection&Stub $fooConnection */ + $fooConnection = $this->createStub(Connection::class); + /** @var Connection&Stub $barConnection */ + $barConnection = $this->createStub(Connection::class); + + $this->connections = [ + 'foo' => $fooConnection, + 'bar' => $barConnection, + ]; + + /** @var ContainerInterface&Stub $container */ + $container = $this->createStub(ContainerInterface::class); + $container->method('has') + ->willReturnCallback(function (string $name) : bool { + return isset($this->connections[$name]); + }); + + $container->method('get') + ->willReturnCallback(function (string $name) : Connection { + 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()); + } +}