Skip to content

Commit 9ef49de

Browse files
authored
Merge pull request #235 from koriym/named_arg
Compatibility of Annotation classes with the PHP 8 attribute feature
2 parents 00a8a53 + 1d9ff1d commit 9ef49de

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ protected function configure()
709709

710710
Ray.Di can be used either with [doctrine/annotation](https://github.com/doctrine/annotations) in PHP 7/8 or with an [Attributes](https://www.php.net/manual/en/language.attributes.overview.php) in PHP8.
711711
See the annotation code examples in the older [README(v2.10)](https://github.com/ray-di/Ray.Di/tree/2.10.5/README.md).
712+
To make forward-compatible annotations for attributes, see [Custom Annotation Classes](https://github.com/kerveros12v/sacinta4/blob/e976c143b3b7d42497334e76c00fdf38717af98e/vendor/doctrine/annotations/docs/en/custom.rst#optional-constructors-with-named-parameters).
712713

713714
## Installation ##
714715

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^7.2 || ^8.0",
14-
"doctrine/annotations": "^1.10",
14+
"doctrine/annotations": "^1.11",
1515
"doctrine/cache": "^1.10",
1616
"ray/aop": "^2.10",
1717
"koriym/printo": "^1.0",

src/di/Di/Assisted.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Ray\Di\Di;
66

77
use Attribute;
8+
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
89
use Ray\Aop\Annotation\AbstractAssisted;
910

1011
/**
@@ -13,6 +14,17 @@
1314
* @Annotation
1415
* @Target("METHOD")
1516
*/
16-
final class Assisted extends AbstractAssisted
17+
#[Attribute(Attribute::TARGET_METHOD)]
18+
final class Assisted extends AbstractAssisted implements NamedArgumentConstructorAnnotation
1719
{
20+
/** @var array<string> */
21+
public $values;
22+
23+
/**
24+
* @param array<string> $value
25+
*/
26+
public function __construct($value)
27+
{
28+
$this->values = $value;
29+
}
1830
}

src/di/Di/Inject.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Ray\Di\Di;
66

77
use Attribute;
8+
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
89

910
/**
1011
* Annotates your class methods into which the Injector should inject values
@@ -13,7 +14,7 @@
1314
* @Target("METHOD")
1415
*/
1516
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PARAMETER)]
16-
final class Inject implements InjectInterface
17+
final class Inject implements InjectInterface, NamedArgumentConstructorAnnotation
1718
{
1819
/**
1920
* If true, and the appropriate binding is not found, the Injector will skip injection of this method or field rather than produce an error.
@@ -23,13 +24,11 @@ final class Inject implements InjectInterface
2324
public $optional = false;
2425

2526
/**
26-
* @param array{optional?: bool} $values
27-
*
2827
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
2928
*/
30-
public function __construct(array $values = [], bool $optional = false)
29+
public function __construct(bool $optional = false)
3130
{
32-
$this->optional = $values['optional'] ?? $optional;
31+
$this->optional = $optional;
3332
}
3433

3534
/**

src/di/Di/Named.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Ray\Di\Di;
66

77
use Attribute;
8+
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
89

910
use function is_array;
1011
use function is_string;
@@ -15,25 +16,14 @@
1516
* @Annotation
1617
* @Target("METHOD")
1718
*/
18-
#[Attribute(Attribute::TARGET_PARAMETER)]
19-
final class Named
19+
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_METHOD)]
20+
final class Named implements NamedArgumentConstructorAnnotation
2021
{
2122
/** @var string */
2223
public $value = '';
2324

24-
/**
25-
* @param array{value: string}|string $value
26-
*/
27-
public function __construct($value)
25+
public function __construct(string $value)
2826
{
29-
if (is_array($value) && isset($value['value'])) {
30-
// doctrine/annotations
31-
$this->value = $value['value'];
32-
}
33-
34-
if (is_string($value)) {
35-
// php8 attribute
36-
$this->value = $value;
37-
}
27+
$this->value = $value;
3828
}
3929
}

tests/di/Fake/FakeAssistedParamsConsumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FakeAssistedParamsConsumer
1111
/**
1212
* @return array [int, FakeAbstractDb]
1313
*
14-
* @Assisted("db")
14+
* @Assisted({"db"})
1515
*/
1616
public function getUser($id, ?FakeAbstractDb $db = null)
1717
{

0 commit comments

Comments
 (0)