From 334599c93cf036d653c0b0e6e297c075a141cb33 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Tue, 27 Sep 2022 11:40:29 +0200 Subject: [PATCH] feat: add persistences to the DI container (#1023) * feat: add persistences to the DI container * chore: fix doc * chore: fix tests * chore: code style fixes * chore: code style fixes * chore: code style fixes * chore: code style fixes * chore: code style fixes * chore: remove coverage check from service provider Co-authored-by: Gabriel Felipe Soares --- README.md | 8 +- .../PersistenceServiceProvider.php | 78 +++++++++++++++++ common/persistence/README.md | 86 +++++++++++++++++++ .../ContainerServiceProvider.php | 3 + manifest.php | 2 + test/unit/oatbox/mutex/test_action.php | 48 +++++------ 6 files changed, 195 insertions(+), 30 deletions(-) create mode 100644 common/persistence/PersistenceServiceProvider.php create mode 100644 common/persistence/README.md diff --git a/README.md b/README.md index e10c40ca6..009dcaedb 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,15 @@ Core extension, provide the low level framework and an API to manage ontologies ### Dependency Injection -Read here [how to use Dependency Injection](./core/DependencyInjection/README.md). +Read here [how to use the Dependency Injection](./core/DependencyInjection/README.md). + +### Persistence + +Read here [how to use the Persistence](./common/persistence/README.md). ### Middleware -Read here [how to use Middlewares](./core/Middleware/README.md). +Read here [how to use the Middlewares](./core/Middleware/README.md). ### Log diff --git a/common/persistence/PersistenceServiceProvider.php b/common/persistence/PersistenceServiceProvider.php new file mode 100644 index 000000000..7407dfbcb --- /dev/null +++ b/common/persistence/PersistenceServiceProvider.php @@ -0,0 +1,78 @@ + + */ + +declare(strict_types=1); + +namespace oat\generis\persistence; + +use common_persistence_sql_Platform; +use common_persistence_SqlPersistence; +use Doctrine\DBAL\Query\QueryBuilder; +use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface; +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +use function Symfony\Component\DependencyInjection\Loader\Configurator\service; + +/** + * @codeCoverageIgnore + */ +class PersistenceServiceProvider implements ContainerServiceProviderInterface +{ + public const DEFAULT_SQL_PERSISTENCE = common_persistence_SqlPersistence::class . '::default'; + public const DEFAULT_SQL_PLATFORM = common_persistence_sql_Platform::class . '::default'; + public const DEFAULT_QUERY_BUILDER = QueryBuilder::class . '::default'; + + public function __invoke(ContainerConfigurator $configurator): void + { + $services = $configurator->services(); + + $services->set(self::DEFAULT_SQL_PERSISTENCE, common_persistence_SqlPersistence::class) + ->factory( + [ + service(PersistenceManager::SERVICE_ID), + 'getPersistenceById' + ] + ) + ->args( + [ + 'default' + ] + ) + ->public(); + + $services->set(self::DEFAULT_SQL_PLATFORM, common_persistence_sql_Platform::class) + ->factory( + [ + service(self::DEFAULT_SQL_PERSISTENCE), + 'getPlatForm' + ] + ); + + $services->set(self::DEFAULT_QUERY_BUILDER, QueryBuilder::class) + ->factory( + [ + service(self::DEFAULT_SQL_PLATFORM), + 'getQueryBuilder' + ] + )->share(false); + } +} diff --git a/common/persistence/README.md b/common/persistence/README.md new file mode 100644 index 000000000..b741adb61 --- /dev/null +++ b/common/persistence/README.md @@ -0,0 +1,86 @@ +Persistence +======= + +## How to connect to persistence? + +In case you need to connect to database, there are a few ways: + +1) **RECOMMENDED:** Using out-of-box DI container services: + +```php +getServiceManager()->getContainer(); + +/** @var common_persistence_SqlPersistence $persistence */ +$persistence = $container->get(PersistenceServiceProvider::DEFAULT_SQL_PERSISTENCE); + +/** @var common_persistence_sql_Platform $platform */ +$platform = $container->get(PersistenceServiceProvider::DEFAULT_SQL_PLATFORM); + +/** @var QueryBuilder $queryBuilder */ +$queryBuilder = $container->get(PersistenceServiceProvider::DEFAULT_QUERY_BUILDER); +``` + +So if you need the query builder in you class you can simply do like the example bellow: + +````php +services(); + + $services->set(MyClass::class, MyClass::class) + ->args([service(PersistenceServiceProvider::DEFAULT_QUERY_BUILDER)]); + } +} + +// Your class using the container +class MyClass +{ + public function __construct(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder) { + } +} +```` + +2) **Legacy / NOT RECOMMENDED**: Using PersistenceManager factory method: + +```php +getServiceManager()->getContainer(); + +/** @var PersistenceManager $persistence */ +$persistenceManager = $container->get(PersistenceManager::SERVICE_ID); + +/** @var common_persistence_SqlPersistence $persistence */ +$persistence = $persistenceManager->getPersistenceById('default'); + +/** @var common_persistence_sql_Platform $platform */ +$platform = $persistence->getPlatform(); + +/** @var QueryBuilder $queryBuilder */ +$queryBuilder = $platform->getQueryBuilder(); +``` + +## Adding another persistence to the container + +In case you need to access other persistence than the default one, +please consider extending the [PersistenceServiceProvider](./PersistenceServiceProvider.php). \ No newline at end of file diff --git a/core/DependencyInjection/ContainerServiceProvider.php b/core/DependencyInjection/ContainerServiceProvider.php index 35ae3651d..5b9a6c4ed 100644 --- a/core/DependencyInjection/ContainerServiceProvider.php +++ b/core/DependencyInjection/ContainerServiceProvider.php @@ -36,6 +36,9 @@ use function Symfony\Component\DependencyInjection\Loader\Configurator\param; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; +/** + * @codeCoverageIgnore + */ class ContainerServiceProvider implements ContainerServiceProviderInterface { public function __invoke(ContainerConfigurator $configurator): void diff --git a/manifest.php b/manifest.php index e36fede70..65ee03dfc 100755 --- a/manifest.php +++ b/manifest.php @@ -20,6 +20,7 @@ declare(strict_types=1); +use oat\generis\persistence\PersistenceServiceProvider; use oat\generis\scripts\update\Updater; use oat\generis\scripts\install\TaskQueue; use oat\generis\scripts\install\RegisterServices; @@ -59,5 +60,6 @@ ContainerServiceProvider::class, LogServiceProvider::class, ResourceServiceProvider::class, + PersistenceServiceProvider::class, ], ]; diff --git a/test/unit/oatbox/mutex/test_action.php b/test/unit/oatbox/mutex/test_action.php index 81f2ed39f..7febcb657 100644 --- a/test/unit/oatbox/mutex/test_action.php +++ b/test/unit/oatbox/mutex/test_action.php @@ -1,21 +1,32 @@ set(\common_persistence_Manager::SERVICE_ID, new \common_persistence_Manager()); + $serviceManager = new ServiceManager($config); + $service = new LockService([ + LockService::OPTION_PERSISTENCE_CLASS => $class, + LockService::OPTION_PERSISTENCE_OPTIONS => $dir + ]); + $service->setServiceLocator($serviceManager); + + return $service; +} +// phpcs:enable + array_shift($argv); $actionId = $argv[0]; -$sleep = (int) $argv[1]; -$implementation = (string) $argv[2]; +$sleep = (int)$argv[1]; +$implementation = (string)$argv[2]; $dir = isset($argv[3]) ? $argv[3] : null; if ($implementation === 'FlockStore') { @@ -30,22 +41,3 @@ $lock->acquire(true); sleep($sleep); $lock->release(); - -/** - * @param $class - * @param $dir - * @return LockService - * @throws common_Exception - */ -function getInstance($class, $dir) -{ - $config = new \common_persistence_KeyValuePersistence([], new \common_persistence_InMemoryKvDriver()); - $config->set(\common_persistence_Manager::SERVICE_ID, new \common_persistence_Manager()); - $serviceManager = new ServiceManager($config); - $service = new LockService([ - LockService::OPTION_PERSISTENCE_CLASS => $class, - LockService::OPTION_PERSISTENCE_OPTIONS => $dir - ]); - $service->setServiceLocator($serviceManager); - return $service; -}