Skip to content

Commit

Permalink
Merge pull request #326 from ixarlie/issue-325-php8-attributes
Browse files Browse the repository at this point in the history
Issue 325 php8 attributes
  • Loading branch information
goetas authored Oct 14, 2024
2 parents 5d7953f + c247512 commit 95427a4
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 121 deletions.
13 changes: 13 additions & 0 deletions src/Configuration/Annotation/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Hateoas\Configuration\Annotation;

use JMS\Serializer\Annotation\AnnotationUtilsTrait;

/**
* @Annotation
* @Target("ANNOTATION")
*/
#[\Attribute]
class Embedded
{
use AnnotationUtilsTrait;

/**
* @Required
* @var mixed
Expand All @@ -32,4 +37,12 @@ class Embedded
* phpcs:enable
*/
public $exclusion = null;

/**
* @param string|array $content
*/
public function __construct(array $values = [], $content = null, ?string $type = null, ?string $xmlElementName = null, ?Exclusion $exclusion = null)
{
$this->loadAnnotationParameters(get_defined_vars());
}
}
10 changes: 10 additions & 0 deletions src/Configuration/Annotation/Exclusion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Hateoas\Configuration\Annotation;

use JMS\Serializer\Annotation\AnnotationUtilsTrait;

/**
* @Annotation
* @Target("ANNOTATION")
*/
#[\Attribute]
final class Exclusion
{
use AnnotationUtilsTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -38,4 +43,9 @@ final class Exclusion
* @var string
*/
public $excludeIf = null;

public function __construct(array $values = [], ?array $groups = null, ?string $sinceVersion = null, ?string $untilVersion = null, ?int $maxDepth = null, ?string $excludeIf = null)
{
$this->loadAnnotationParameters(get_defined_vars());
}
}
14 changes: 14 additions & 0 deletions src/Configuration/Annotation/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Hateoas\Configuration\Annotation;

use JMS\Serializer\Annotation\AnnotationUtilsTrait;

/**
* @Annotation
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class Relation
{
use AnnotationUtilsTrait;

/**
* @Required
* @var string
Expand Down Expand Up @@ -37,4 +42,13 @@ final class Relation
* phpcs:enable
*/
public $exclusion = null;

/**
* @param string|Route $href
* @param string|Embedded $embedded
*/
public function __construct(array $values = [], ?string $name = null, $href = null, $embedded = null, array $attributes = [], ?Exclusion $exclusion = null)
{
$this->loadAnnotationParameters(get_defined_vars());
}
}
10 changes: 10 additions & 0 deletions src/Configuration/Annotation/RelationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

namespace Hateoas\Configuration\Annotation;

use JMS\Serializer\Annotation\AnnotationUtilsTrait;

/**
* @Annotation
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class RelationProvider
{
use AnnotationUtilsTrait;

/**
* @var string
*/
public $name;

public function __construct(array $values = [], ?string $name = null)
{
$this->loadAnnotationParameters(get_defined_vars());
}
}
14 changes: 14 additions & 0 deletions src/Configuration/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace Hateoas\Configuration\Annotation;

use JMS\Serializer\Annotation\AnnotationUtilsTrait;

/**
* @Annotation
* @Target("ANNOTATION")
*/
#[\Attribute]
class Route
{
use AnnotationUtilsTrait;

/**
* @Required
* @var string
Expand All @@ -31,4 +36,13 @@ class Route
* @var string
*/
public $generator = null;

/**
* @param array|string $parameters
* @param bool|string $absolute
*/
public function __construct(array $values = [], ?string $name = null, $parameters = null, $absolute = false, ?string $generator = null)
{
$this->loadAnnotationParameters(get_defined_vars());
}
}
126 changes: 6 additions & 120 deletions src/Configuration/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,147 +5,33 @@
namespace Hateoas\Configuration\Metadata\Driver;

