Skip to content

Commit

Permalink
Separate chain calls (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed May 22, 2022
1 parent c6fc044 commit f23e8b1
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 64 deletions.
19 changes: 12 additions & 7 deletions README.md
Expand Up @@ -253,7 +253,9 @@ class CarFactoryProvider extends ServiceProviderInterface
return [
// Note that Garage should already be defined in container
Garage::class => function(ContainerInterface $container, Garage $garage) {
$car = $container->get(CarFactory::class)->create();
$car = $container
->get(CarFactory::class)
->create();
$garage->setCar($car);

return $garage;
Expand Down Expand Up @@ -362,7 +364,9 @@ while ($request = $psr7->acceptRequest()) {
$response = $application->handle($request);
$psr7->respond($response);
$application->afterEmit($response);
$container->get(\Yiisoft\Di\StateResetter::class)->reset();
$container
->get(\Yiisoft\Di\StateResetter::class)
->reset();
gc_collect_cycles();
}
```
Expand Down Expand Up @@ -449,11 +453,12 @@ In order to configure delegates use additional config:
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()->withDelegates([
function (ContainerInterface $container): ContainerInterface {
// ...
}
]);
$config = ContainerConfig::create()
->withDelegates([
function (ContainerInterface $container): ContainerInterface {
// ...
}
]);


$container = new Container($config);
Expand Down
3 changes: 2 additions & 1 deletion src/ExtensibleService.php
Expand Up @@ -62,7 +62,8 @@ public function addExtension(callable $closure): void
public function resolve(ContainerInterface $container)
{
/** @var mixed $service */
$service = DefinitionNormalizer::normalize($this->definition, $this->id)->resolve($container);
$service = DefinitionNormalizer::normalize($this->definition, $this->id)
->resolve($container);

foreach ($this->extensions as $extension) {
/** @var mixed $result */
Expand Down
60 changes: 48 additions & 12 deletions tests/Benchmark/ContainerBench.php
Expand Up @@ -77,14 +77,38 @@ public function before(): void

$this->composite = new CompositeContainer();
// We attach the dummy containers multiple times, to see what would happen if we have lots of them.
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions2)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions3)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions2)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions3)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions2)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions3)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions2)));
$this->composite->attach(new Container(ContainerConfig::create()->withDefinitions($definitions3)));
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions2))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions3))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions2))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions3))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions2))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions3))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions2))
);
$this->composite->attach(new Container(
ContainerConfig::create()
->withDefinitions($definitions3))
);
}

/**
Expand All @@ -99,7 +123,10 @@ public function benchConstruct(): void
for ($i = 0; $i < self::SERVICE_COUNT; $i++) {
$definitions["service$i"] = PropertyTestClass::class;
}
$container = new Container(ContainerConfig::create()->withDefinitions($definitions));
$container = new Container(
ContainerConfig::create()
->withDefinitions($definitions)
);
}

/**
Expand All @@ -115,7 +142,10 @@ public function benchSequentialLookups($params): void
if (isset($params['otherDefinitions'])) {
$definitions = array_merge($definitions, $params['otherDefinitions']);
}
$container = new Container(ContainerConfig::create()->withDefinitions($definitions));
$container = new Container(
ContainerConfig::create()
->withDefinitions($definitions)
);
for ($i = 0; $i < self::SERVICE_COUNT / 2; $i++) {
// Do array lookup.
$index = $this->indexes[$i];
Expand All @@ -136,7 +166,10 @@ public function benchRandomLookups($params): void
if (isset($params['otherDefinitions'])) {
$definitions = array_merge($definitions, $params['otherDefinitions']);
}
$container = new Container(ContainerConfig::create()->withDefinitions($definitions));
$container = new Container(
ContainerConfig::create()
->withDefinitions($definitions)
);
for ($i = 0; $i < self::SERVICE_COUNT / 2; $i++) {
// Do array lookup.
$index = $this->randomIndexes[$i];
Expand All @@ -157,7 +190,10 @@ public function benchRandomLookupsComposite($params): void
if (isset($params['otherDefinitions'])) {
$definitions = array_merge($definitions, $params['otherDefinitions']);
}
$container = new Container(ContainerConfig::create()->withDefinitions($definitions));
$container = new Container(
ContainerConfig::create()
->withDefinitions($definitions)
);
$this->composite->attach($container);
for ($i = 0; $i < self::SERVICE_COUNT / 2; $i++) {
// Do array lookup.
Expand Down
5 changes: 4 additions & 1 deletion tests/Benchmark/ContainerMethodHasBench.php
Expand Up @@ -37,7 +37,10 @@ public function before(): void
}
$definitions['service'] = PropertyTestClass::class;

$this->container = new Container(ContainerConfig::create()->withDefinitions($definitions));
$this->container = new Container(
ContainerConfig::create()
->withDefinitions($definitions)
);
}

public function benchPredefinedExisting(): void
Expand Down
46 changes: 39 additions & 7 deletions tests/Unit/CompositePsrContainerOverYiisoftTest.php
Expand Up @@ -58,20 +58,52 @@ public function testResetterInCompositeContainerWithExternalResetter(): void

$engineMarkOne = $composite->get('engineMarkOne');
$engineMarkTwo = $composite->get('engineMarkTwo');
$this->assertSame(42, $composite->get('engineMarkOne')->getNumber());
$this->assertSame(43, $composite->get('engineMarkTwo')->getNumber());
$this->assertSame(
42,
$composite
->get('engineMarkOne')
->getNumber(),
);
$this->assertSame(
43,
$composite
->get('engineMarkTwo')
->getNumber(),
);

$engineMarkOne->setNumber(45);
$engineMarkTwo->setNumber(46);
$this->assertSame(45, $composite->get('engineMarkOne')->getNumber());
$this->assertSame(46, $composite->get('engineMarkTwo')->getNumber());
$this->assertSame(
45,
$composite
->get('engineMarkOne')
->getNumber(),
);
$this->assertSame(
46,
$composite
->get('engineMarkTwo')
->getNumber(),
);

$composite->get(StateResetter::class)->reset();
$composite
->get(StateResetter::class)
->reset();

$this->assertSame($engineMarkOne, $composite->get('engineMarkOne'));
$this->assertSame($engineMarkTwo, $composite->get('engineMarkTwo'));
$this->assertSame(42, $composite->get('engineMarkOne')->getNumber());
$this->assertSame(43, $composite->get('engineMarkTwo')->getNumber());
$this->assertSame(
42,
$composite
->get('engineMarkOne')
->getNumber(),
);
$this->assertSame(
43,
$composite
->get('engineMarkTwo')
->getNumber(),
);
}

public function testNotFoundException(): void
Expand Down

0 comments on commit f23e8b1

Please sign in to comment.