Skip to content

Commit

Permalink
Merge pull request #1024 from oat-sa/release-15.21
Browse files Browse the repository at this point in the history
Release 15.21
  • Loading branch information
gabrielfs7 authored Sep 27, 2022
2 parents 12e493a + 334599c commit 7bebeea
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 30 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
78 changes: 78 additions & 0 deletions common/persistence/PersistenceServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Gabriel Felipe Soares <[email protected]>
*/

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);
}
}
86 changes: 86 additions & 0 deletions common/persistence/README.md
Original file line number Diff line number Diff line change
@@ -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
<?php

use oat\generis\persistence\PersistenceServiceProvider;
use Doctrine\DBAL\Query\QueryBuilder;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $contaner */
$container = $this->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
<?php

use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\generis\persistence\PersistenceServiceProvider;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

class MyServiceProvider implements ContainerServiceProviderInterface
{
public function __invoke(ContainerConfigurator $configurator): void
{
$services = $configurator->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
<?php

use oat\generis\persistence\PersistenceManager;

/** @var ContainerInterface $contaner */
$container = $this->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).
3 changes: 3 additions & 0 deletions core/DependencyInjection/ContainerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,5 +60,6 @@
ContainerServiceProvider::class,
LogServiceProvider::class,
ResourceServiceProvider::class,
PersistenceServiceProvider::class,
],
];
48 changes: 20 additions & 28 deletions test/unit/oatbox/mutex/test_action.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<?php

$extRoot = __DIR__ . '/../../../../';
if (is_dir($extRoot.'vendor')) {
require($extRoot.'vendor/autoload.php');
} elseif (is_dir($extRoot.'../vendor')) {
require($extRoot.'../vendor/autoload.php');
}

use oat\oatbox\mutex\LockService;
use oat\oatbox\service\ServiceManager;
use Symfony\Component\Lock\Store\FlockStore;
use oat\oatbox\mutex\NoLockStorage;

// phpcs:disable
require __DIR__ . '/../../../../../vendor/autoload.php';

function getInstance($class, $dir): LockService
{
$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;
}
// 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') {
Expand All @@ -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;
}

0 comments on commit 7bebeea

Please sign in to comment.