Skip to content

Commit

Permalink
Extension: 'connection' & 'entityFactory' can be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Feb 15, 2021
1 parent 06bca0b commit f79460a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
37 changes: 31 additions & 6 deletions src/LeanMapperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ public function loadConfiguration()
// Services
$connection = $this->configConnection($builder, $config);
$this->configMapper($builder, $config);

$builder->addDefinition($this->prefix('entityFactory'))
->setClass($config['entityFactory']);
$this->configEntityFactory($builder, $config);

// profiler
if ($useProfiler) {
if ($connection && $useProfiler) {
$panel = $builder->addDefinition($this->prefix('panel'))
->setClass(\Dibi\Bridges\Tracy\Panel::class);

Expand All @@ -73,8 +71,14 @@ public function loadConfiguration()
*/
protected function configConnection(ContainerBuilder $builder, array $config)
{
if (!isset($config['connection']) || !is_string($config['connection'])) {
throw new \RuntimeException('Connection class definition is missing, or not (string).');
$connectionClass = $config['connection'];

if ($connectionClass === FALSE || $connectionClass === NULL) {
return NULL;
}

if (!is_string($config['connection'])) {
throw new \RuntimeException('Connection class definition must be string.');
}

return $builder->addDefinition($this->prefix('connection'))
Expand All @@ -92,6 +96,27 @@ protected function configConnection(ContainerBuilder $builder, array $config)
}


/**
* Adds connection service into container
* @return ServiceDefinition
*/
protected function configEntityFactory(ContainerBuilder $builder, array $config)
{
$entityFactoryClass = $config['entityFactory'];

if ($entityFactoryClass === FALSE || $entityFactoryClass === NULL) {
return NULL;
}

if (!is_string($config['entityFactory'])) {
throw new \RuntimeException('EntityFactory class definition must be string.');
}

return $builder->addDefinition($this->prefix('entityFactory'))
->setClass($config['entityFactory']);
}


/**
* Adds mapper service into container
* @return ServiceDefinition|NULL
Expand Down
17 changes: 16 additions & 1 deletion tests/LeanMapperExtension/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class NewsRepository extends LeanMapper\Repository {}
test(function () {
$container = createContainer('readme.entities');

$connection = $container->getService('leanmapper.connection');
Assert::true($connection instanceof \LeanMapper\Connection);

$entityFactory = $container->getService('leanmapper.entityFactory');
Assert::true($entityFactory instanceof \LeanMapper\DefaultEntityFactory);

$newsRepository = $container->getByType(NewsRepository::class);
Assert::true($newsRepository instanceof NewsRepository);

Expand All @@ -82,8 +88,17 @@ test(function () {

// Disable
test(function () {
$container = createContainer('disable-mapper');
$container = createContainer('disable-services');

Assert::exception(function () use ($container) {;
$container->getByType(LeanMapper\Connection::class);
}, Nette\DI\MissingServiceException::class);

Assert::exception(function () use ($container) {;
$container->getByType(LeanMapper\IMapper::class);
}, Nette\DI\MissingServiceException::class);

Assert::exception(function () use ($container) {;
$container->getByType(LeanMapper\IEntityFactory::class);
}, Nette\DI\MissingServiceException::class);
});
2 changes: 0 additions & 2 deletions tests/LeanMapperExtension/config/disable-mapper.neon

This file was deleted.

4 changes: 4 additions & 0 deletions tests/LeanMapperExtension/config/disable-services.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
leanmapper:
mapper: no
connection: no
entityFactory: no

0 comments on commit f79460a

Please sign in to comment.