Skip to content

Commit d1c5d96

Browse files
committed
Code chapter 2
Signed-off-by: Howriq <[email protected]>
1 parent b5a00a7 commit d1c5d96

File tree

13 files changed

+132
-177
lines changed

13 files changed

+132
-177
lines changed
File renamed without changes.

src/App/src/ConfigProvider.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
use Doctrine\ORM\EntityManager;
88
use Doctrine\ORM\EntityManagerInterface;
9-
use Doctrine\ORM\Mapping\EntityListenerResolver as EntityListenerResolverInterface;
109
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
1110
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
1211
use Dot\Cache\Adapter\ArrayAdapter;
1312
use Dot\Cache\Adapter\FilesystemAdapter;
14-
use Light\App\Factory\EntityListenerResolverFactory;
1513
use Light\App\Factory\GetIndexViewHandlerFactory;
1614
use Light\App\Handler\GetIndexViewHandler;
17-
use Light\App\Resolver\EntityListenerResolver;
1815
use Light\App\Types\UuidType;
1916
use Mezzio\Application;
2017
use Roave\PsrContainerDoctrine\EntityManagerFactory;
@@ -40,7 +37,6 @@
4037
* },
4138
* configuration: array{
4239
* orm_default: array{
43-
* entity_listener_resolver: class-string<EntityListenerResolverInterface>,
4440
* result_cache: non-empty-string,
4541
* metadata_cache: non-empty-string,
4642
* query_cache: non-empty-string,
@@ -100,7 +96,6 @@ public function getDependencies(): array
10096
'factories' => [
10197
'doctrine.entity_manager.orm_default' => EntityManagerFactory::class,
10298
GetIndexViewHandler::class => GetIndexViewHandlerFactory::class,
103-
EntityListenerResolver::class => EntityListenerResolverFactory::class,
10499
],
105100
'aliases' => [
106101
EntityManager::class => 'doctrine.entity_manager.orm_default',
@@ -149,13 +144,12 @@ private function getDoctrineConfig(): array
149144
],
150145
'configuration' => [
151146
'orm_default' => [
152-
'entity_listener_resolver' => EntityListenerResolver::class,
153-
'result_cache' => 'filesystem',
154-
'metadata_cache' => 'filesystem',
155-
'query_cache' => 'filesystem',
156-
'hydration_cache' => 'array',
157-
'typed_field_mapper' => null,
158-
'second_level_cache' => [
147+
'result_cache' => 'filesystem',
148+
'metadata_cache' => 'filesystem',
149+
'query_cache' => 'filesystem',
150+
'hydration_cache' => 'array',
151+
'typed_field_mapper' => null,
152+
'second_level_cache' => [
159153
'enabled' => true,
160154
'default_lifetime' => 3600,
161155
'default_lock_lifetime' => 60,

src/App/src/Entity/AbstractEntity.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace Light\App\Entity;
66

7+
use DateTimeImmutable;
78
use Doctrine\ORM\Mapping as ORM;
89
use Laminas\Stdlib\ArraySerializableInterface;
10+
use Ramsey\Uuid\Uuid;
11+
use Ramsey\Uuid\UuidInterface;
912

1013
use function is_array;
1114
use function method_exists;
@@ -14,21 +17,67 @@
1417
#[ORM\MappedSuperclass]
1518
abstract class AbstractEntity implements ArraySerializableInterface
1619
{
20+
#[ORM\Column(name: 'created', type: 'datetime_immutable', nullable: false)]
21+
protected DateTimeImmutable $created;
22+
23+
#[ORM\Column(name: 'updated', type: 'datetime_immutable', nullable: true)]
24+
protected ?DateTimeImmutable $updated = null;
25+
26+
#[ORM\Id]
27+
#[ORM\Column(name: 'id', type: 'uuid', unique: true, nullable: false)]
28+
protected UuidInterface $uuid;
29+
30+
#[ORM\PrePersist]
31+
public function created(): void
32+
{
33+
$this->created = new DateTimeImmutable();
34+
}
35+
36+
#[ORM\PreUpdate]
37+
public function touch(): void
38+
{
39+
$this->updated = new DateTimeImmutable();
40+
}
41+
1742
public function __construct()
1843
{
19-
$this->initId();
44+
$this->uuid = Uuid::uuid7();
2045
}
2146

22-
protected function initId(): void
47+
public function getId(): UuidInterface
2348
{
49+
return $this->uuid;
2450
}
2551

26-
/**
27-
* Override this method in soft-deletable entities
28-
*/
29-
public function isDeleted(): bool
52+
public function setId(UuidInterface $id): static
53+
{
54+
$this->uuid = $id;
55+
56+
return $this;
57+
}
58+
59+
public function getCreated(): ?DateTimeImmutable
3060
{
31-
return false;
61+
return $this->created;
62+
}
63+
64+
public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string
65+
{
66+
return $this->created->format($dateFormat);
67+
}
68+
69+
public function getUpdated(): ?DateTimeImmutable
70+
{
71+
return $this->updated;
72+
}
73+
74+
public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string
75+
{
76+
if ($this->updated instanceof DateTimeImmutable) {
77+
return $this->updated->format($dateFormat);
78+
}
79+
80+
return null;
3281
}
3382

3483
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Entity;
6+
7+
use DateTimeImmutable;
8+
use Ramsey\Uuid\UuidInterface;
9+
10+
interface EntityInterface
11+
{
12+
public function getId(): UuidInterface;
13+
14+
public function getCreated(): ?DateTimeImmutable;
15+
16+
public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string;
17+
18+
public function getUpdated(): ?DateTimeImmutable;
19+
20+
public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string;
21+
}

src/App/src/Entity/TimestampsTrait.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/App/src/Entity/UuidIdentifierTrait.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/App/src/Factory/EntityListenerResolverFactory.php

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Repository;
6+
7+
use Doctrine\ORM\EntityRepository;
8+
use Doctrine\ORM\QueryBuilder;
9+
use Light\App\Entity\EntityInterface;
10+
11+
/**
12+
* @extends EntityRepository<object>
13+
*/
14+
class AbstractRepository extends EntityRepository
15+
{
16+
public function deleteResource(EntityInterface $resource): void
17+
{
18+
$this->getEntityManager()->remove($resource);
19+
$this->getEntityManager()->flush();
20+
}
21+
22+
public function getQueryBuilder(): QueryBuilder
23+
{
24+
return $this->getEntityManager()->createQueryBuilder();
25+
}
26+
27+
public function saveResource(EntityInterface $resource): void
28+
{
29+
$this->getEntityManager()->persist($resource);
30+
$this->getEntityManager()->flush();
31+
}
32+
}

src/Book/src/ConfigProvider.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
/**
1111
* @phpstan-type ConfigType array{
12-
* dependencies: DependenciesType,
1312
* doctrine: DoctrineConfigType,
1413
* }
1514
* @phpstan-type DoctrineConfigType array{
@@ -24,9 +23,6 @@
2423
* },
2524
* },
2625
* }
27-
* @phpstan-type DependenciesType array{
28-
* factories: array<class-string, class-string>,
29-
* }
3026
*/
3127
class ConfigProvider
3228
{
@@ -36,19 +32,7 @@ class ConfigProvider
3632
public function __invoke(): array
3733
{
3834
return [
39-
'dependencies' => $this->getDependencies(),
40-
'doctrine' => $this->getDoctrineConfig(),
41-
];
42-
}
43-
44-
/**
45-
* @return DependenciesType
46-
*/
47-
private function getDependencies(): array
48-
{
49-
return [
50-
'delegators' => [],
51-
'factories' => [],
35+
'doctrine' => $this->getDoctrineConfig(),
5236
];
5337
}
5438

src/Book/src/Entity/Book.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66

77
use Doctrine\ORM\Mapping as ORM;
88
use Light\App\Entity\AbstractEntity;
9-
use Light\App\Entity\TimestampsTrait;
10-
use Light\App\Entity\UuidIdentifierTrait;
9+
use Light\Book\Repository\BookRepository;
1110

12-
#[ORM\Entity]
11+
#[ORM\Entity(repositoryClass: BookRepository::class)]
1312
#[ORM\Table(name: 'books')]
1413
#[ORM\HasLifecycleCallbacks]
1514
class Book extends AbstractEntity
1615
{
17-
use TimestampsTrait;
18-
use UuidIdentifierTrait;
19-
2016
#[ORM\Column(name: 'title', type: 'string', length: 500, nullable: true)]
2117
private ?string $title = null;
2218

@@ -26,8 +22,6 @@ class Book extends AbstractEntity
2622
public function __construct()
2723
{
2824
parent::__construct();
29-
30-
$this->created();
3125
}
3226

3327
public function getTitle(): ?string

0 commit comments

Comments
 (0)