Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnnotatedInjectAttributeConfig should only mark for blueprint creation #225

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading