-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from spiral/annotated-4-0
Adding cycle/annotated 4.0 support
- Loading branch information
Showing
9 changed files
with
281 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cycle\Annotated\Locator; | ||
|
||
use Cycle\Annotated\Annotation\Embeddable; | ||
use Cycle\Annotated\Exception\AnnotationException; | ||
use Cycle\Annotated\Locator\Embedding; | ||
use Cycle\Annotated\Locator\EmbeddingLocatorInterface; | ||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\Tokenizer\Attribute\TargetAttribute; | ||
use Spiral\Tokenizer\TokenizationListenerInterface; | ||
|
||
#[TargetAttribute(Embeddable::class, useAnnotations: true)] | ||
final class ListenerEmbeddingsLocator implements EmbeddingLocatorInterface, TokenizationListenerInterface | ||
{ | ||
/** | ||
* @var Embedding[] | ||
*/ | ||
private array $embeddings = []; | ||
private bool $collected = false; | ||
|
||
public function __construct( | ||
private readonly ReaderInterface $reader | ||
) { | ||
} | ||
|
||
public function listen(\ReflectionClass $class): void | ||
{ | ||
try { | ||
$attribute = $this->reader->firstClassMetadata($class, Embeddable::class); | ||
} catch (\Exception $e) { | ||
throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e); | ||
} | ||
|
||
if ($attribute !== null) { | ||
$this->embeddings[] = new Embedding($attribute, $class); | ||
} | ||
} | ||
|
||
public function finalize(): void | ||
{ | ||
$this->collected = true; | ||
} | ||
|
||
public function getEmbeddings(): array | ||
{ | ||
if (!$this->collected) { | ||
throw new AnnotationException(\sprintf('Tokenizer did not finalize %s listener.', self::class)); | ||
} | ||
|
||
return $this->embeddings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Cycle\Annotated\Locator; | ||
|
||
use Cycle\Annotated\Annotation\Entity as Attribute; | ||
use Cycle\Annotated\Exception\AnnotationException; | ||
use Cycle\Annotated\Locator\Entity; | ||
use Cycle\Annotated\Locator\EntityLocatorInterface; | ||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\Tokenizer\Attribute\TargetAttribute; | ||
use Spiral\Tokenizer\TokenizationListenerInterface; | ||
|
||
#[TargetAttribute(Attribute::class, useAnnotations: true)] | ||
final class ListenerEntityLocator implements EntityLocatorInterface, TokenizationListenerInterface | ||
{ | ||
/** | ||
* @var Entity[] | ||
*/ | ||
private array $entities = []; | ||
private bool $collected = false; | ||
|
||
public function __construct( | ||
private readonly ReaderInterface $reader | ||
) { | ||
} | ||
|
||
public function listen(\ReflectionClass $class): void | ||
{ | ||
try { | ||
/** @var Attribute $attribute */ | ||
$attribute = $this->reader->firstClassMetadata($class, Attribute::class); | ||
} catch (\Exception $e) { | ||
throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e); | ||
} | ||
|
||
if ($attribute !== null) { | ||
$this->entities[] = new Entity($attribute, $class); | ||
} | ||
} | ||
|
||
public function finalize(): void | ||
{ | ||
$this->collected = true; | ||
} | ||
|
||
public function getEntities(): array | ||
{ | ||
if (!$this->collected) { | ||
throw new AnnotationException(\sprintf('Tokenizer did not finalize %s listener.', self::class)); | ||
} | ||
|
||
return $this->entities; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\App\Entities; | ||
|
||
use Cycle\Annotated\Annotation\Embeddable; | ||
use Cycle\Annotated\Annotation\Column; | ||
|
||
#[Embeddable] | ||
class Address | ||
{ | ||
#[Column(type: 'string')] | ||
public string $country; | ||
|
||
#[Column(type: 'string(32)')] | ||
public string $city; | ||
|
||
#[Column(type: 'string(100)')] | ||
public string $address; | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/src/Annotated/Locator/ListenerEmbeddingsLocatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Annotated\Locator; | ||
|
||
use Cycle\Annotated\Annotation\Embeddable; | ||
use Cycle\Annotated\Exception\AnnotationException; | ||
use Cycle\Annotated\Locator\Embedding; | ||
use PHPUnit\Framework\TestCase; | ||
use Spiral\App\Entities\Address; | ||
use Spiral\App\Entities\User; | ||
use Spiral\Attributes\AttributeReader; | ||
use Spiral\Cycle\Annotated\Locator\ListenerEmbeddingsLocator; | ||
|
||
final class ListenerEmbeddingsLocatorTest extends TestCase | ||
{ | ||
public function testListen(): void | ||
{ | ||
$locator = new ListenerEmbeddingsLocator(new AttributeReader()); | ||
$locator->listen(new \ReflectionClass(Address::class)); | ||
$locator->finalize(); | ||
|
||
$this->assertEquals( | ||
[ | ||
new Embedding( | ||
new Embeddable(), | ||
new \ReflectionClass(Address::class) | ||
), | ||
], | ||
$locator->getEmbeddings()); | ||
} | ||
|
||
public function testListenWithoutAttribute(): void | ||
{ | ||
$locator = new ListenerEmbeddingsLocator(new AttributeReader()); | ||
$locator->listen(new \ReflectionClass(User::class)); | ||
$locator->finalize(); | ||
|
||
$this->assertSame([], $locator->getEmbeddings()); | ||
} | ||
|
||
public function testGetEmbeddingsWithoutFinalize(): void | ||
{ | ||
$this->expectException(AnnotationException::class); | ||
$this->expectExceptionMessage( | ||
\sprintf('Tokenizer did not finalize %s listener.', ListenerEmbeddingsLocator::class) | ||
); | ||
|
||
$locator = new ListenerEmbeddingsLocator(new AttributeReader()); | ||
$locator->getEmbeddings(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Annotated\Locator; | ||
|
||
use Cycle\Annotated\Exception\AnnotationException; | ||
use Cycle\Annotated\Locator\Entity; | ||
use PHPUnit\Framework\TestCase; | ||
use Spiral\App\Entities\User; | ||
use Spiral\App\Repositories\UserRepository; | ||
use Spiral\Attributes\AttributeReader; | ||
use Spiral\Cycle\Annotated\Locator\ListenerEntityLocator; | ||
|
||
final class ListenerEntityLocatorTest extends TestCase | ||
{ | ||
public function testListen(): void | ||
{ | ||
$locator = new ListenerEntityLocator(new AttributeReader()); | ||
$locator->listen(new \ReflectionClass(User::class)); | ||
$locator->finalize(); | ||
|
||
$this->assertEquals( | ||
[ | ||
new Entity( | ||
new \Cycle\Annotated\Annotation\Entity(repository: UserRepository::class), | ||
new \ReflectionClass(User::class) | ||
), | ||
], | ||
$locator->getEntities()); | ||
} | ||
|
||
public function testListenWithoutAttribute(): void | ||
{ | ||
$locator = new ListenerEntityLocator(new AttributeReader()); | ||
$locator->listen(new \ReflectionClass(\stdClass::class)); | ||
$locator->finalize(); | ||
|
||
$this->assertSame([], $locator->getEntities()); | ||
} | ||
|
||
public function testGetEntitiesWithoutFinalize(): void | ||
{ | ||
$this->expectException(AnnotationException::class); | ||
$this->expectExceptionMessage( | ||
\sprintf('Tokenizer did not finalize %s listener.', ListenerEntityLocator::class) | ||
); | ||
|
||
$locator = new ListenerEntityLocator(new AttributeReader()); | ||
$locator->getEntities(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters