Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from chubbyphp/chubbyphp-container
Browse files Browse the repository at this point in the history
chubbyphp-container
  • Loading branch information
dominikzogg committed Nov 28, 2019
2 parents b5ac9d9 + 86e039c commit 1680f35
Show file tree
Hide file tree
Showing 20 changed files with 3,254 additions and 82 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ matrix:
- php: 7.4snapshot
env: dependencies=lowest
- php: 7.4snapshot
allow_failures:
- php: 7.4snapshot

before_script:
- echo "USE mysql;\nUPDATE user SET authentication_string=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@ Doctrine database service providers for doctrine dbal and orm.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-doctrine-db-service-provider][1].

```sh
composer require chubbyphp/chubbyphp-doctrine-db-service-provider "^1.4"
composer require chubbyphp/chubbyphp-doctrine-db-service-provider "^1.5"
```

## Providers
## Usage

* [DoctrineDbalServiceProvider][2]
* [DoctrineOrmServiceProvider][3]
### ServiceFactory (chubbyphp/chubbyphp-container)

* [DoctrineDbalServiceFactory][2]
* [DoctrineOrmServiceFactory][3]

### ServiceProvider (pimple/pimple)

* [DoctrineDbalServiceProvider][4]
* [DoctrineOrmServiceProvider][5]

## Copyright

Dominik Zogg 2018
Dominik Zogg 2019

There is some code with @see, copied with small modifications by from thirdparties.
*There is some code with @see, copied with small modifications by from thirdparties.*

[1]: https://packagist.org/packages/chubbyphp/chubbyphp-doctrine-db-service-provider

[2]: doc/ServiceProvider/DoctrineDbalServiceProvider.md
[3]: doc/ServiceProvider/DoctrineOrmServiceProvider.md
[2]: doc/ServiceFactory/DoctrineDbalServiceFactory.md
[3]: doc/ServiceFactory/DoctrineOrmServiceFactory.md

[4]: doc/ServiceProvider/DoctrineDbalServiceProvider.md
[5]: doc/ServiceProvider/DoctrineOrmServiceProvider.md
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"doctrine/orm": "<2.6.3,>=3.0"
},
"require-dev": {
"chubbyphp/chubbyphp-container": "^1.0",
"chubbyphp/chubbyphp-mock": "^1.4.2",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^2.16.1",
Expand All @@ -45,7 +46,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
"dev-master": "1.5-dev"
}
},
"scripts": {
Expand All @@ -61,8 +62,8 @@
"@test:insights"
],
"test:cs": "mkdir -p build && vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --cache-file=build/phpcs.cache",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=87 --verbose --coverage=build/phpunit",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=92 --disable-security-check | tee build/phpinsights.log; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=86 --verbose --coverage=build/phpunit",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=94 --disable-security-check | tee build/phpinsights.log; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
"test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-result-file=build/phpunit/phpunit.result.cache",
"test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log",
"test:loc": "mkdir -p build && vendor/bin/phploc src --verbose | tee build/phploc.log",
Expand Down
130 changes: 130 additions & 0 deletions doc/ServiceFactory/DoctrineDbalServiceFactory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# DoctrineDbalServiceFactory

The *DoctrineDbalServiceFactory* provides integration with the [Doctrine Dbal][1].

## Parameters

* **doctrine.dbal.db.options**: Array of Doctrine DBAL options.

These options are available:

* **configuration**

* **auto_commit**: Auto commit. Defaults to `true`
* **cache.result**: Array with the cache settings, defaults to `['type' => 'array']`.
Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service:
`$container['doctrine.dbal.db.cache_factory.<type>']`
* **filter_schema_assets_expression**: An expression to filter for schema (tables)
`deprecated, use schema_assets_filter instead`
* **schema_assets_filter**: An filter callable for schema (tables)

* **connection**:

* **charset**: Specifies the charset used when connecting to the database.
Only relevant for `pdo_mysql`, and `pdo_oci/oci8`,
* **dbname**: The name of the database to connect to.
* **driver**: The database driver to use, defaults to `pdo_mysql`.
Can be any of: `pdo_mysql`, `pdo_sqlite`, `pdo_pgsql`,
`pdo_oci`, `oci8`, `ibm_db2`, `pdo_ibm`, `pdo_sqlsrv`.
* **host**: The host of the database to connect to. Defaults to localhost.
* **password**: The password of the database to connect to.
* **path**: Only relevant for `pdo_sqlite`, specifies the path to the SQLite database.
* **port**: Only relevant for `pdo_mysql`, `pdo_pgsql`, and `pdo_oci/oci8`,
* **user**: The user of the database to connect to. Defaults to root.