use Doctrine\Common\Annotations\Reader as AnnotationsReader;
use Hateoas\Configuration\Annotation;
use Hateoas\Configuration\Embedded;
use Hateoas\Configuration\Exclusion;
use Hateoas\Configuration\Metadata\ClassMetadata;
use Hateoas\Configuration\Provider\RelationProviderInterface;
use Hateoas\Configuration\Relation;
use Hateoas\Configuration\RelationProvider;
use Hateoas\Configuration\Route;
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
use JMS\Serializer\Expression\Expression;
use JMS\Serializer\Type\ParserInterface;
use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\DriverInterface;

class AnnotationDriver implements DriverInterface
class AnnotationDriver extends AnnotationOrAttributeDriver
{
use CheckExpressionTrait;

/**
* @var AnnotationsReader
*/
private $reader;

/**
* @var RelationProviderInterface
*/
private $relationProvider;

/**
* @var ParserInterface
*/
private $typeParser;

public function __construct(
AnnotationsReader $reader,
CompilableExpressionEvaluatorInterface $expressionLanguage,
RelationProviderInterface $relationProvider,
ParserInterface $typeParser
) {
$this->reader = $reader;
$this->relationProvider = $relationProvider;
$this->expressionLanguage = $expressionLanguage;
$this->typeParser = $typeParser;
}

public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata
{
$annotations = $this->reader->getClassAnnotations($class);

if (0 === count($annotations)) {
return null;
}

$classMetadata = new ClassMetadata($class->getName());
$classMetadata->fileResources[] = $class->getFilename();

foreach ($annotations as $annotation) {
if ($annotation instanceof Annotation\Relation) {
$classMetadata->addRelation(new Relation(
$annotation->name,
$this->createHref($annotation->href),
$this->createEmbedded($annotation->embedded),
$this->checkExpressionArray($annotation->attributes) ?: [],
$this->createExclusion($annotation->exclusion)
));
} elseif ($annotation instanceof Annotation\RelationProvider) {
$relations = $this->relationProvider->getRelations(new RelationProvider($annotation->name), $class->getName());
foreach ($relations as $relation) {
$classMetadata->addRelation($relation);
}
}
}

if (0 === count($classMetadata->getRelations())) {
return null;
}

return $classMetadata;
}

private function parseExclusion(Annotation\Exclusion $exclusion): Exclusion
{
return new Exclusion(
$exclusion->groups,
null !== $exclusion->sinceVersion ? (string) $exclusion->sinceVersion : null,
null !== $exclusion->untilVersion ? (string) $exclusion->untilVersion : null,
null !== $exclusion->maxDepth ? (int) $exclusion->maxDepth : null,
$this->checkExpression($exclusion->excludeIf)
);
}

/**
* @param mixed $href
*
* @return Expression|mixed
*/
private function createHref($href)
{
if ($href instanceof Annotation\Route) {
return new Route(
$this->checkExpression($href->name),
is_array($href->parameters) ? $this->checkExpressionArray($href->parameters) : $this->checkExpression($href->parameters),
$this->checkExpression($href->absolute),
$href->generator
);
}
parent::__construct($expressionLanguage, $relationProvider, $typeParser);

return $this->checkExpression($href);
$this->reader = $reader;
}

/**
* @param Annotation\Embedded|mixed $embedded
*
* @return Expression|mixed
* {@inheritdoc}
*/
private function createEmbedded($embedded)
{
if ($embedded instanceof Annotation\Embedded) {
$embeddedExclusion = $embedded->exclusion;

if (null !== $embeddedExclusion) {
$embeddedExclusion = $this->parseExclusion($embeddedExclusion);
}

return new Embedded(
$this->checkExpression($embedded->content),
$this->checkExpression($embedded->xmlElementName),
$embeddedExclusion,
null !== $embedded->type ? $this->typeParser->parse($embedded->type) : null
);
}

return $this->checkExpression($embedded);
}

private function createExclusion(?Annotation\Exclusion $exclusion = null): ?Exclusion
protected function getClassAnnotations(\ReflectionClass $class): array
{
if (null !== $exclusion) {
$exclusion = $this->parseExclusion($exclusion);
}

return $exclusion;
return $this->reader->getClassAnnotations($class);
}
}
Loading

0 comments on commit 95427a4

Please sign in to comment.