From f79460a51c45df22b48d5779725f84b7bf5d7bd7 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Sun, 10 Jan 2021 11:12:28 +0100 Subject: [PATCH] Extension: 'connection' & 'entityFactory' can be disabled --- src/LeanMapperExtension.php | 37 ++++++++++++++++--- tests/LeanMapperExtension/Extension.phpt | 17 ++++++++- .../config/disable-mapper.neon | 2 - .../config/disable-services.neon | 4 ++ 4 files changed, 51 insertions(+), 9 deletions(-) delete mode 100644 tests/LeanMapperExtension/config/disable-mapper.neon create mode 100644 tests/LeanMapperExtension/config/disable-services.neon diff --git a/src/LeanMapperExtension.php b/src/LeanMapperExtension.php index 84bdb0d..be6583f 100644 --- a/src/LeanMapperExtension.php +++ b/src/LeanMapperExtension.php @@ -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); @@ -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')) @@ -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 diff --git a/tests/LeanMapperExtension/Extension.phpt b/tests/LeanMapperExtension/Extension.phpt index 01dd6d8..aa58b75 100644 --- a/tests/LeanMapperExtension/Extension.phpt +++ b/tests/LeanMapperExtension/Extension.phpt @@ -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); @@ -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); }); diff --git a/tests/LeanMapperExtension/config/disable-mapper.neon b/tests/LeanMapperExtension/config/disable-mapper.neon deleted file mode 100644 index 965602d..0000000 --- a/tests/LeanMapperExtension/config/disable-mapper.neon +++ /dev/null @@ -1,2 +0,0 @@ -leanmapper: - mapper: no diff --git a/tests/LeanMapperExtension/config/disable-services.neon b/tests/LeanMapperExtension/config/disable-services.neon new file mode 100644 index 0000000..4ca660d --- /dev/null +++ b/tests/LeanMapperExtension/config/disable-services.neon @@ -0,0 +1,4 @@ +leanmapper: + mapper: no + connection: no + entityFactory: no