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
30 changes: 30 additions & 0 deletions packages/Ecotone/src/AnnotationFinder/AnnotatedDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@

/**
* licence Apache-2.0
*
* @template TClassAttribute of object
* @template TMethodAttribute of object
* @implements AnnotatedFinding<TMethodAttribute>
*/
class AnnotatedDefinition implements AnnotatedFinding
{
private string $className;
private string $methodName;
/**
* @var TClassAttribute
*/
private object $annotationForClass;

/**
* @var TMethodAttribute
*/
private object $annotationForMethod;
/**
* @var object[]
Expand All @@ -25,6 +36,13 @@ class AnnotatedDefinition implements AnnotatedFinding
*/
private array $classAnnotations;

/**
* @param TClassAttribute $annotationForClass
* @param TMethodAttribute $annotationForMethod
* @param class-string $className
* @param object[] $classAnnotations
* @param object[] $methodAnnotations
*/
private function __construct(object $annotationForClass, object $annotationForMethod, string $className, string $methodName, array $classAnnotations, array $methodAnnotations)
{
$this->annotationForClass = $annotationForClass;
Expand All @@ -36,6 +54,9 @@ private function __construct(object $annotationForClass, object $annotationForMe
}

/**
* @param TClassAttribute $annotationForClass
* @param TMethodAttribute $annotationForMethod
* @param class-string $className
* @param object[] $classAnnotations
* @param object[] $methodAnnotations
*/
Expand All @@ -44,16 +65,25 @@ public static function create(object $annotationForClass, object $annotationForM
return new self($annotationForClass, $annotationForMethod, $className, $methodName, $classAnnotations, $methodAnnotations);
}

/**
* @return TClassAttribute
*/
public function getAnnotationForClass(): object
{
return $this->annotationForClass;
}

/**
* @return TMethodAttribute
*/
public function getAnnotationForMethod(): object
{
return $this->annotationForMethod;
}

/**
* @return class-string
*/
public function getClassName(): string
{
return $this->className;
Expand Down
8 changes: 8 additions & 0 deletions packages/Ecotone/src/AnnotationFinder/AnnotatedFinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@

/**
* licence Apache-2.0
*
* @template TMethodAttribute of object
*/
interface AnnotatedFinding
{
/**
* @return TMethodAttribute
*/
public function getAnnotationForMethod(): object;

/**
* @return class-string
*/
public function getClassName(): string;

public function getMethodName(): string;
Expand Down
10 changes: 7 additions & 3 deletions packages/Ecotone/src/AnnotationFinder/AnnotationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
interface AnnotationFinder extends AnnotationResolver
{
/**
* @return AnnotatedDefinition[]
* @template TClassAttribute of object
* @template TMethodAttribute of object
* @param class-string<TClassAttribute> $classAnnotationName
* @param class-string<TMethodAttribute> $methodAnnotationClassName
* @return list<AnnotatedDefinition<TClassAttribute,TMethodAttribute>>
*/
public function findCombined(string $classAnnotationName, string $methodAnnotationClassName): array;

/**
* @return string[]
* @return class-string[]
*/
public function findAnnotatedClasses(string $annotationClassName): array;

Expand All @@ -25,7 +29,7 @@ public function findAnnotatedClasses(string $annotationClassName): array;
public function findAnnotatedMethods(string $methodAnnotationClassName): array;

/**
* @template T
* @template T of object
* @param class-string<T> $attributeClassName
* @return T
* @throws InvalidArgumentException
Expand Down
7 changes: 5 additions & 2 deletions packages/Ecotone/src/AnnotationFinder/AnnotationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ interface AnnotationResolver
public function getAnnotationsForMethod(string $className, string $methodName): array;

/**
* @return object[]
* @template T of object
* @param class-string $className
* @param class-string<T>|null $attributeClassName
* @return ($attributeClassName is null ? list<object> : list<T>)
*/
public function getAnnotationsForClass(string $className): array;
public function getAnnotationsForClass(string $className, ?string $attributeClassName = null): array;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this parameter to allow finding repeated attributes on the same class.


/**
* @return object[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getAnnotationsForMethod(string $className, string $methodName):
/**
* @inheritDoc
*/
public function getAnnotationsForClass(string $className): array
public function getAnnotationsForClass(string $className, ?string $attributeClassName = null): array
{
$attributes = [];
$currentClass = new ReflectionClass($className);
Expand All @@ -58,6 +58,9 @@ public function getAnnotationsForClass(string $className): array
if (! class_exists($attribute->getName())) {
continue;
}
if ($attributeClassName && ! ($attribute instanceof $attributeClassName)) {
continue;
}
if (in_array($attribute->getName(), array_map(fn ($attr) => $attr::class, $attributes))) {
continue; // Avoid duplicate attributes from parent classes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,25 @@ public function findAnnotatedClasses(string $annotationClassName): array

$classesWithAnnotations = [];
foreach ($this->registeredClasses as $class) {
$classAnnotation = $this->getAnnotationForClass($class, $annotationClassName);

if ($classAnnotation) {
if ($this->hasAnnotation($class, $annotationClassName)) {
$classesWithAnnotations[] = $class;
}
}

return $classesWithAnnotations;
}

private function hasAnnotation(string $className, string $annotationClassNameToFind): bool
{
$annotationsForClass = $this->getAnnotationsForClass($className);
foreach ($annotationsForClass as $annotationForClass) {
if (is_a($annotationForClass, $annotationClassNameToFind)) {
return true;
}
}
return false;
}

private function getAnnotationForClass(string $className, string $annotationClassNameToFind): ?object
{
$annotationsForClass = $this->getAnnotationsForClass($className);
Expand All @@ -236,11 +245,18 @@ private function getAnnotationForClass(string $className, string $annotationClas
}

/**
* @param string $className
* @param string|null $attributeClassName
* @inheritDoc
*/
public function getAnnotationsForClass(string $className): array
public function getAnnotationsForClass(string $className, ?string $attributeClassName = null): array
{
return $this->getCachedAnnotationsForClass($className);
$attributes = $this->getCachedAnnotationsForClass($className);
if ($attributeClassName) {
return array_values(array_filter($attributes, fn (object $attribute) => $attribute instanceof $attributeClassName));
} else {
return $attributes;
}
}

/**
Expand Down Expand Up @@ -345,7 +361,7 @@ public function findAnnotatedMethods(string $methodAnnotationClassName): array
return $registrations;
}

private function isMethodBannedFromCurrentEnvironment(string $className, string $methodName)
private function isMethodBannedFromCurrentEnvironment(string $className, string $methodName): bool
{
return isset($this->bannedEnvironmentClassMethods[$className][$methodName]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,17 @@ public function getAnnotationsForMethod(string $className, string $methodName):
/**
* @inheritDoc
*/
public function getAnnotationsForClass(string $classNameToFind): array
public function getAnnotationsForClass(string $className, ?string $attributeClassName = null): array
{
if (! isset($this->annotationsForClass[self::CLASS_ANNOTATIONS][$classNameToFind])) {
if (! isset($this->annotationsForClass[self::CLASS_ANNOTATIONS][$className])) {
return [];
}
$attributes = $this->annotationsForClass[self::CLASS_ANNOTATIONS][$className];
if ($attributeClassName) {
$attributes = array_filter($attributes, fn ($attribute) => $attribute instanceof $attributeClassName);
}

return array_values($this->annotationsForClass[self::CLASS_ANNOTATIONS][$classNameToFind]);
return array_values($attributes);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/Ecotone/src/Modelling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class Event
{
private function __construct(
protected function __construct(
private string $eventName,
private array|object $payload,
private array $metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Ecotone\EventSourcing\Attribute;

use Attribute;
use Ecotone\AnnotationFinder\AnnotationFinder;
use Ecotone\EventSourcing\EventStore;
use Ecotone\Messaging\Config\ConfigurationException;
use Ecotone\Modelling\Attribute\EventSourcingAggregate;

/*
* Configures a projection to read from an aggregate's event stream.
Expand All @@ -23,7 +26,7 @@
*
* licence Enterprise
*/
#[Attribute(Attribute::TARGET_CLASS)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class FromAggregateStream
{
/**
Expand Down
10 changes: 5 additions & 5 deletions packages/PdoEventSourcing/src/Attribute/FromStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use Attribute;
use Ecotone\EventSourcing\EventStore;

#[Attribute(Attribute::TARGET_CLASS)]
class FromStream
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
readonly class FromStream
{
public function __construct(
public readonly string $stream,
public readonly ?string $aggregateType = null,
public readonly string $eventStoreReferenceName = EventStore::class
public string $stream,
public ?string $aggregateType = null,
public string $eventStoreReferenceName = EventStore::class
) {
}
}
Loading