Skip to content

Commit

Permalink
drop annotated with
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed May 21, 2024
1 parent 3b27190 commit d36eb92
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $di->params['Example']['foo'] = $di->lazyNew(Foo::class);

## Value Attribute

If the parameter is annotated with the `#[Value]` attribute, you define a new value that should be injected.
If a parameter is annotated with the `#[Value]` attribute, you define that a value should be injected.

For example, look at the following class; the `$foo` constructor parameter has such an annotation:

Expand Down
31 changes: 0 additions & 31 deletions src/Attribute/AnnotatedWith.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Attribute/DefineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace Aura\Di\Attribute;

use Aura\Di\Resolver\Resolver;

interface DefineInterface
{
public function define(\Reflector $reflector): mixed;
public function define(Resolver $resolver, \Reflector $reflector): void;
}
8 changes: 4 additions & 4 deletions src/Resolver/AnnotatedResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function detectAnnotatedClasses(Resolver $resolver): void
foreach ($reflectionClass->getAttributes(DefineInterface::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
/** @var DefineInterface $instance */
$instance = $attribute->newInstance();
$resolver->annotations[$attribute->getName()][] = $instance->define($reflectionClass);
$instance->define($resolver, $reflectionClass);
}

$methods = $reflectionClass->getMethods();
Expand All @@ -110,7 +110,7 @@ protected function detectAnnotatedClasses(Resolver $resolver): void
foreach ($parameter->getAttributes(DefineInterface::class) as $attribute) {
/** @var DefineInterface $instance */
$instance = $attribute->newInstance();
$resolver->annotations[$attribute->getName()][] = $instance->define($parameter);
$instance->define($resolver, $parameter);
}
}
}
Expand All @@ -120,7 +120,7 @@ protected function detectAnnotatedClasses(Resolver $resolver): void
foreach ($property->getAttributes(DefineInterface::class) as $attribute) {
/** @var DefineInterface $instance */
$instance = $attribute->newInstance();
$resolver->annotations[$attribute->getName()][] = $instance->define($property);
$instance->define($resolver, $property);
}
}

Expand All @@ -130,7 +130,7 @@ protected function detectAnnotatedClasses(Resolver $resolver): void
foreach ($constant->getAttributes(DefineInterface::class) as $attribute) {
/** @var DefineInterface $instance */
$instance = $attribute->newInstance();
$resolver->annotations[$attribute->getName()][] = $instance->define($constant);
$instance->define($resolver, $constant);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
*
* @property array $values
*
* @property array $annotations
*
*/
class Resolver
{
Expand Down Expand Up @@ -93,15 +91,6 @@ class Resolver
*/
protected array $values = [];

/**
*
* Annotations that need to be injected inside objects annotated with AnnotatedWith.
*
* @var array
*
*/
protected array $annotations = [];

/**
*
* Constructor params and setter definitions, unified across class
Expand Down
4 changes: 2 additions & 2 deletions tests/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function testCompilationSerialization()
$this->assertCount(1, $injectedWith->getWorkers());

$annotation = $injectedWith->getWorkers()[0];
$this->assertSame(3, $annotation->getSomeSetting());
$this->assertSame('Aura\Di\Fake\FakeConstructAttributeClass', $annotation->getClassName());
$this->assertSame(3, $annotation['someSetting']);
$this->assertSame('Aura\Di\Fake\FakeConstructAttributeClass', $annotation['className']);
}
}
4 changes: 2 additions & 2 deletions tests/Fake/FakeInjectAnnotatedWithClass.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
namespace Aura\Di\Fake;

use Aura\Di\Attribute\AnnotatedWith;
use Aura\Di\Attribute\Value;

class FakeInjectAnnotatedWithClass
{
private array $workers;

public function __construct(
#[AnnotatedWith(FakeWorkerAttribute::class)]
#[Value('worker')]
array $workers,
) {
$this->workers = $workers;
Expand Down
22 changes: 6 additions & 16 deletions tests/Fake/FakeWorkerAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,25 @@
namespace Aura\Di\Fake;

use Aura\Di\Attribute\DefineInterface;
use Aura\Di\Resolver\Resolver;

#[\Attribute]
class FakeWorkerAttribute implements DefineInterface
{
private int $someSetting;
private ?string $className = null;

public function __construct(int $someSetting = 1)
{
$this->someSetting = $someSetting;
}

public function define(\Reflector $reflector): mixed
public function define(Resolver $resolver, \Reflector $reflector): void
{
if ($reflector instanceof \ReflectionClass) {
$this->className = $reflector->getName();
return $this;
$resolver->values['worker'][] = [
'someSetting' => $this->someSetting,
'className' => $reflector->getName(),
];
}

throw new \UnexpectedValueException('FakeWorkerAttribute is only applicable for methods');
}

public function getSomeSetting(): int
{
return $this->someSetting;
}

public function getClassName(): ?string
{
return $this->className;
}
}
15 changes: 8 additions & 7 deletions tests/Resolver/AnnotatedResolverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AnnotatedResolverFactoryTest extends TestCase
protected function setUp(): void
{
parent::setUp();

$annotatedResolverFactory = new AnnotatedResolverFactory(
new ResolverFactory(),
['Aura\Di\Fake'],
Expand All @@ -33,11 +34,11 @@ public function testAnnotatedWith()
{
$this->resolver->compile();

$this->assertCount(1, $this->resolver->annotations);
$this->assertCount(1, $this->resolver->values['worker']);

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

/** @var FakeInjectAnnotatedWithClass $injectedWith */
$injectedWith = $this->resolver->resolve(new Blueprint(FakeInjectAnnotatedWithClass::class));
Expand All @@ -52,9 +53,9 @@ public function testAttributes()
$this->resolver->values['fake.value'] = 'value';
$this->resolver->compile();

$this->assertCount(1, $this->resolver->annotations);
$this->assertSame(3, $this->resolver->annotations[FakeWorkerAttribute::class][0]->getSomeSetting());
$this->assertSame('Aura\Di\Fake\FakeConstructAttributeClass', $this->resolver->annotations[FakeWorkerAttribute::class][0]->getClassName());
$this->assertCount(1, $this->resolver->values['worker']);
$this->assertSame(3, $this->resolver->values['worker'][0]['someSetting']);
$this->assertSame('Aura\Di\Fake\FakeConstructAttributeClass', $this->resolver->values['worker'][0]['className']);

$actual = $this->resolver->resolve(new Blueprint('Aura\Di\Fake\FakeConstructAttributeClass'));
$this->assertInstanceOf('Aura\Di\Fake\FakeInterfaceClass1', $actual->getFakeService());
Expand Down

0 comments on commit d36eb92

Please sign in to comment.