Skip to content

Commit

Permalink
Merge branch 'inject-into-provide'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vegner committed Jan 18, 2018
2 parents 30d7f7f + 14c5ca6 commit 57d94f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $config = [], $selfName = '') {
public function connect(ProviderInterface $provider) {
$this->register($provider->getServiceName(), function () use ($provider) {
$this->injectRecursive($provider);
return $provider->provide();
return $this->inject([$provider, 'provide']);
});
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public function get($name) {

private function injectRecursive($consumer) {
if (is_callable($consumer)) {
$parameters = $this->getParameterValues(new \ReflectionFunction($consumer));
$parameters = $this->getParameterValues(new \ReflectionFunction(\Closure::fromCallable($consumer)));
return $consumer(...$parameters);
} else {
$consumer = $this->injectDeclarer($consumer);
Expand Down
10 changes: 10 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ public function testMerge() {
$this->assertSame($request, $consumer->getRequest(), 'Must inject request from request container');
$this->assertSame($mergedContainer, $consumer->getContainer(), 'Must inject merged container');
}

public function testInjectIntoProvider() {
$param = 'R2D2';
$container = new Container([
'param' => $param,
]);
$container->connect(new InjectionReceiverProvider());
$service = $container->get('service');
$this->assertSame($param, $service->param, 'Must inject param into service');
}
}
16 changes: 16 additions & 0 deletions tests/InjectionReceiverProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace tests;

use SD\DependencyInjection\ProviderInterface;

class InjectionReceiverProvider implements ProviderInterface {
public function getServiceName(): string {
return 'service';
}

public function provide(string $param = '') {
return (object)[
'param' => $param,
];
}
}

0 comments on commit 57d94f3

Please sign in to comment.