Skip to content

Commit e71f77b

Browse files
alongoszSteveb-pkonradoboza
authored andcommitted
IBX-8399: Moved RepositoryConfigurationProvider to Repository layer (#383)
For more details see https://issues.ibexa.co/browse/IBX-8399 and #383 Key changes: * Changed Core Bundle ApiLoader Exception to be Repository Exceptions * Extracted an interface from RepositoryConfigurationProvider * Refactored StorageConnectionFactory to rely on ServiceProviderInterface * Added missing readonly keyword in relevant places * Deprecated RepositoryConfigurationProvider located in Core Bundle namespace * [Tests] Aligned tests with the changes * [PHPStan] Aligned baseline with the changes * [PHPStan] Updated baseline after PHPStan release --------- Co-Authored-By: Paweł Niedzielski <[email protected]> Co-Authored-By: Konrad Oboza <[email protected]>
1 parent 42df0fb commit e71f77b

32 files changed

+543
-338
lines changed

src/bundle/Core/ApiLoader/Exception/InvalidRepositoryException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\Bundle\Core\ApiLoader\Exception;
910

10-
use InvalidArgumentException;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112

12-
class InvalidRepositoryException extends InvalidArgumentException
13+
final class InvalidRepositoryException extends InvalidArgumentException
1314
{
1415
}

src/bundle/Core/ApiLoader/Exception/InvalidSearchEngine.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\Bundle\Core\ApiLoader\Exception;
910

10-
use InvalidArgumentException;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112

12-
class InvalidSearchEngine extends InvalidArgumentException
13+
final class InvalidSearchEngine extends InvalidArgumentException
1314
{
1415
}

src/bundle/Core/ApiLoader/Exception/InvalidSearchEngineIndexer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\Bundle\Core\ApiLoader\Exception;
910

10-
use InvalidArgumentException;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112

12-
class InvalidSearchEngineIndexer extends InvalidArgumentException
13+
final class InvalidSearchEngineIndexer extends InvalidArgumentException
1314
{
1415
}

src/bundle/Core/ApiLoader/Exception/InvalidStorageEngine.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\Bundle\Core\ApiLoader\Exception;
910

10-
use InvalidArgumentException;
11+
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1112

12-
class InvalidStorageEngine extends InvalidArgumentException
13+
final class InvalidStorageEngine extends InvalidArgumentException
1314
{
1415
}

src/bundle/Core/ApiLoader/RepositoryConfigurationProvider.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,39 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\Bundle\Core\ApiLoader;
910

10-
use Ibexa\Bundle\Core\ApiLoader\Exception\InvalidRepositoryException;
11-
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
11+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1212

1313
/**
14-
* The repository configuration provider.
14+
* @deprecated 5.0.0 The "\Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider" class is deprecated, will be removed in 6.0.0.
15+
* Inject {@see \Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface} from Dependency Injection Container instead.
1516
*/
16-
class RepositoryConfigurationProvider
17+
final readonly class RepositoryConfigurationProvider implements RepositoryConfigurationProviderInterface
1718
{
18-
private const REPOSITORY_STORAGE = 'storage';
19-
private const REPOSITORY_CONNECTION = 'connection';
20-
private const DEFAULT_CONNECTION_NAME = 'default';
21-
22-
/** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface */
23-
private $configResolver;
24-
25-
/** @var array */
26-
private $repositories;
27-
28-
public function __construct(ConfigResolverInterface $configResolver, array $repositories)
19+
public function __construct(private RepositoryConfigurationProviderInterface $configurationProvider)
2920
{
30-
$this->configResolver = $configResolver;
31-
$this->repositories = $repositories;
3221
}
3322

34-
/**
35-
* @return array
36-
*
37-
* @throws \Ibexa\Bundle\Core\ApiLoader\Exception\InvalidRepositoryException
38-
*/
39-
public function getRepositoryConfig()
23+
public function getRepositoryConfig(): array
4024
{
41-
// Takes configured repository as the reference, if it exists.
42-
// If not, the first configured repository is considered instead.
43-
$repositoryAlias = $this->configResolver->getParameter('repository');
44-
$repositoryAlias = $repositoryAlias ?: $this->getDefaultRepositoryAlias();
45-
46-
if (empty($repositoryAlias) || !isset($this->repositories[$repositoryAlias])) {
47-
throw new InvalidRepositoryException(
48-
"Undefined Repository '$repositoryAlias'. Check if the Repository is configured in your project's ibexa.yaml."
49-
);
50-
}
51-
52-
return ['alias' => $repositoryAlias] + $this->repositories[$repositoryAlias];
25+
return $this->configurationProvider->getRepositoryConfig();
5326
}
5427

5528
public function getCurrentRepositoryAlias(): string
5629
{
57-
return $this->getRepositoryConfig()['alias'];
30+
return $this->configurationProvider->getCurrentRepositoryAlias();
5831
}
5932

6033
public function getDefaultRepositoryAlias(): ?string
6134
{
62-
$aliases = array_keys($this->repositories);
63-
64-
return array_shift($aliases);
35+
return $this->configurationProvider->getDefaultRepositoryAlias();
6536
}
6637

6738
public function getStorageConnectionName(): string
6839
{
69-
$repositoryConfig = $this->getRepositoryConfig();
70-
71-
return $repositoryConfig[self::REPOSITORY_STORAGE][self::REPOSITORY_CONNECTION]
72-
? $repositoryConfig[self::REPOSITORY_STORAGE][self::REPOSITORY_CONNECTION]
73-
: self::DEFAULT_CONNECTION_NAME;
40+
return $this->configurationProvider->getStorageConnectionName();
7441
}
7542
}

src/bundle/Core/ApiLoader/RepositoryFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Ibexa\Bundle\Core\ApiLoader;
99

10+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1011
use Ibexa\Contracts\Core\Persistence\Filter\Content\Handler as ContentFilteringHandler;
1112
use Ibexa\Contracts\Core\Persistence\Filter\Location\Handler as LocationFilteringHandler;
1213
use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
@@ -59,6 +60,7 @@ public function __construct(
5960
$repositoryClass,
6061
array $policyMap,
6162
LanguageResolver $languageResolver,
63+
private readonly RepositoryConfigurationProviderInterface $repositoryConfigurationProvider,
6264
LoggerInterface $logger = null
6365
) {
6466
$this->configResolver = $configResolver;
@@ -94,9 +96,9 @@ public function buildRepository(
9496
LocationFilteringHandler $locationFilteringHandler,
9597
PasswordValidatorInterface $passwordValidator,
9698
ConfigResolverInterface $configResolver,
97-
NameSchemaServiceInterface $nameSchemaService
99+
NameSchemaServiceInterface $nameSchemaService,
98100
): Repository {
99-
$config = $this->container->get(RepositoryConfigurationProvider::class)->getRepositoryConfig();
101+
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();
100102

101103
return new $this->repositoryClass(
102104
$persistenceHandler,

src/bundle/Core/ApiLoader/SearchEngineFactory.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
namespace Ibexa\Bundle\Core\ApiLoader;
99

1010
use Ibexa\Bundle\Core\ApiLoader\Exception\InvalidSearchEngine;
11+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1112
use Ibexa\Contracts\Core\Search\Handler as SearchHandler;
1213

1314
/**
1415
* The search engine factory.
1516
*/
1617
class SearchEngineFactory
1718
{
18-
/** @var \Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider */
19-
private $repositoryConfigurationProvider;
20-
2119
/**
2220
* Hash of registered search engines.
2321
* Key is the search engine identifier, value search handler itself.
@@ -26,9 +24,9 @@ class SearchEngineFactory
2624
*/
2725
protected $searchEngines = [];
2826

29-
public function __construct(RepositoryConfigurationProvider $repositoryConfigurationProvider)
30-
{
31-
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
27+
public function __construct(
28+
private readonly RepositoryConfigurationProviderInterface $repositoryConfigurationProvider,
29+
) {
3230
}
3331

3432
/**

src/bundle/Core/ApiLoader/SearchEngineIndexerFactory.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99

1010
use Ibexa\Bundle\Core\ApiLoader\Exception\InvalidSearchEngine;
1111
use Ibexa\Bundle\Core\ApiLoader\Exception\InvalidSearchEngineIndexer;
12+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1213
use Ibexa\Core\Search\Common\Indexer as SearchEngineIndexer;
1314

1415
/**
1516
* The search engine indexer factory.
1617
*/
1718
class SearchEngineIndexerFactory
1819
{
19-
/** @var \Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider */
20-
private $repositoryConfigurationProvider;
21-
2220
/**
2321
* Hash of registered search engine indexers.
2422
* Key is the search engine identifier, value indexer itself.
@@ -27,9 +25,9 @@ class SearchEngineIndexerFactory
2725
*/
2826
protected $searchEngineIndexers = [];
2927

30-
public function __construct(RepositoryConfigurationProvider $repositoryConfigurationProvider)
31-
{
32-
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
28+
public function __construct(
29+
private readonly RepositoryConfigurationProviderInterface $repositoryConfigurationProvider,
30+
) {
3331
}
3432

3533
/**

src/bundle/Core/ApiLoader/StorageConnectionFactory.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,44 @@
77

88
namespace Ibexa\Bundle\Core\ApiLoader;
99

10+
use Doctrine\DBAL\Connection;
11+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1012
use InvalidArgumentException;
11-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
13+
use Symfony\Contracts\Service\ServiceProviderInterface;
1314

14-
class StorageConnectionFactory implements ContainerAwareInterface
15+
/**
16+
* @internal
17+
*/
18+
final readonly class StorageConnectionFactory
1519
{
16-
use ContainerAwareTrait;
17-
18-
/** @var \Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider */
19-
protected $repositoryConfigurationProvider;
20-
21-
public function __construct(RepositoryConfigurationProvider $repositoryConfigurationProvider)
22-
{
23-
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
20+
public function __construct(
21+
private RepositoryConfigurationProviderInterface $repositoryConfigurationProvider,
22+
private ServiceProviderInterface $serviceLocator,
23+
) {
2424
}
2525

2626
/**
2727
* Returns database connection used by database handler.
2828
*
2929
* @throws \InvalidArgumentException
30-
*
31-
* @return \Doctrine\DBAL\Connection
30+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
31+
* @throws \Psr\Container\ContainerExceptionInterface
32+
* @throws \Psr\Container\NotFoundExceptionInterface
3233
*/
33-
public function getConnection()
34+
public function getConnection(): Connection
3435
{
3536
$repositoryConfig = $this->repositoryConfigurationProvider->getRepositoryConfig();
3637
// Taking provided connection name if any.
3738
// Otherwise, just fallback to the default connection.
3839

39-
if (isset($repositoryConfig['storage']['connection'])) {
40-
$doctrineConnectionId = sprintf('doctrine.dbal.%s_connection', $repositoryConfig['storage']['connection']);
41-
} else {
42-
// "database_connection" is an alias to the default connection, set up by DoctrineBundle.
43-
$doctrineConnectionId = 'database_connection';
44-
}
45-
46-
if (!$this->container->has($doctrineConnectionId)) {
40+
$connectionName = $repositoryConfig['storage']['connection'] ?? 'default';
41+
if (!$this->serviceLocator->has($connectionName)) {
4742
throw new InvalidArgumentException(
48-
"Invalid Doctrine connection '{$repositoryConfig['storage']['connection']}' for Repository '{$repositoryConfig['alias']}'." .
49-
'Valid connections are: ' . implode(', ', array_keys($this->container->getParameter('doctrine.connections')))
43+
"Invalid Doctrine connection '$connectionName' for Repository '{$repositoryConfig['alias']}'. " .
44+
'Valid connections are: ' . implode(', ', array_keys($this->serviceLocator->getProvidedServices()))
5045
);
5146
}
5247

53-
return $this->container->get($doctrineConnectionId);
48+
return $this->serviceLocator->get($connectionName);
5449
}
5550
}

src/bundle/Core/ApiLoader/StorageEngineFactory.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,49 @@
88
namespace Ibexa\Bundle\Core\ApiLoader;
99

1010
use Ibexa\Bundle\Core\ApiLoader\Exception\InvalidStorageEngine;
11+
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1112
use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
1213

1314
/**
1415
* The storage engine factory.
1516
*/
1617
class StorageEngineFactory
1718
{
18-
/** @var \Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider */
19-
private $repositoryConfigurationProvider;
20-
2119
/**
2220
* Hash of registered storage engines.
2321
* Key is the storage engine identifier, value persistence handler itself.
2422
*
2523
* @var \Ibexa\Contracts\Core\Persistence\Handler[]
2624
*/
27-
protected $storageEngines = [];
25+
protected array $storageEngines = [];
2826

29-
public function __construct(RepositoryConfigurationProvider $repositoryConfigurationProvider)
30-
{
31-
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
27+
public function __construct(
28+
private readonly RepositoryConfigurationProviderInterface $repositoryConfigurationProvider,
29+
) {
3230
}
3331

3432
/**
3533
* Registers $persistenceHandler as a valid storage engine, with identifier $storageEngineIdentifier.
3634
*
3735
* Note: It is strongly recommenced to register a lazy persistent handler.
38-
*
39-
* @param \Ibexa\Contracts\Core\Persistence\Handler $persistenceHandler
40-
* @param string $storageEngineIdentifier
4136
*/
42-
public function registerStorageEngine(PersistenceHandler $persistenceHandler, $storageEngineIdentifier)
37+
public function registerStorageEngine(PersistenceHandler $persistenceHandler, string $storageEngineIdentifier): void
4338
{
4439
$this->storageEngines[$storageEngineIdentifier] = $persistenceHandler;
4540
}
4641

4742
/**
4843
* @return \Ibexa\Contracts\Core\Persistence\Handler[]
4944
*/
50-
public function getStorageEngines()
45+
public function getStorageEngines(): array
5146
{
5247
return $this->storageEngines;
5348
}
5449

5550
/**
5651
* Builds storage engine identified by $storageEngineIdentifier (the "alias" attribute in the service tag).
5752
*
58-
* @throws \Ibexa\Bundle\Core\ApiLoader\Exception\InvalidStorageEngine
53+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
5954
*/
6055
public function buildStorageEngine(): PersistenceHandler
6156
{
@@ -73,9 +68,9 @@ public function buildStorageEngine(): PersistenceHandler
7368

7469
if (!isset($this->storageEngines[$storageEngineAlias])) {
7570
throw new InvalidStorageEngine(
76-
"Invalid storage engine '{$storageEngineAlias}'. " .
71+
"Invalid storage engine '$storageEngineAlias'. " .
7772
'Could not find any service tagged with ibexa.storage ' .
78-
"with alias {$storageEngineAlias}."
73+
"with alias $storageEngineAlias."
7974
);
8075
}
8176

0 commit comments

Comments
 (0)