These and additional options are described in detail in [Doctrine Dbal Configuration][2].

* **doctrine.dbal.types**: Array of dbal types (additional and/or override)
Example: [Type::STRING => StringType::class]

## Services

* **doctrine.dbal.connection_registry**: The connection registry, instance of `Doctrine\Common\Persistence\ConnectionRegistry`.
* **doctrine.dbal.db**: The database connection, instance of `Doctrine\DBAL\Connection`.
* **doctrine.dbal.db.config**: The doctrine configuration, instance of `Doctrine\DBAL\Configuration`.
* **doctrine.dbal.db.event_manager**: The doctrine event manager, instance of `Doctrine\Common\EventManager`.

## Registering

### Single connection

```php
$container = new Container();

$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())());

$container->factory('doctrine.dbal.db.options', static function () {
return [
'connection' => [
'dbname' => 'my_database',
'host' => 'mysql.someplace.tld',
'password' => 'my_password',
'user' => 'my_username',
],
];
});
```

### Multiple connections

```php
$container = new Container();

$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())());

$container->factory('doctrine.dbal.dbs.options', static function () {
return [
'mysql_read' => [
'connection' => [
'dbname' => 'my_database',
'host' => 'mysql.read.someplace.tld',
'password' => 'my_password',
'user' => 'my_username',
],
],
'mysql_write' => [
'connection' => [
'dbname' => 'my_database',
'host' => 'mysql.write.someplace.tld',
'password' => 'my_password',
'user' => 'my_username',
],
],
];
});
```

## Usage

### Single connection

```php
$container->get('doctrine.dbal.db')
->createQueryBuilder()
->select('u')
->from('users', 'u')
->where($qb->expr()->eq('u.username', ':username'))
->setParameter('username', 'john.doe@domain.com')
->execute()
->fetch(\PDO::FETCH_ASSOC);
```

### Multiple connections

```php
$container->get('doctrine.dbal.dbs')->get('name')
->createQueryBuilder()
->select('u')
->from('users', 'u')
->where($qb->expr()->eq('u.username', ':username'))
->setParameter('username', 'john.doe@domain.com')
->execute()
->fetch(\PDO::FETCH_ASSOC);
```

## Copyright

Dominik Zogg <dominik.zogg@gmail.com>

[1]: https://www.doctrine-project.org/projects/dbal
[2]: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
190 changes: 190 additions & 0 deletions doc/ServiceFactory/DoctrineOrmServiceFactory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# DoctrineOrmServiceFactory

The *DoctrineOrmServiceFactory* provides integration with the [Doctrine ORM][1].

## Install

```sh
composer require doctrine/orm "^2.5"
```

## Parameters

* **doctrine.orm.em.options**: Array of Doctrine ORM options.

These options are available:

