Skip to content

Commit

Permalink
Allow to disbale use autowire #35
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Dec 24, 2018
1 parent 64da22c commit 0e505b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
])
Expand Down
8 changes: 8 additions & 0 deletions src/Config.php
Expand Up @@ -31,6 +31,7 @@ public function configureContainer(ContainerBuilder $builder): void
$this->addDefinitions($builder);
}

$this->useAutowire($builder);
$this->enableCache($builder);
}

Expand Down Expand Up @@ -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])) {
Expand Down
1 change: 1 addition & 0 deletions src/ConfigInterface.php
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions tests/ConfigTest.php
Expand Up @@ -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'];
Expand Down

0 comments on commit 0e505b6

Please sign in to comment.