Skip to content

Commit

Permalink
Support for generated fields (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Feb 8, 2024
1 parent bb376d2 commit 4024d2e
Show file tree
Hide file tree
Showing 34 changed files with 314 additions and 47 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"cycle/orm": "^2.2.0",
"cycle/schema-builder": "^2.6",
"cycle/orm": "^2.7",
"cycle/schema-builder": "^2.8",
"doctrine/annotations": "^1.14.3 || ^2.0.1",
"spiral/attributes": "^2.8|^3.0",
"spiral/tokenizer": "^2.8|^3.0",
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target({"PROPERTY", "ANNOTATION", "CLASS"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Embeddable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target({"PROPERTY", "ANNOTATION", "CLASS"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
Expand Down
37 changes: 37 additions & 0 deletions src/Annotation/GeneratedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Annotation;

use Cycle\ORM\Schema\GeneratedField;
use Spiral\Attributes\NamedArgumentConstructor;

/**
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
#[NamedArgumentConstructor]
class GeneratedValue
{
public function __construct(
protected bool $beforeInsert = false,
protected bool $onInsert = false,
protected bool $beforeUpdate = false,
) {
}

public function getFlags(): ?int
{
if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) {
return null;
}

return
($this->beforeInsert ? GeneratedField::BEFORE_INSERT : 0) |
($this->onInsert ? GeneratedField::ON_INSERT : 0) |
($this->beforeUpdate ? GeneratedField::BEFORE_UPDATE : 0);
}
}
2 changes: 0 additions & 2 deletions src/Annotation/Inheritance/DiscriminatorColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Inheritance/JoinedTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Inheritance/SingleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/Inverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target({"PROPERTY", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/Morphed/BelongsToMorphed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/Morphed/MorphedHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/Morphed/MorphedHasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Relation/RefersTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target({"CLASS", "ANNOTATION"})
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Table/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("ANNOTATION", "CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE), NamedArgumentConstructor]
Expand Down
2 changes: 0 additions & 2 deletions src/Annotation/Table/PrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("ANNOTATION", "CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
Expand Down
30 changes: 28 additions & 2 deletions src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use Cycle\Annotated\Annotation\Embeddable;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\ForeignKey;
use Cycle\Annotated\Annotation\GeneratedValue;
use Cycle\Annotated\Annotation\Relation as RelationAnnotation;
use Cycle\Annotated\Exception\AnnotationException;
use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException;
use Cycle\Annotated\Exception\AnnotationWrongTypeArgumentException;
use Cycle\Annotated\Utils\EntityUtils;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\Schema\Definition\Entity as EntitySchema;
use Cycle\Schema\Definition\ForeignKey as ForeignKeySchema;
use Cycle\Schema\Definition\Field;
Expand All @@ -22,7 +24,6 @@
use Doctrine\Common\Annotations\Reader as DoctrineReader;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\Rules\English\InflectorFactory;
use Exception;
use Spiral\Attributes\ReaderInterface;

final class Configurator
Expand Down Expand Up @@ -91,7 +92,7 @@ public function initFields(EntitySchema $entity, \ReflectionClass $class, string
foreach ($class->getProperties() as $property) {
try {
$column = $this->reader->firstPropertyMetadata($property, Column::class);
} catch (Exception $e) {
} catch (\Exception $e) {
throw new AnnotationException($e->getMessage(), $e->getCode(), $e);
} catch (\ArgumentCountError $e) {
throw AnnotationRequiredArgumentsException::createFor($property, Column::class, $e);
Expand Down Expand Up @@ -221,6 +222,9 @@ public function initField(string $name, Column $column, \ReflectionClass $class,
$field->setType($column->getType());
$field->setColumn($columnPrefix . ($column->getColumn() ?? $this->inflector->tableize($name)));
$field->setPrimary($column->isPrimary());
if ($this->isOnInsertGeneratedField($field)) {
$field->setGenerated(GeneratedField::ON_INSERT);
}

$field->setTypecast($this->resolveTypecast($column->getTypecast(), $class));

Expand Down Expand Up @@ -286,6 +290,20 @@ public function initForeignKeys(Entity $ann, EntitySchema $entity, \ReflectionCl
}
}

public function initGeneratedFields(EntitySchema $entity, \ReflectionClass $class): void
{
foreach ($class->getProperties() as $property) {
try {
$generated = $this->reader->firstPropertyMetadata($property, GeneratedValue::class);
if ($generated !== null) {
$entity->getFields()->get($property->getName())->setGenerated($generated->getFlags());
}
} catch (\Throwable $e) {
throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e);
}
}
}

/**
* Resolve class or role name relative to the current class.
*/
Expand Down Expand Up @@ -368,4 +386,12 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name
throw new AnnotationException($e->getMessage(), $e->getCode(), $e);
}
}

private function isOnInsertGeneratedField(Field $field): bool
{
return match ($field->getType()) {
'serial', 'bigserial', 'smallserial' => true,
default => $field->isPrimary()
};
}
}
5 changes: 4 additions & 1 deletion src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function run(Registry $registry): Registry
continue;
}

// generated fields
$this->generator->initGeneratedFields($e, $class);

// register entity (OR find parent)
$registry->register($e);
$registry->linkTable($e, $e->getDatabase(), $e->getTableName());
Expand All @@ -99,7 +102,7 @@ public function run(Registry $registry): Registry
private function normalizeNames(Registry $registry): Registry
{
foreach ($this->locator->getClasses() as $class) {
if (! $registry->hasEntity($class->getName())) {
if (!$registry->hasEntity($class->getName())) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/AnnotationRequiredArgumentsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function createFor(\ReflectionProperty $property, string $annotati

$requiredArguments = [];
foreach ($column->getConstructor()->getParameters() as $parameter) {
if (! $parameter->isOptional()) {
if (!$parameter->isOptional()) {
$requiredArguments[] = $parameter->getName();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Fixtures25\PostgreSQL;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

/**
* @Entity(role="withGeneratedSerial", table="with_generated_serial")
*/
#[Entity(role: 'withGeneratedSerial', table: 'with_generated_serial')]
class WithGeneratedSerial
{
/**
* @Column(type="primary")
*/
#[Column(type: 'primary')]
public int $id;

/**
* @Column(type="smallserial", name="small_serial")
*/
#[Column(type: 'smallserial', name: 'small_serial')]
public int $smallSerial;

/**
* @Column(type="serial")
*/
#[Column(type: 'serial')]
public int $serial;

/**
* @Column(type="bigserial", name="big_serial")
*/
#[Column(type: 'bigserial', name: 'big_serial')]
public int $bigSerial;
}
Loading

0 comments on commit 4024d2e

Please sign in to comment.