Skip to content

Commit

Permalink
Merge pull request #414 from dotkernel/issue-413
Browse files Browse the repository at this point in the history
Issue #413: Doctrine: replaced annotations with attributes.
  • Loading branch information
arhimede authored Oct 31, 2023
2 parents f7386fa + ef630a4 commit 228d122
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 125 deletions.
10 changes: 4 additions & 6 deletions src/App/src/Common/TimestampAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ trait TimestampAwareTrait
{
private string $dateFormat = 'Y-m-d H:i:s';

/** @ORM\Column(name="created", type="datetime_immutable") */
#[ORM\Column(name: 'created', type: 'datetime_immutable')]
protected ?DateTimeImmutable $created = null;

/** @ORM\Column(name="updated", type="datetime_immutable", nullable=true) */
#[ORM\Column(name: 'updated', type: 'datetime_immutable', nullable: true)]
protected ?DateTimeImmutable $updated = null;

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->touch();
Expand Down
10 changes: 4 additions & 6 deletions src/App/src/Common/UuidAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

trait UuidAwareTrait
{
/**
* @ORM\Id()
* @ORM\Column(name="uuid", type="uuid_binary_ordered_time", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
*/
#[ORM\Id]
#[ORM\Column(name: 'uuid', type: 'uuid_binary_ordered_time', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: \Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator::class)]
protected ?UuidInterface $uuid = null;

public function getUuid(): ?UuidInterface
Expand Down
4 changes: 2 additions & 2 deletions src/Contact/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Frontend\Contact;

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Dot\AnnotatedServices\Factory\AnnotatedRepositoryFactory;
use Dot\AnnotatedServices\Factory\AnnotatedServiceFactory;
use Frontend\Contact\Controller\ContactController;
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getDoctrineConfig(): array
],
],
'ContactEntities' => [
'class' => AnnotationDriver::class,
'class' => AttributeDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/Entity'],
],
Expand Down
19 changes: 9 additions & 10 deletions src/Contact/src/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@

use Doctrine\ORM\Mapping as ORM;
use Frontend\App\Common\AbstractEntity;
use Frontend\Contact\Repository\MessageRepository;

/**
* @ORM\Entity(repositoryClass="Frontend\Contact\Repository\MessageRepository")
* @ORM\Table(name="contact_message")
* @ORM\HasLifecycleCallbacks
*/
#[ORM\Entity(repositoryClass: MessageRepository::class)]
#[ORM\Table(name: 'contact_message')]
#[ORM\HasLifecycleCallbacks]
class Message extends AbstractEntity
{
public const PLATFORM_WEBSITE = 'website';

/** @ORM\Column(name="email", type="string", length=150) */
#[ORM\Column(name: 'email', type: 'string', length: 150)]
protected string $email = '';

/** @ORM\Column(name="name", type="string", length=150) */
#[ORM\Column(name: 'name', type: 'string', length: 150)]
protected string $name = '';

/** @ORM\Column(name="subject", type="text") */
#[ORM\Column(name: 'subject', type: 'text')]
protected string $subject = '';

/** @ORM\Column(name="message", type="text") */
#[ORM\Column(name: 'message', type: 'text')]
protected string $message = '';

/** @ORM\Column(name="platform", type="text") */
#[ORM\Column(name: 'platform', type: 'text')]
protected string $platform = '';

public function __construct(
Expand Down
4 changes: 2 additions & 2 deletions src/User/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Frontend\User;

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Dot\AnnotatedServices\Factory\AnnotatedRepositoryFactory;
use Dot\AnnotatedServices\Factory\AnnotatedServiceFactory;
use Frontend\User\Adapter\AuthenticationAdapter;
Expand Down Expand Up @@ -96,7 +96,7 @@ public function getDoctrineConfig(): array
],
],
'UserEntities' => [
'class' => AnnotationDriver::class,
'class' => AttributeDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/Entity'],
],
Expand Down
45 changes: 21 additions & 24 deletions src/User/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
use Exception;
use Frontend\App\Common\AbstractEntity;
use Frontend\App\Common\UuidOrderedTimeGenerator;
use Frontend\User\Repository\UserRepository;

use function bin2hex;
use function random_bytes;

/**
* @ORM\Entity(repositoryClass="Frontend\User\Repository\UserRepository")
* @ORM\Table(name="user")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'user')]
#[ORM\HasLifecycleCallbacks]
class User extends AbstractEntity implements UserInterface
{
public const STATUS_PENDING = 'pending';
Expand All @@ -37,41 +36,39 @@ class User extends AbstractEntity implements UserInterface
self::IS_DELETED_NO,
];

/** @ORM\OneToOne(targetEntity="Frontend\User\Entity\UserDetail", cascade={"persist", "remove"}, mappedBy="user") */
#[ORM\OneToOne(mappedBy: 'user', targetEntity: UserDetail::class, cascade: ['persist', 'remove'])]
protected UserDetail $detail;

