-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1024 from oat-sa/release-15.21
Release 15.21
- Loading branch information
Showing
6 changed files
with
195 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters