Skip to content

Commit

Permalink
馃摝 Container compilation and delegators #52
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie NEHME committed Jan 13, 2022
1 parent fa50c6a commit 0fe984b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## V8.1.1 - TBD

### Added

- Nothing

### Changed

- [#52](https://github.com/elie29/zend-di-config/issues/52) Container compilation and delegators

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## V8.1.0 - 2021-12-16

### Added
Expand Down
29 changes: 20 additions & 9 deletions src/Config.php
Expand Up @@ -5,6 +5,7 @@
namespace Elie\PHPDI\Config;

use DI\ContainerBuilder;
use DI\Definition\Helper\DefinitionHelper;
use Psr\Container\ContainerInterface;

use function DI\autowire;
Expand Down Expand Up @@ -137,19 +138,29 @@ private function addDelegators(): void
foreach ($delegators as $delegator) {
$previous = $name . '-' . ++$this->delegatorCounter;
$this->definitions[$previous] = $this->definitions[$name];
$current =
function (ContainerInterface $container) use ($delegator, $previous, $name) {
$factory = new $delegator();
$callable = function () use ($previous, $container) {
return $container->get($previous);
};
return $factory($container, $name, $callable);
};
$this->definitions[$name] = $current;
$this->definitions[$name] = $this->createDelegatorFactory($delegator, $previous, $name);
}
}
}

private function createDelegatorFactory(string $delegator, string $previous, string $name): DefinitionHelper
{
return factory(function (
ContainerInterface $container,
string $delegator,
string $previous,
string $name
) {
$factory = new $delegator();
$callable = function () use ($previous, $container) {
return $container->get($previous);
};
return $factory($container, $name, $callable);
})->parameter('delegator', $delegator)
->parameter('previous', $previous)
->parameter('name', $name);
}

private function useAutowire(ContainerBuilder $builder): void
{
// default autowire is true
Expand Down

0 comments on commit 0fe984b

Please sign in to comment.