Skip to content

Commit

Permalink
Improved class definition in the Container annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed Apr 27, 2022
1 parent ea31420 commit 1233f37
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
```
20 changes: 18 additions & 2 deletions src/Annotation/ContainerAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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']), '\\');
}

/**
Expand Down
8 changes: 5 additions & 3 deletions tests/App/Ajax/Annotated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down
6 changes: 4 additions & 2 deletions tests/App/Ajax/ClassAnnotated.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jaxon\Annotations\Tests\App\Ajax;

use Jaxon\Annotations\Tests\App\CallableClass;
use Jaxon\Annotations\Tests\Service\TextService;

/**
* @exclude(false)
Expand All @@ -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
{
Expand Down
8 changes: 6 additions & 2 deletions tests/TestAnnotation/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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']);
}

/**
Expand Down

0 comments on commit 1233f37

Please sign in to comment.