diff --git a/README.md b/README.md index e45b1d0..3b87509 100644 --- a/README.md +++ b/README.md @@ -162,3 +162,61 @@ class JaxonExample extends \Jaxon\App\CallableClass } } ``` + +### @di + +It defines an attribute that will be injected in a class. +It takes the name and the class of the attribute as mandatory parameters. +It applies to methods and classes. + +```php +class JaxonExample extends \Jaxon\App\CallableClass +{ + /** + * @var \App\Services\Translator + */ + protected $translator; + + /** + * @di('attr' => 'translator', class => '\App\Services\Translator') + */ + public function translate(string $phrase) + { + // The $translator property is set from the DI container when this method is called. + $phrase = $this->translator->translate($phrase); + } +} +``` + +If the class name does not start with a `\`, then the corresponding fully qualified name (FQN) will be set using +either the `use` instructions or the `namespace` in its source file. + +```php +namespace App\Ajax; + +use App\Services\Translator; + +class JaxonExample extends \Jaxon\App\CallableClass +{ + /** + * @var Translator + */ + protected $translator; + + /** + * @var Formatter + */ + protected $formatter; + + /** + * @di('attr' => 'translator', class => 'Translator') + * @di('attr' => 'formatter', class => 'Formatter') + */ + public function translate(string $phrase) + { + // The Translator FQN is defined by the use instruction => App\Services\Translator. + // The Formatter FQN is defined by the current namespace => App\Ajax\Formatter. + $phrase = $this->formatter->format($this->translator->translate($phrase)); + } +} +``` diff --git a/src/Annotation/ContainerAnnotation.php b/src/Annotation/ContainerAnnotation.php index 581e963..0a10e31 100755 --- a/src/Annotation/ContainerAnnotation.php +++ b/src/Annotation/ContainerAnnotation.php @@ -14,17 +14,20 @@ namespace Jaxon\Annotations\Annotation; use mindplay\annotations\AnnotationException; +use mindplay\annotations\AnnotationFile; +use mindplay\annotations\IAnnotationFileAware; use function count; use function is_array; use function is_string; +use function ltrim; /** * Specifies attributes to inject into a callable object. * * @usage('class' => true, 'method'=>true, 'multiple'=>true, 'inherited'=>true) */ -class ContainerAnnotation extends AbstractAnnotation +class ContainerAnnotation extends AbstractAnnotation implements IAnnotationFileAware { /** * The attribute name @@ -40,6 +43,19 @@ class ContainerAnnotation extends AbstractAnnotation */ protected $sClass = ''; + /** + * @var AnnotationFile + */ + protected $xClassFile; + + /** + * @inheritDoc + */ + public function setAnnotationFile(AnnotationFile $file) + { + $this->xClassFile = $file; + } + /** * @inheritDoc * @throws AnnotationException @@ -54,7 +70,7 @@ public function initAnnotation(array $properties) 'and a property "class" of type string'); } $this->sAttr = $properties['attr']; - $this->sClass = $properties['class']; + $this->sClass = ltrim($this->xClassFile->resolveType($properties['class']), '\\'); } /** diff --git a/tests/App/Ajax/Annotated.php b/tests/App/Ajax/Annotated.php index 58ab032..936aaed 100644 --- a/tests/App/Ajax/Annotated.php +++ b/tests/App/Ajax/Annotated.php @@ -3,6 +3,7 @@ namespace Jaxon\Annotations\Tests\App\Ajax; use Jaxon\Annotations\Tests\App\CallableClass; +use Jaxon\Annotations\Tests\Service\ColorService; class Annotated extends CallableClass { @@ -94,15 +95,16 @@ public function cbParams() } /** - * @di('attr' => 'colorService', 'class' => 'Jaxon\Annotations\Tests\Service\ColorService') + * @di('attr' => 'colorService', 'class' => 'ColorService') + * @di('attr' => 'fontService', 'class' => 'FontService') */ public function di1() { } /** - * @di('attr' => 'colorService', 'class' => 'Jaxon\Annotations\Tests\Service\ColorService') - * @di('attr' => 'textService', 'class' => 'Jaxon\Annotations\Tests\Service\TextService') + * @di('attr' => 'colorService', 'class' => 'ColorService') + * @di('attr' => 'textService', 'class' => '\Jaxon\Annotations\Tests\Service\TextService') */ public function di2() { diff --git a/tests/App/Ajax/ClassAnnotated.php b/tests/App/Ajax/ClassAnnotated.php index 55915d3..a94592f 100644 --- a/tests/App/Ajax/ClassAnnotated.php +++ b/tests/App/Ajax/ClassAnnotated.php @@ -3,6 +3,7 @@ namespace Jaxon\Annotations\Tests\App\Ajax; use Jaxon\Annotations\Tests\App\CallableClass; +use Jaxon\Annotations\Tests\Service\TextService; /** * @exclude(false) @@ -13,8 +14,9 @@ * @after('call' => 'funcAfter1') * @after('call' => 'funcAfter2') * @after('call' => 'funcAfter3') - * @di('attr' => 'colorService', 'class' => 'Jaxon\Annotations\Tests\Service\ColorService') - * @di('attr' => 'textService', 'class' => 'Jaxon\Annotations\Tests\Service\TextService') + * @di('attr' => 'colorService', 'class' => '\Jaxon\Annotations\Tests\Service\ColorService') + * @di('attr' => 'textService', 'class' => 'TextService') + * @di('attr' => 'fontService', 'class' => 'FontService') */ class ClassAnnotated extends CallableClass { diff --git a/tests/TestAnnotation/AnnotationTest.php b/tests/TestAnnotation/AnnotationTest.php index cd3c82c..e3aa594 100644 --- a/tests/TestAnnotation/AnnotationTest.php +++ b/tests/TestAnnotation/AnnotationTest.php @@ -135,9 +135,10 @@ public function testContainerAnnotation() $this->assertCount(2, $aProperties); $this->assertArrayHasKey('di1', $aProperties); $this->assertArrayHasKey('di2', $aProperties); - $this->assertCount(1, $aProperties['di1']['__di']); + $this->assertCount(2, $aProperties['di1']['__di']); $this->assertCount(2, $aProperties['di2']['__di']); $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di1']['__di']['colorService']); + $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['di1']['__di']['fontService']); $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di2']['__di']['colorService']); $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['di2']['__di']['textService']); } @@ -148,6 +149,7 @@ public function testContainerAnnotation() public function testClassAnnotation() { [$bExcluded, $aProperties,] = $this->xAnnotationReader->getAttributes(ClassAnnotated::class, []); + // $this->assertEquals('', json_encode($aProperties)); $this->assertFalse($bExcluded); @@ -176,11 +178,13 @@ public function testClassAnnotation() $this->assertIsArray($aProperties['*']['__after']['funcAfter2']); $this->assertIsArray($aProperties['*']['__after']['funcAfter3']); - $this->assertCount(2, $aProperties['*']['__di']); + $this->assertCount(3, $aProperties['*']['__di']); $this->assertArrayHasKey('colorService', $aProperties['*']['__di']); $this->assertArrayHasKey('textService', $aProperties['*']['__di']); + $this->assertArrayHasKey('fontService', $aProperties['*']['__di']); $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['*']['__di']['colorService']); $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['*']['__di']['textService']); + $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['*']['__di']['fontService']); } /**