Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More connections #47

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions src/DI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Nette\DI\Helpers;
use Nette\DI\Statement;
use Nette\InvalidArgumentException;
use Nette\Utils\Strings;
use Nettrine\ORM\EntityManagerDecorator;
use Nettrine\ORM\Exception\Logical\InvalidStateException;
use Nettrine\ORM\ManagerRegistry;
Expand Down Expand Up @@ -129,26 +130,42 @@ public function loadEntityManagerConfiguration(): void
throw new InvalidStateException(sprintf('EntityManagerDecorator class "%s" not found', $entityManagerDecoratorClass));
}

// Entity Manager
$original = $builder->addDefinition($this->prefix('entityManager'))
->setType(DoctrineEntityManager::class)
->setFactory(DoctrineEntityManager::class . '::create', [
$builder->getDefinitionByType(Connection::class), // Nettrine/DBAL
$this->prefix('@configuration'),
])
->setAutowired(false);

// Entity Manager Decorator
$builder->addDefinition($this->prefix('entityManagerDecorator'))
->setFactory($entityManagerDecoratorClass, [$original]);

// ManagerRegistry
$builder->addDefinition($this->prefix('managerRegistry'))
->setType(ManagerRegistry::class)
->setArguments([
$builder->getDefinitionByType(Connection::class),
$this->prefix('@entityManagerDecorator'),
]);
foreach ($builder->findByType(\Doctrine\DBAL\Connection::class) as $definitionName => $serviceDefinition) {
$match = Strings::match($definitionName, '~([a-zA-Z]+\.([a-zA-Z]+))\.connection~');
Copy link
Contributor

@mabar mabar Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it would work only if connection is defined with name matching this regex? Seems like hidden dependency on Nettrine\DBAL\DI\DbalExtension for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mabar any another idea, how to get connection name from DBAL?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$definitionName is all you need.


if ($serviceDefinition->getTag(\Nettrine\DBAL\DI\DbalExtension::TAG_CONNECTION) !== null && array_key_exists(1, $match) && array_key_exists(2, $match)) {
$nameWithPrefix = $match[1];
$name = $match[2];

} else {
continue;
}

// Entity Manager
$original = $builder->addDefinition($this->prefix($name . '.entityManager'))
->setType(DoctrineEntityManager::class)
->setFactory(DoctrineEntityManager::class . '::create', [
$builder->getDefinition($nameWithPrefix . '.connection'), // Nettrine/DBAL
$this->prefix('@configuration'),
])
->setAutowired(false);

$autowired = $name === \Nettrine\DBAL\DI\DbalExtension::DEFAULT_CONNECTION_NAME ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$serviceDefinition->isAutowired()


// Entity Manager Decorator
$builder->addDefinition($this->prefix($name . '.entityManagerDecorator'))
->setFactory($entityManagerDecoratorClass, [$original])
->setAutowired($autowired);

// ManagerRegistry
$builder->addDefinition($this->prefix($name . '.managerRegistry'))
->setType(ManagerRegistry::class)
->setArguments([
$builder->getDefinition($nameWithPrefix . '.connection'),
$this->prefix('@' . $name . '.entityManagerDecorator'),
])
->setAutowired($autowired);
}
}

}