diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b4ad47..99dd5fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#35](https://github.com/elie29/zend-di-config/issues/35) Allow to disbale use autowire. ### Changed diff --git a/README.md b/README.md index a9b99d4..a7c31e8 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ $container = $factory( // Enable compilation Config::DI_CACHE_PATH => __DIR__, // Folder path + // Disable autowire (enabled by default) + Config::USE_AUTOWIRE => false + // Enable cache Config::ENABLE_CACHE_DEFINITION => false, // boolean, true if APCu is activated ]) diff --git a/src/Config.php b/src/Config.php index 0d9c3d4..074de8a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -31,6 +31,7 @@ public function configureContainer(ContainerBuilder $builder): void $this->addDefinitions($builder); } + $this->useAutowire($builder); $this->enableCache($builder); } @@ -131,6 +132,13 @@ private function addDelegators(): void } } + private function useAutowire(ContainerBuilder $builder): void + { + // default autowire is true + $autowire = $this->definitions[$this::CONFIG][$this::USE_AUTOWIRE] ?? true; + $builder->useAutowiring($autowire); + } + private function enableCache(ContainerBuilder $builder): void { if (! empty($this->definitions[$this::CONFIG][$this::ENABLE_CACHE_DEFINITION])) { diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index a361138..31eb263 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -12,6 +12,7 @@ interface ConfigInterface public const CONFIG = 'config'; public const DI_CACHE_PATH = 'di_cache_path'; public const ENABLE_CACHE_DEFINITION = 'enable_cache_definition'; + public const USE_AUTOWIRE = 'use_autowire'; public function configureContainer(ContainerBuilder $builder): void; } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 89cb2d6..3c7fbcd 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -32,6 +32,16 @@ public function testConfigurationEnableCache() $config->configureContainer($builder); } + public function testConfigurationDisableAutowire() + { + $builder = $this->createMock(ContainerBuilder::class); + + $builder->expects($this->once())->method('useAutowiring'); + + $config = new Config([Config::USE_AUTOWIRE => false]); + $config->configureContainer($builder); + } + public function testConfigurationKeysValues() { $config = ['a' => new \DateTime(), 'b' => [1, 2, 3], 'c' => 'd'];