Skip to content

Commit

Permalink
Merge pull request #318 from namo-R/load_annotation_from_traits_in_en…
Browse files Browse the repository at this point in the history
…tity

Loading annotation from traits too
  • Loading branch information
hrach committed Oct 28, 2018
2 parents e422f5d + 8fbdb0a commit 933c82c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Entity/Reflection/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,25 @@ protected function loadProperties(& $fileDependencies)

foreach (array_reverse($classTree) as $class) {
if (!isset($this->classPropertiesCache[$class])) {
foreach (class_uses($class) as $traitName) {
$reflectionTrait = new ReflectionClass($traitName);
$fileDependencies[] = $reflectionTrait->getFileName();
$this->currentReflection = $reflectionTrait;
$this->classPropertiesCache[$traitName] = $this->parseAnnotations($reflectionTrait, $methods);
}

$reflection = new ReflectionClass($class);
$fileDependencies[] = $reflection->getFileName();
$this->currentReflection = $reflection;
$this->classPropertiesCache[$class] = $this->parseAnnotations($reflection, $methods);
}

foreach (class_uses($class) as $traitName) {
foreach ($this->classPropertiesCache[$traitName] as $name => $property) {
$this->metadata->setProperty($name, $property);
}
}

foreach ($this->classPropertiesCache[$class] as $name => $property) {
$this->metadata->setProperty($name, $property);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/inc/model/CreatedColumnTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace NextrasTests\Orm;

use DateTimeImmutable;

/**
* @property DateTimeImmutable $createdAt {default now}
* @property-read string $createdAtFormatted {virtual}
*/
trait CreatedColumnTrait {

/**
* Getter for column createdAtFormatted
* @return string
*/
protected function getterCreatedAtFormatted() : string {
return $this->createdAt->format('d.m.Y H:i:s');
}
}
3 changes: 1 addition & 2 deletions tests/inc/model/tagFollower/TagFollower.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace NextrasTests\Orm;

use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;


/**
* @property array $id {primary-proxy}
* @property Author $author {m:1 Author::$tagFollowers} {primary}
* @property Tag $tag {m:1 Tag::$tagFollowers} {primary}
* @property DateTimeImmutable $createdAt {default now}
*/
final class TagFollower extends Entity
{
use CreatedColumnTrait;
}

0 comments on commit 933c82c

Please sign in to comment.