* **cache.hydration**: Array with the cache settings, defaults to `['type' => 'array']`.
Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service:
`$container['doctrine.dbal.db.cache_factory.<type>']`
* **cache.metadata**: Array with the cache settings, defaults to `['type' => 'array']`.
Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service:
`$container['doctrine.dbal.db.cache_factory.<type>']`
* **cache.query**: Array with the cache settings, defaults to `['type' => 'array']`.
Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service:
`$container['doctrine.dbal.db.cache_factory.<type>']`
* **class_metadata.factory.name**: String with class, defaults to `Doctrine\ORM\Mapping\ClassMetadataFactory`.
* **connection**: The connection name of the Doctrine DBAL configuration. Defaults to `default`.
* **custom.functions.datetime**: Array of datetime related [custom functions][2].
* **custom.functions.numeric**: Array of numeric related [custom functions][2].
* **custom.functions.string**: Array of string related [custom functions][2].
* **custom.hydration_modes**: Array of [hydration modes][3].
* **entity.listener_resolver**: String with the resolver type, defaults to `default`.
Add additional resolvers by adding new service:
`$container['doctrine.orm.entity.listener_resolver.<type>']`.
* **mappings**: Array of Mappings.
* **type**: The mapping driver to use.
Can be any of: `annotation`, `class_map`, `php`, `simple_yaml`, `simple_xml`, `static_php`, `yaml` or `xml`.
Add additional mapping driver factories by adding new service:
`$container['doctrine.orm.mapping_driver.factory.<type>']`
* **namespace**: The entity namespace. Example: `One\Entity`
* **path**: The path to the entities. Example: `/path/to/project/One/Entity`
* **alias**: The entity alias to the namespace. Example: `Alias\Entity`
* **extension**: The file extension to search for mappings. Example: `.dcm.xml`
* **proxies.auto_generate**: Enable or disable the auto generation of proxies. Defaults to `true`.
* **proxies.dir**: The directory where generated proxies get saved. Example: `var/cache/doctrine/orm/proxies`.
* **proxies.namespace**: The namespace of generated proxies. Defaults to `DoctrineProxy`.
* **query_hints**: Array of [query hints][4].
* **repository.default.class**: String with class, defaults to `Doctrine\ORM\EntityRepository`.
* **repository.factory**: String with the repository factory type, defaults to `default`.
Add additional repository factories by adding new service: `$container['doctrine.orm.repository.factory.<type>']`.
* **second_level_cache.enabled**: Enable or disable second level cache, defaults to `false`.
* **second_level_cache**: Array with the cache settings, defaults to `['type' => 'array']`.
Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service:
`$container['doctrine.dbal.db.cache_factory.<type>']`
* **strategy.naming**: String with the naming strategy type, defaults to `default`.
Add additional naming stratigies by adding new service: `$container['doctrine.orm.strategy.naming.<type>']`.
* **strategy.quote**: String with the quote strategy type, defaults to `default`.
Add additional quote stratigies by adding new service: `$container['doctrine.orm.strategy.quote.<type>']`.

## Services

* **doctrine.orm.em**: The entity manager, instance of `Doctrine\ORM\EntityManager`.
* **doctrine.orm.em.config**: Configuration object for Doctrine. Defaults to an empty `Doctrine\ORM\Configuration`.
* **doctrine.orm.manager_registry**: The manager registry, instance of `Doctrine\Common\Persistence\ManagerRegistry`.

## Registering

### Single connection

```php
$container = new Container();

$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())());
$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineOrmServiceFactory())());

$container->factory('doctrine.dbal.db.options', static function () {
return [
'connection' => [
'driver' => 'pdo_mysql',
'host' => 'mysql.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8mb4',
],
];
});

$container->factory('doctrine.orm.em.options', static function () {
return [
'mappings' => [
[
'type' => 'annotation',
'namespace' => 'One\Entity',
'path' => __DIR__.'/src/One/Entity',
]
],
];
});
```

### Multiple connections

```php
$container = new Container();

$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())());
$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineOrmServiceFactory())());

$container->factory('doctrine.dbal.dbs.options', static function () {
return [
'mysql_read' => [
'connection' => [
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8mb4',
],
],
'mysql_write' => [
'connection' => [
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8mb4',
],
],
];
});

$container->factory('doctrine.orm.ems.options', static function () {
return [
'mysql_read' => [
'connection' => 'mysql_read',
'mappings' => [
[
'type' => 'annotation',
'namespace' => 'One\Entity',
'alias' => 'One',
'path' => __DIR__.'/src/One/Entity',
'use_simple_annotation_reader' => false,
],
],
],
'mysql_write' => [
'connection' => 'mysql_write',
'mappings' => [
[
'type' => 'annotation',
'namespace' => 'One\Entity',
'path' => __DIR__.'/src/One/Entity',
'use_simple_annotation_reader' => false,
],
],
],
];
});
```

## Usage

### Single connection

```php
$container->get('doctrine.orm.em')
->getRepository(User::class)
->findOneBy(['username' => 'john.doe@domain.com']);
```

### Multiple connections

```php
$container->get('doctrine.orm.ems')->get('name')
->getRepository(User::class)
->findOneBy(['username' => 'john.doe@domain.com']);
```

## Copyright

Dominik Zogg <dominik.zogg@gmail.com>

[1]: https://www.doctrine-project.org/projects/orm
[2]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html
[3]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes
[4]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#query-hints

Loading

0 comments on commit 1680f35

Please sign in to comment.