Skip to content
Permalink
Browse files

Traits support

  • Loading branch information
mabar authored and f3l1x committed Nov 18, 2019
1 parent ad71ea2 commit 3c1f6b26a54a135b62f653687b7d7345bc995545
@@ -57,7 +57,7 @@ services:

### Entity

Just add annotation `@<Before/Update>` to your entity.
Just add annotation `@<Before/Update>` to your entity or to trait which entity uses.

```php
/**
@@ -118,18 +118,22 @@ private function loadListeners(array $mapping): void
throw new ServiceCreationException(sprintf("Entity class '%s' not found", $entity));
}
// Parse annotations from phpDoc
$rf = new ClassType($entity);
// Add entity as dependency
$builder->addDependency($rf);
// Try all annotations
foreach (self::$annotations as $annotation => $events) {
/** @var IAnnotation|null $listener */
$listener = $rf->getAnnotation($annotation);
if ($listener !== null) {
$this->loadListenerByAnnotation($events, $repository, (string) $listener);
$types = class_uses($entity) + [$entity];
foreach ($types as $type) {
// Parse annotations from phpDoc
$rf = new ClassType($type);
// Add entity/trait as dependency
$builder->addDependency($rf);
// Try all annotations
foreach (self::$annotations as $annotation => $events) {
/** @var IAnnotation|null $listener */
$listener = $rf->getAnnotation($annotation);
if ($listener !== null) {
$this->loadListenerByAnnotation($events, $repository, (string) $listener);
}
}
}
}
@@ -16,6 +16,7 @@
use Tests\Fixtures\Mocks\Foo\Foo\FooRepository;
use Tests\Fixtures\Mocks\Foo\FooLifecycleListener;
use Tests\Fixtures\Mocks\Foo\FooListener;
use Tests\Fixtures\Mocks\Foo\FooTraitListener;
use Tests\Fixtures\Mocks\Foo\Model;
use Tests\Fixtures\Mocks\InvalidFoo\InvalidModel;
@@ -47,6 +48,7 @@ protected function createContainer(callable $callback): Container
- Tests\Fixtures\Mocks\Foo\FooListener
- Tests\Fixtures\Mocks\Foo\FooLifecycleListener
- Tests\Fixtures\Mocks\Foo\FooTraitListener
', 'neon'));
$callback($compiler);
@@ -68,6 +70,7 @@ public function testListenerLazyLoading(): void
Assert::falsey($container->isCreated($container->findByType(FooListener::class)[0]));
Assert::falsey($container->isCreated($container->findByType(FooLifecycleListener::class)[0]));
Assert::falsey($container->isCreated($container->findByType(FooTraitListener::class)[0]));
$entity = new Foo();
$entity->bar = 'foobar';
@@ -76,6 +79,7 @@ public function testListenerLazyLoading(): void
Assert::truthy($container->isCreated($container->findByType(FooListener::class)[0]));
Assert::truthy($container->isCreated($container->findByType(FooLifecycleListener::class)[0]));
Assert::truthy($container->isCreated($container->findByType(FooTraitListener::class)[0]));
}
public function testEventsHierarchy(): void
@@ -102,6 +106,10 @@ public function testEventsHierarchy(): void
/** @var FooListener $listener */
$listener = $container->getByType(FooListener::class);
Assert::equal(['onBeforePersist'], $listener->onCallHistory);
/** @var FooTraitListener $listener */
$listener = $container->getByType(FooTraitListener::class);
Assert::equal(['onBeforePersist'], $listener->onCallHistory);
}
public function testInvalidListener(): void
@@ -13,4 +13,6 @@
final class Foo extends Entity
{
use FooTrait;
}
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);
namespace Tests\Fixtures\Mocks\Foo\Foo;
/**
* @BeforePersist(Tests\Fixtures\Mocks\Foo\FooTraitListener)
*/
trait FooTrait
{
}
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);
namespace Tests\Fixtures\Mocks\Foo;
use Contributte\Nextras\Orm\Events\Listeners\BeforePersistListener;
use Nextras\Orm\Entity\IEntity;
final class FooTraitListener implements BeforePersistListener
{
/** @var callable[] */
public $onCall = [];
/** @var string[] */
public $onCallHistory = [];
public function onBeforePersist(IEntity $entity): void
{
$method = str_replace(self::class . '::', null, __METHOD__);
foreach ($this->onCall as $cb) {
$cb($method, $entity);
}
$this->onCallHistory[] = $method;
}
}

0 comments on commit 3c1f6b2

Please sign in to comment.
You can’t perform that action at this time.