Skip to content

Commit

Permalink
Merge pull request #225 from auraphp/fix_bug_blueprint_order
Browse files Browse the repository at this point in the history
AnnotatedInjectAttributeConfig should only mark for blueprint creation
  • Loading branch information
frederikbosch authored Aug 23, 2024
2 parents 29e7b73 + 20174cb commit 618c73b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/ClassScanner/AnnotatedInjectAttributeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

namespace Aura\Di\ClassScanner;

use Aura\Di\Attribute\AnnotatedInjectInterface;
use Aura\Di\Container;

final class AnnotatedInjectAttributeConfig implements AttributeConfigInterface
{
public static function define(Container $di, AttributeSpecification $attribute, ClassSpecification $class): void
{
if ($attribute->isConstructorParameterAttribute()) {
/** @var AnnotatedInjectInterface $annotatedInject */
$annotatedInject = $attribute->getAttributeInstance();
$di->params[$attribute->getClassName()][$attribute->getTargetParameter()] = $annotatedInject->inject();
$di->params[$attribute->getClassName()] = $di->params[$attribute->getClassName()] ?? [];;
}
}
}
6 changes: 4 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ public function newFactory(
* Note the that container must be locked before creating a new instance.
* This prevents premature resolution of params and setters.
*
* @param string $class The class to instantiate.
* @phpstan-template T
*
* @param class-string<T> $class The class to instantiate.
*
* @param array $mergeParams An array of override parameters; the key may
* be the name *or* the numeric position of the constructor parameter, and
Expand All @@ -499,7 +501,7 @@ public function newFactory(
* name of the setter method to call and the value is the value to be
* passed to the setter method.
*
* @return object
* @return object&T
*
*/
public function newInstance(
Expand Down
18 changes: 17 additions & 1 deletion tests/ClassScanner/ClassScannerConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Aura\Di\ClassScanner;

use Aura\Di\Container;
use Aura\Di\Fake\FakeCompilationTestConfig;
use Aura\Di\Fake\FakeConstructAttributeClass;
use Aura\Di\Fake\FakeControllerClass;
use Aura\Di\Fake\FakeInjectAnnotatedWithClass;
use Aura\Di\Resolver\Reflector;
Expand Down Expand Up @@ -32,7 +34,7 @@ public function testAttributes()

$annotation = $container->values['worker'][0];
$this->assertSame(3, $annotation['someSetting']);
$this->assertSame('Aura\Di\Fake\FakeConstructAttributeClass', $annotation['className']);
$this->assertSame(FakeConstructAttributeClass::class, $annotation['className']);

/** @var FakeInjectAnnotatedWithClass $injectedWith */
$injectedWith = $container->newInstance(FakeInjectAnnotatedWithClass::class);
Expand All @@ -48,4 +50,18 @@ public function testBlueprintApplied()

$this->assertTrue(\array_key_exists(FakeControllerClass::class, $resolver->params));
}

public function testOrder()
{
$resolver = new Resolver(new Reflector());
$container = new Container($resolver);
$container->params[FakeConstructAttributeClass::class]['string'] = 'this_value_should_override_the_attribute';

$testConfig = new FakeCompilationTestConfig();
$testConfig->define($container);
$this->config->define($container);

$instance = $container->newInstance(FakeConstructAttributeClass::class);
$this->assertSame('this_value_should_override_the_attribute', $instance->getString());
}
}

0 comments on commit 618c73b

Please sign in to comment.