Skip to content

Commit c6c92ec

Browse files
authored
Merge pull request #156 from shadowhand/fix/never-cache-closure-params
Prevent caching of delegate closures
2 parents 54b2fed + a9600a7 commit c6c92ec

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/CachingReflector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getParamTypeHint(\ReflectionFunctionAbstract $function, \Reflect
7575
$paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}";
7676
} else {
7777
$lowFunc = strtolower($function->name);
78-
$paramCacheKey = ($lowFunc !== '{closure}')
78+
$paramCacheKey = (strpos($lowFunc, '{closure}') === false)
7979
? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}"
8080
: null;
8181
}

test/InjectorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,4 +1112,21 @@ public function testChildWithoutConstructorMissingParam() {
11121112
$injector->define('Auryn\Test\ParentWithConstructor', array(':foo' => 'parent'));
11131113
$injector->make('Auryn\Test\ChildWithoutConstructor');
11141114
}
1115+
1116+
public function testInstanceClosureDelegates()
1117+
{
1118+
$injector = new Injector;
1119+
$injector->delegate('Auryn\Test\DelegatingInstanceA', function (DelegateA $d) {
1120+
return new \Auryn\Test\DelegatingInstanceA($d);
1121+
});
1122+
$injector->delegate('Auryn\Test\DelegatingInstanceB', function (DelegateB $d) {
1123+
return new \Auryn\Test\DelegatingInstanceB($d);
1124+
});
1125+
1126+
$a = $injector->make('Auryn\Test\DelegatingInstanceA');
1127+
$b = $injector->make('Auryn\Test\DelegatingInstanceB');
1128+
1129+
$this->assertInstanceOf('Auryn\Test\DelegateA', $a->a);
1130+
$this->assertInstanceOf('Auryn\Test\DelegateB', $b->b);
1131+
}
11151132
}

test/fixtures.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,3 +713,17 @@ function __construct($foo) {
713713

714714
class ChildWithoutConstructor extends ParentWithConstructor {
715715
}
716+
717+
class DelegateA {}
718+
class DelegatingInstanceA {
719+
public function __construct(DelegateA $a) {
720+
$this->a = $a;
721+
}
722+
}
723+
724+
class DelegateB {}
725+
class DelegatingInstanceB {
726+
public function __construct(DelegateB $b) {
727+
$this->b = $b;
728+
}
729+
}

0 commit comments

Comments
 (0)