Skip to content
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
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# CHANGELOG

## v2.2.0

### New Requirements

None

### New features

None

### Deprecated Features

None

### Backward Incompatible Changes

None

### Other Changes

- [#36](https://github.com/olvlvl/composer-attribute-collector/pull/36) Attribute arguments are now serialized to support [nested attributes introduced in PHP 8.1](https://wiki.php.net/rfc/new_in_initializers).



## v2.1.0

### New Requirements
Expand Down
42 changes: 21 additions & 21 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
final class Collection
{
/**
* @param array<class-string, array<array{ mixed[], class-string }>> $targetClasses
* @param array<class-string, array<array{ string, class-string }>> $targetClasses
* Where _key_ is an attribute class and _value_ an array of arrays
* where 0 are the attribute arguments and 1 is a target class.
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string }>> $targetMethods
* where `0` is the serialized attribute arguments and `1` is a target class.
* @param array<class-string, array<array{ string, class-string, non-empty-string }>> $targetMethods
* Where _key_ is an attribute class and _value_ an array of arrays
* where 0 are the attribute arguments, 1 is a target class, and 2 is the target method.
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string }>> $targetProperties
* where `0` is the serialized attribute arguments, `1` is a target class, and `2` is the target method.
* @param array<class-string, array<array{ string, class-string, non-empty-string }>> $targetProperties
* Where _key_ is an attribute class and _value_ an array of arrays
* where 0 are the attribute arguments, 1 is a target class, and 2 is the target property.
* @param array<class-string, array<array{ mixed[], class-string, non-empty-string, non-empty-string }>> $targetParameters
* Where _key_ is an attribute class and _value_ an array of arrays where 0 are the
* attribute arguments, 1 is a target class, 2 is the target method, and 3 is the target parameter.
* where `0` is the serialized attribute arguments, `1` is a target class, and `2` is the target property.
* @param array<class-string, array<array{ string, class-string, non-empty-string, non-empty-string }>> $targetParameters
* Where _key_ is an attribute class and _value_ an array of arrays
* where `0` are the serialized attribute arguments, `1` is a target class, `2` is the target method, and `3` is the target parameter.
*/
public function __construct(
private array $targetClasses,
Expand Down Expand Up @@ -53,15 +53,15 @@ public function findTargetClasses(string $attribute): array
* @template T of object
*
* @param class-string<T> $attribute
* @param array<mixed> $arguments
* @param string $arguments The serialized arguments
* @param class-string $class
*
* @return TargetClass<T>
*/
private static function createClassAttribute(string $attribute, array $arguments, string $class): object
private static function createClassAttribute(string $attribute, string $arguments, string $class): object
{
try {
$a = new $attribute(...$arguments);
$a = new $attribute(...unserialize($arguments));
return new TargetClass($a, $class);
} catch (Throwable $e) {
throw new RuntimeException(
Expand Down Expand Up @@ -90,20 +90,20 @@ public function findTargetMethods(string $attribute): array
* @template T of object
*
* @param class-string<T> $attribute
* @param array<mixed> $arguments
* @param string $arguments The serialized arguments
* @param class-string $class
* @param non-empty-string $method
*
* @return TargetMethod<T>
*/
private static function createMethodAttribute(
string $attribute,
array $arguments,
string $arguments,
string $class,
string $method,
): object {
try {
$a = new $attribute(...$arguments);
$a = new $attribute(...unserialize($arguments));
return new TargetMethod($a, $class, $method);
} catch (Throwable $e) {
throw new RuntimeException(
Expand Down Expand Up @@ -132,7 +132,7 @@ public function findTargetParameters(string $attribute): array
* @template T of object
*
* @param class-string<T> $attribute
* @param array<mixed> $arguments
* @param string $arguments The serialized arguments
* @param class-string $class
* @param non-empty-string $method
* @param non-empty-string $parameter
Expand All @@ -141,13 +141,13 @@ public function findTargetParameters(string $attribute): array
*/
private static function createParameterAttribute(
string $attribute,
array $arguments,
string $arguments,
string $class,
string $method,
string $parameter,
): object {
try {
$a = new $attribute(...$arguments);
$a = new $attribute(...unserialize($arguments));
return new TargetParameter($a, $class, $method, $parameter);
} catch (Throwable $e) {
throw new RuntimeException(
Expand Down Expand Up @@ -176,20 +176,20 @@ public function findTargetProperties(string $attribute): array
* @template T of object
*
* @param class-string<T> $attribute
* @param array<mixed> $arguments
* @param string $arguments The serialized arguments
* @param class-string $class
* @param non-empty-string $property
*
* @return TargetProperty<T>
*/
private static function createPropertyAttribute(
string $attribute,
array $arguments,
string $arguments,
string $class,
string $property,
): object {
try {
$a = new $attribute(...$arguments);
$a = new $attribute(...unserialize($arguments));
return new TargetProperty($a, $class, $property);
} catch (Throwable $e) {
throw new RuntimeException(
Expand Down
7 changes: 4 additions & 3 deletions src/TransientCollectionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ private static function targetsToCode(iterable $targetByClass): string
* @param iterable<class-string, iterable<TransientTargetClass|TransientTargetMethod|TransientTargetParameter|TransientTargetProperty>> $targetByClass
*
* @return array<class-string, array<array{
* array<int|string, mixed>,
* string,
* class-string,
* 2?:non-empty-string,
* 3?:non-empty-string
* }>>
* }>> Where _key_ is an attribute class and _value_ is an array of parameters,
* where `1` is the serialized arguments and `2` is the target class.
*/
private static function targetsToArray(iterable $targetByClass): array
{
$by = [];

foreach ($targetByClass as $class => $targets) {
foreach ($targets as $t) {
$a = [ $t->arguments, $class ];
$a = [ serialize($t->arguments), $class ];

if ($t instanceof TransientTargetParameter) {
$a[] = $t->method;
Expand Down
14 changes: 14 additions & 0 deletions tests/Acme81/Attribute/SampleNested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Acme81\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class SampleNested
{
public function __construct(
public SampleNestedValue $value,
) {
}
}
11 changes: 11 additions & 0 deletions tests/Acme81/Attribute/SampleNestedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Acme81\Attribute;

class SampleNestedValue
{
public function __construct(
public int $value
) {
}
}
3 changes: 3 additions & 0 deletions tests/Acme81/PSR4/Presentation/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Acme81\Attribute\Method;
use Acme81\Attribute\Post;
use Acme81\Attribute\Route;
use Acme81\Attribute\SampleNested;
use Acme81\Attribute\SampleNestedValue;

#[Route('/articles')]
#[SampleNested(new SampleNestedValue(1))]
class ArticleController
{
#[Route('/:id', method: Method::GET)]
Expand Down
56 changes: 28 additions & 28 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public function testInstantiationErrorIsDecorated(string $expectedMessage, Closu
$collection = new Collection(
targetClasses: [
Permission::class => [
[ [ 'Permission' => 'is_admin' ], DeleteMenu::class ],
[ serialize([ 'Permission' => 'is_admin' ]), DeleteMenu::class ],
]
],
targetMethods: [
Route::class => [
[ [ 'Method' => 'GET' ], ArticleController::class, 'list' ],
[ serialize([ 'Method' => 'GET' ]), ArticleController::class, 'list' ],
]
],
targetProperties: [
Serial::class => [
[ [ 'Primary' => true ], Article::class, 'id' ],
[ serialize([ 'Primary' => true ]), Article::class, 'id' ],
]
],
targetParameters: [
Expand Down Expand Up @@ -101,9 +101,9 @@ public function testFilterTargetClasses(): void
$collection = new Collection(
targetClasses: [
Route::class => [
[ [ 'pattern' => '/articles' ], ArticleController::class ],
[ [ 'pattern' => '/images' ], ImageController::class ],
[ [ 'pattern' => '/files' ], FileController::class ],
[ serialize([ 'pattern' => '/articles' ]), ArticleController::class ],
[ serialize([ 'pattern' => '/images' ]), ImageController::class ],
[ serialize([ 'pattern' => '/files' ]), FileController::class ],
],
],
targetMethods: [
Expand Down Expand Up @@ -131,13 +131,13 @@ public function testFilterTargetMethods(): void
],
targetMethods: [
Route::class => [
[ [ 'pattern' => '/recent' ], ArticleController::class, 'recent' ],
[ serialize([ 'pattern' => '/recent' ]), ArticleController::class, 'recent' ],
],
Get::class => [
[ [ ], ArticleController::class, 'show' ],
[ serialize([ ]), ArticleController::class, 'show' ],
],
Post::class => [
[ [ ], ArticleController::class, 'create' ],
[ serialize([ ]), ArticleController::class, 'create' ],
],
],
targetProperties: [
Expand Down Expand Up @@ -166,12 +166,12 @@ public function testFilterTargetParameters(): void
],
targetParameters: [
ParameterA::class => [
[ [ 'a' ], ArticleController::class, 'myMethod', 'myParamA', ],
[ [ 'a2' ], ArticleController::class, 'myMethod', 'myParamA2' ],
[ [ 'a3' ], ArticleController::class, 'myFoo', 'fooParam' ],
[ serialize([ 'a' ]), ArticleController::class, 'myMethod', 'myParamA', ],
[ serialize([ 'a2' ]), ArticleController::class, 'myMethod', 'myParamA2' ],
[ serialize([ 'a3' ]), ArticleController::class, 'myFoo', 'fooParam' ],
],
ParameterB::class => [
[ [ 'b', 'more data'], ArticleController::class, 'myMethod', 'myParamB' ],
[ serialize([ 'b', 'more data']), ArticleController::class, 'myMethod', 'myParamB' ],
],
]
);
Expand All @@ -192,27 +192,27 @@ public function testFilterTargetProperties(): void
],
targetMethods: [
Route::class => [
[ [ 'pattern' => '/recent' ], ArticleController::class, 'recent' ],
[ serialize([ 'pattern' => '/recent' ]), ArticleController::class, 'recent' ],
],
Get::class => [
[ [ ], ArticleController::class, 'show' ],
[ serialize([ ]), ArticleController::class, 'show' ],
],
Post::class => [
[ [ ], ArticleController::class, 'create' ],
[ serialize([ ]), ArticleController::class, 'create' ],
],
],
targetProperties: [
Id::class => [
[ [ ], Article::class, 'id' ],
[ serialize([ ]), Article::class, 'id' ],
],
Serial::class => [
[ [ ], Article::class, 'id' ],
[ serialize([ ]), Article::class, 'id' ],
],
Varchar::class => [
[ [ 'size' => 80 ], Article::class, 'title' ],
[ serialize([ 'size' => 80 ]), Article::class, 'title' ],
],
Text::class => [
[ [ ], Article::class, 'body' ],
[ serialize([ ]), Article::class, 'body' ],
]
],
targetParameters: [
Expand All @@ -236,30 +236,30 @@ public function testForClass(): void
$collection = new Collection(
targetClasses: [
Index::class => [
[ [ 'slug', 'unique' => true ], Article::class ],
[ serialize([ 'slug', 'unique' => true ]), Article::class ],
],
Route::class => [ // trap
[ [ 'pattern' => '/articles' ], ArticleController::class ],
[ serialize([ 'pattern' => '/articles' ]), ArticleController::class ],
],
],
targetMethods: [
Route::class => [ // trap
[ [ 'pattern' => '/recent' ], ArticleController::class, 'recent' ],
[ serialize([ 'pattern' => '/recent' ]), ArticleController::class, 'recent' ],
],
],
targetProperties: [
Id::class => [
[ [ ], Article::class, 'id' ],
[ serialize([ ]), Article::class, 'id' ],
],
Serial::class => [
[ [ ], Article::class, 'id' ],
[ serialize([ ]), Article::class, 'id' ],
],
Varchar::class => [
[ [ 'size' => 80 ], Article::class, 'title' ],
[ [ 'size' => 80 ], Article::class, 'slug' ],
[ serialize([ 'size' => 80 ]), Article::class, 'title' ],
[ serialize([ 'size' => 80 ]), Article::class, 'slug' ],
],
Text::class => [
[ [ ], Article::class, 'body' ],
[ serialize([ ]), Article::class, 'body' ],
]
],
targetParameters: [
Expand Down
18 changes: 18 additions & 0 deletions tests/CollectorTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,24 @@ public function testFilterTargetMethods(): void
], $this->collectMethods($actual));
}

/**
* @requires PHP >= 8.1
*/
public function testNestedAttributes81(): void
{
$expected = [
new TargetClass(
new \Acme81\Attribute\SampleNested(new \Acme81\Attribute\SampleNestedValue(1)),
\Acme81\PSR4\Presentation\ArticleController::class,
)
];
$actual = Attributes::filterTargetClasses(
Attributes::predicateForAttributeInstanceOf(\Acme81\Attribute\SampleNested::class)
);

$this->assertEquals($expected, $actual);
}

/**
* @requires PHP >= 8.1
*/
Expand Down