Skip to content

Commit

Permalink
Container aware types
Browse files Browse the repository at this point in the history
- added interface `ContainerAwareTypeInterface`
- added processing of container aware types in the Nettrine bridge
- updated README
  • Loading branch information
tg666 committed Nov 29, 2020
1 parent 80f7450 commit 11cfcf8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ class FooExtension extends CompilerExtension implements DatabaseTypeProviderInte
}
```

#### Services in Doctrine Types

Doctrine DBAL types don't have access to services by default.
With this extension, you can receive DI Container in your custom types when the Connection is created.

```php
<?php

use Nette\DI\Container;
use Doctrine\DBAL\Types\StringType;
use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface;

final class CustomType extends StringType implements ContainerAwareTypeInterface
{
private $service;

public function setContainer(Container $container) : void
{
$this->service = $container->getByType(MyService::class);
}
}
```

### Entity Mapping Provider

```php
Expand Down
24 changes: 23 additions & 1 deletion src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace SixtyEightPublishers\DoctrineBridge\Bridge\Nettrine\DI;

use Doctrine\DBAL\Types\Type;
use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\PhpLiteral;
use Nette\DI\Definitions\Reference;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\Helpers\MappingHelper;
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use SixtyEightPublishers\DoctrineBridge\DI\EntityMapping;
use SixtyEightPublishers\DoctrineBridge\DI\DatabaseTypeProviderInterface;
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface;
use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface;
use SixtyEightPublishers\DoctrineBridge\DI\EntityMappingProviderInterface;

final class DoctrineBridgeExtension extends CompilerExtension
Expand Down Expand Up @@ -51,9 +55,10 @@ private function processDatabaseTypeProviders(): void
}

$dbalExtensionName = key($dbalExtensions);
$builder = $this->getContainerBuilder();

/** @var \Nette\DI\Definitions\ServiceDefinition $connectionFactory */
$connectionFactory = $this->getContainerBuilder()->getDefinition($dbalExtensionName . '.connectionFactory');
$connectionFactory = $builder->getDefinition($dbalExtensionName . '.connectionFactory');
$factory = $connectionFactory->getFactory();
[$types, $typesMapping] = $factory->arguments;

Expand All @@ -73,6 +78,23 @@ private function processDatabaseTypeProviders(): void

$factory->arguments[0] = $types;
$factory->arguments[1] = $typesMapping;

/** @var \Nette\DI\Definitions\ServiceDefinition $connection */
$connection = $builder->getDefinition($dbalExtensionName . '.connection');

foreach ($types as $typeName => $typeOptions) {
$typeClassName = $typeOptions['class'];

if (!is_subclass_of($typeClassName, ContainerAwareTypeInterface::class, TRUE)) {
continue;
}

$connection->addSetup('?::getType(?)->setContainer(?)', [
new PhpLiteral(Type::class),
$typeName,
new Reference($builder::THIS_CONTAINER),
]);
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Type/ContainerAwareTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\DoctrineBridge\Type;

use Nette\DI\Container;

interface ContainerAwareTypeInterface
{
/**
* @param \Nette\DI\Container $container
*
* @return void
*/
public function setContainer(Container $container): void;
}

0 comments on commit 11cfcf8

Please sign in to comment.