/** @ORM\OneToOne(targetEntity="Frontend\User\Entity\UserAvatar", cascade={"persist", "remove"}, mappedBy="user") */
#[ORM\OneToOne(mappedBy: 'user', targetEntity: UserAvatar::class, cascade: ['persist', 'remove'])]
protected ?UserAvatar $avatar;

/** @ORM\Column(name="identity", type="string", length=191, nullable=false, unique=true) */
#[ORM\Column(name: 'identity', type: 'string', length: 191, unique: true, nullable: false)]
protected string $identity;

/** @ORM\Column(name="password", type="string", length=191, nullable=false) */
#[ORM\Column(name: 'password', type: 'string', length: 191, nullable: false)]
protected string $password;

/** @ORM\Column(name="status", type="string", length=20, columnDefinition="ENUM('pending', 'active')") */
#[ORM\Column(name: 'status', type: 'string', length: 20, columnDefinition: "ENUM('pending', 'active')")]
protected string $status = self::STATUS_PENDING;

/** @ORM\Column(name="isDeleted", type="boolean") */
#[ORM\Column(name: 'isDeleted', type: 'boolean')]
protected bool $isDeleted = self::IS_DELETED_NO;

/** @ORM\Column(name="hash", type="string", length=64, nullable=false, unique=true) */
#[ORM\Column(name: 'hash', type: 'string', length: 64, unique: true, nullable: false)]
protected string $hash;

/**
* @ORM\ManyToMany(targetEntity="Frontend\User\Entity\UserRole")
* @ORM\JoinTable(
* name="user_roles",
* joinColumns={@ORM\JoinColumn(name="userUuid", referencedColumnName="uuid")},
* inverseJoinColumns={@ORM\JoinColumn(name="roleUuid", referencedColumnName="uuid")}
* )
*/
#[ORM\ManyToMany(targetEntity: UserRole::class)]
#[ORM\JoinTable(name: 'user_roles')]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid')]
#[ORM\InverseJoinColumn(name: 'roleUuid', referencedColumnName: 'uuid')]
protected Collection $roles;

/**
* @ORM\OneToMany(targetEntity="UserResetPassword",
* cascade={"persist", "remove"}, mappedBy="user", fetch="EXTRA_LAZY")
*/
#[ORM\OneToMany(
mappedBy: 'user',
targetEntity: UserResetPassword::class,
cascade: ['persist', 'remove'],
fetch: 'EXTRA_LAZY'
)]
protected Collection $resetPasswords;

