Skip to content

Commit

Permalink
fix: make serialization of attributes possible
Browse files Browse the repository at this point in the history
Following up eaa1283, fixes an issue where cache mechanism could break
when relying on serialization.
  • Loading branch information
romm committed Jul 7, 2023
1 parent 5c89c66 commit e8ca2ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
18 changes: 10 additions & 8 deletions src/Definition/NativeAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ final class NativeAttributes implements Attributes
{
private AttributesContainer $delegate;

/** @var array<ReflectionAttribute<object>> */
private array $reflectionAttributes;
/** @var array<class-string, array<mixed>> */
private array $definition = [];

/**
* @param ReflectionClass<object>|ReflectionProperty|ReflectionMethod|ReflectionFunction|ReflectionParameter $reflection
*/
public function __construct(ReflectionClass|ReflectionProperty|ReflectionMethod|ReflectionFunction|ReflectionParameter $reflection)
{
$this->reflectionAttributes = $reflection->getAttributes();

$attributes = array_filter(
array_map(
static function (ReflectionAttribute $attribute) {
Expand All @@ -45,10 +43,14 @@ static function (ReflectionAttribute $attribute) {
return null;
}
},
$this->reflectionAttributes,
$reflection->getAttributes(),
),
);

foreach ($reflection->getAttributes() as $attribute) {
$this->definition[$attribute->getName()] = $attribute->getArguments();
}

$this->delegate = new AttributesContainer(...$attributes);
}

Expand All @@ -73,10 +75,10 @@ public function count(): int
}

/**
* @return array<ReflectionAttribute<object>>
* @return array<class-string, array<mixed>>
*/
public function reflectionAttributes(): array
public function definition(): array
{
return $this->reflectionAttributes;
return $this->definition;
}
}
36 changes: 14 additions & 22 deletions src/Definition/Repository/Cache/Compiler/AttributesCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use CuyZ\Valinor\Definition\Attributes;
use CuyZ\Valinor\Definition\AttributesContainer;
use CuyZ\Valinor\Definition\NativeAttributes;
use ReflectionAttribute;

use function array_map;
use function count;
use function implode;
use function var_export;
Expand All @@ -32,31 +30,25 @@ public function compile(Attributes $attributes): string
PHP;
}

/**
* @param ReflectionAttribute<object> $reflectionAttribute
*/
private function compileNativeAttribute(ReflectionAttribute $reflectionAttribute): string
private function compileNativeAttributes(NativeAttributes $attributes): string
{
$name = $reflectionAttribute->getName();
$arguments = $reflectionAttribute->getArguments();

/** @infection-ignore-all */
if (count($arguments) > 0) {
$arguments = serialize($arguments);
$arguments = 'unserialize(' . var_export($arguments, true) . ')';
$attributes = $attributes->definition();

return "new $name(...$arguments)";
if (count($attributes) === 0) {
return '[]';
}

return "new $name()";
}
$attributesListCode = [];

private function compileNativeAttributes(NativeAttributes $attributes): string
{
$attributesListCode = array_map(
fn (ReflectionAttribute $attribute) => $this->compileNativeAttribute($attribute),
$attributes->reflectionAttributes()
);
foreach ($attributes as $className => $arguments) {
if (count($arguments) === 0) {
$argumentsCode = '';
} else {
$argumentsCode = '...unserialize(' . var_export(serialize($arguments), true) . ')';
}

$attributesListCode[] = "new $className($argumentsCode)";
}

return '...[' . implode(",\n", $attributesListCode) . ']';
}
Expand Down

0 comments on commit e8ca2ff

Please sign in to comment.