/**
Expand Down
24 changes: 8 additions & 16 deletions src/User/src/Entity/UserAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,23 @@
use Doctrine\ORM\Mapping as ORM;
use Frontend\App\Common\AbstractEntity;
use Frontend\User\EventListener\UserAvatarEventListener;
use Frontend\User\Repository\UserAvatarRepository;

/**
* @ORM\Entity(repositoryClass="Frontend\User\Repository\UserAvatarRepository")
* @ORM\Table(name="user_avatar")
* @ORM\HasLifecycleCallbacks()
* @ORM\EntityListeners({UserAvatarEventListener::class})
*/
#[ORM\Entity(repositoryClass: UserAvatarRepository::class)]
#[ORM\Table(name: 'user_avatar')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([UserAvatarEventListener::class])]
class UserAvatar extends AbstractEntity
{
/**
* @ORM\OneToOne(targetEntity="Frontend\User\Entity\User", inversedBy="avatar")
* @ORM\JoinColumn(name="userUuid", referencedColumnName="uuid", nullable=false)
*/
#[ORM\OneToOne(inversedBy: 'avatar', targetEntity: User::class)]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid', nullable: false)]
protected UserInterface $user;

/** @ORM\Column(name="name", type="string", length=191) */
#[ORM\Column(name: 'name', type: 'string', length: 191)]
protected string $name;

protected string $url;

public function __construct()
{
parent::__construct();
}

public function getUser(): UserInterface
{
return $this->user;
Expand Down
24 changes: 8 additions & 16 deletions src/User/src/Entity/UserDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,23 @@

use Doctrine\ORM\Mapping as ORM;
use Frontend\App\Common\AbstractEntity;
use Frontend\User\Repository\UserDetailRepository;

/**
* @ORM\Entity(repositoryClass="Frontend\User\Repository\UserDetailRepository")
* @ORM\Table(name="user_detail")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: UserDetailRepository::class)]
#[ORM\Table(name: 'user_detail')]
#[ORM\HasLifecycleCallbacks]
class UserDetail extends AbstractEntity
{
/**
* @ORM\OneToOne(targetEntity="Frontend\User\Entity\User", inversedBy="detail")
* @ORM\JoinColumn(name="userUuid", referencedColumnName="uuid", nullable=false)
*/
#[ORM\OneToOne(inversedBy: 'detail', targetEntity: User::class)]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid', nullable: false)]
protected UserInterface $user;

/** @ORM\Column(name="firstName", type="string", length=191, nullable=true) */
#[ORM\Column(name: 'firstName', type: 'string', length: 191, nullable: true)]
protected string $firstName;

/** @ORM\Column(name="lastName", type="string", length=191, nullable=true) */
#[ORM\Column(name: 'lastName', type: 'string', length: 191, nullable: true)]
protected string $lastName;

public function __construct()
{
parent::__construct();
}

public function getUser(): UserInterface
{
return $this->user;
Expand Down
17 changes: 4 additions & 13 deletions src/User/src/Entity/UserIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@

class UserIdentity implements UserInterface
{
protected string $identity;
protected array $roles;
protected array $details;
protected string $uuid;

public function __construct(
string $uuid,
string $identity,
array $roles = [],
array $details = []
protected string $uuid,
protected string $identity,
protected array $roles = [],
protected array $details = []
) {
$this->uuid = $uuid;
$this->identity = $identity;
$this->roles = $roles;
$this->details = $details;
}

public function getUuid(): string
Expand Down
20 changes: 8 additions & 12 deletions src/User/src/Entity/UserRememberMe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@
use Doctrine\ORM\Mapping as ORM;
use Frontend\App\Common\AbstractEntity;

/**
* @ORM\Entity()
* @ORM\Table(name="user_remember_me")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity]
#[ORM\Table(name: 'user_remember_me')]
#[ORM\HasLifecycleCallbacks]
class UserRememberMe extends AbstractEntity
{
/**
* @ORM\ManyToOne(targetEntity="Frontend\User\Entity\User")
* @ORM\JoinColumn(name="userUuid", referencedColumnName="uuid", nullable=false)
*/
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid', nullable: false)]
protected User $user;

/** @ORM\Column(name="rememberMeToken", type="string", length=100, nullable=false, unique=true) */
#[ORM\Column(name: 'rememberMeToken', type: 'string', length: 100, unique: true, nullable: false)]
protected string $rememberMeToken = '';

/** @ORM\Column(name="userAgent", type="text") */
#[ORM\Column(name: 'userAgent', type: 'text')]
protected ?string $userAgent = null;

/** @ORM\Column(name="expireDate", type="datetime_immutable") */
#[ORM\Column(name: 'expireDate', type: 'datetime_immutable')]
protected DateTimeImmutable $expireDate;

public function getUser(): User
Expand Down
20 changes: 8 additions & 12 deletions src/User/src/Entity/UserResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
use Exception;
use Frontend\App\Common\AbstractEntity;

/**
* @ORM\Entity()
* @ORM\Table(name="user_reset_password")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity]
#[ORM\Table(name: 'user_reset_password')]
#[ORM\HasLifecycleCallbacks]
class UserResetPassword extends AbstractEntity
{
public const STATUS_COMPLETED = 'completed';
Expand All @@ -25,19 +23,17 @@ class UserResetPassword extends AbstractEntity
self::STATUS_REQUESTED,
];

/**
* @ORM\ManyToOne(targetEntity="User", cascade={"persist", "remove"}, inversedBy="resetPasswords")
* @ORM\JoinColumn(name="userUuid", referencedColumnName="uuid", nullable=false)
*/
#[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist', 'remove'], inversedBy: 'resetPasswords')]
#[ORM\JoinColumn(name: 'userUuid', referencedColumnName: 'uuid', nullable: false)]
protected User $user;

/** @ORM\Column(name="expires", type="datetime_immutable", nullable=false) */
#[ORM\Column(name: 'expires', type: 'datetime_immutable', nullable: false)]
protected DateTimeImmutable $expires;

/** @ORM\Column(name="hash", type="string", length=64, nullable=false, unique=true) */
#[ORM\Column(name: 'hash', type: 'string', length: 64, unique: true, nullable: false)]
protected string $hash;

/** @ORM\Column(name="status", type="string", length=20, nullable=false) */
#[ORM\Column(name: 'status', type: 'string', length: 20, nullable: false)]
protected string $status = self::STATUS_REQUESTED;

public function __construct()
Expand Down
11 changes: 5 additions & 6 deletions src/User/src/Entity/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Doctrine\ORM\Mapping as ORM;
use Dot\Authorization\Role\RoleInterface;
use Frontend\App\Common\AbstractEntity;
use Frontend\User\Repository\UserRoleRepository;

/**
* @ORM\Entity(repositoryClass="Frontend\User\Repository\UserRoleRepository")
* @ORM\Table(name="user_role")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: UserRoleRepository::class)]
#[ORM\Table(name: 'user_role')]
#[ORM\HasLifecycleCallbacks]
class UserRole extends AbstractEntity implements RoleInterface
{
public const ROLE_ADMIN = 'admin';
Expand All @@ -24,7 +23,7 @@ class UserRole extends AbstractEntity implements RoleInterface
self::ROLE_GUEST,
];

/** @ORM\Column(name="name", type="string", length=30, nullable=false, unique=true) */
#[ORM\Column(name: 'name', type: 'string', length: 30, unique: true, nullable: false)]
protected string $name;

public function getName(): string
Expand Down

0 comments on commit 228d122

Please sign in to comment.