Skip to content

Commit 014ed93

Browse files
authored
Merge pull request #121 from WyriHaximus/add-configuration-object
Add configuration object
2 parents ae32ff5 + 6c7a6f7 commit 014ed93

File tree

8 files changed

+46
-20
lines changed

8 files changed

+46
-20
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"react/promise": "^2.9",
2222
"react/stream": "^1.1",
2323
"reactivex/rxphp": "^2.0",
24-
"roave/better-reflection": "^5 || ^4.0",
24+
"roave/better-reflection": "^6 || ^5 || ^4.0",
2525
"thecodingmachine/safe": "^1.3",
2626
"voryx/pgasync": "^2.0",
2727
"wyrihaximus/constants": "^1.5",

src/Client.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ final class Client implements ClientInterface
2828

2929
private QueryFactory $queryFactory;
3030

31-
public static function create(AdapterInterface $adapter, MiddlewareInterface ...$middleware): self
31+
public static function create(AdapterInterface $adapter, Configuration $configuration, MiddlewareInterface ...$middleware): self
3232
{
33-
return new self($adapter, new AnnotationReader(), ...$middleware);
33+
return new self($adapter, $configuration, new AnnotationReader(), ...$middleware);
3434
}
3535

36-
public static function createWithAnnotationReader(AdapterInterface $adapter, Reader $annotationReader, MiddlewareInterface ...$middleware): self
36+
public static function createWithAnnotationReader(AdapterInterface $adapter, Configuration $configuration, Reader $annotationReader, MiddlewareInterface ...$middleware): self
3737
{
38-
return new self($adapter, $annotationReader, ...$middleware);
38+
return new self($adapter, $configuration, $annotationReader, ...$middleware);
3939
}
4040

41-
private function __construct(AdapterInterface $adapter, Reader $annotationReader, MiddlewareInterface ...$middleware)
41+
private function __construct(AdapterInterface $adapter, Configuration $configuration, Reader $annotationReader, MiddlewareInterface ...$middleware)
4242
{
4343
$this->adapter = $adapter;
44-
$this->entityInspector = new EntityInspector($annotationReader);
44+
$this->entityInspector = new EntityInspector($configuration, $annotationReader);
4545
$this->queryFactory = new QueryFactory($adapter->engine());
4646

4747
$this->middlewareRunner = new MiddlewareRunner(...$middleware);

src/Configuration.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WyriHaximus\React\SimpleORM;
6+
7+
final class Configuration
8+
{
9+
private string $tablePrefix = '';
10+
11+
public function __construct(string $tablePrefix)
12+
{
13+
$this->tablePrefix = $tablePrefix;
14+
}
15+
16+
public function tablePrefix(): string
17+
{
18+
return $this->tablePrefix;
19+
}
20+
}

src/EntityInspector.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
final class EntityInspector
2323
{
24+
private Configuration $configuration;
2425
private Reader $annotationReader;
2526

2627
/** @var InspectedEntityInterface[] */
2728
private array $entities = [];
2829

29-
public function __construct(Reader $annotationReader)
30+
public function __construct(Configuration $configuration, Reader $annotationReader)
3031
{
32+
$this->configuration = $configuration;
3133
$this->annotationReader = $annotationReader;
3234
}
3335

@@ -53,7 +55,7 @@ public function entity(string $entity): InspectedEntityInterface
5355
/** @psalm-suppress ArgumentTypeCoercion */
5456
$this->entities[$entity] = new InspectedEntity(
5557
$entity,
56-
$tableAnnotation->table(),
58+
$this->configuration->tablePrefix() . $tableAnnotation->table(),
5759
iteratorOrArrayToArray($this->fields($class, $joins)), /** @phpstan-ignore-line */
5860
$joins
5961
);

tests/EntityInspectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function setUp(): void
2323
{
2424
parent::setUp();
2525

26-
$this->entityInspector = new EntityInspector(new AnnotationReader());
26+
$this->entityInspector = new EntityInspector(new Configuration(''), new AnnotationReader());
2727
}
2828

2929
/**

tests/FunctionalTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use WyriHaximus\React\SimpleORM\Adapter\Postgres;
1313
use WyriHaximus\React\SimpleORM\Client;
1414
use WyriHaximus\React\SimpleORM\ClientInterface;
15+
use WyriHaximus\React\SimpleORM\Configuration;
1516
use WyriHaximus\React\SimpleORM\Middleware\QueryCountMiddleware;
1617
use WyriHaximus\React\SimpleORM\Query\Limit;
1718
use WyriHaximus\React\SimpleORM\Query\Where;
@@ -60,6 +61,7 @@ protected function setUp(): void
6061
Loop::get()
6162
)
6263
),
64+
new Configuration(''),
6365
$this->counter
6466
);
6567
}

tests/HydratorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WyriHaximus\React\Tests\SimpleORM;
66

77
use Doctrine\Common\Annotations\AnnotationReader;
8+
use WyriHaximus\React\SimpleORM\Configuration;
89
use WyriHaximus\React\SimpleORM\EntityInspector;
910
use WyriHaximus\React\SimpleORM\Hydrator;
1011
use WyriHaximus\React\Tests\SimpleORM\Stub\BlogPostStub;
@@ -29,7 +30,7 @@ public function testHydrate(): void
2930
$title = 'tables.title';
3031

3132
$entity = (new Hydrator())->hydrate(
32-
(new EntityInspector(new AnnotationReader()))->entity(UserStub::class),
33+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(UserStub::class),
3334
[
3435
'id' => $id,
3536
'name' => $title,
@@ -48,7 +49,7 @@ public function testHydrateIgnoringNonExistingFields(): void
4849
$title = 'tables.title';
4950

5051
$entity = (new Hydrator())->hydrate(
51-
(new EntityInspector(new AnnotationReader()))->entity(UserStub::class),
52+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(UserStub::class),
5253
[
5354
'doesnotexist' => resolve(true),
5455
'id' => $id,
@@ -73,7 +74,7 @@ public function testHydrateWithJoins(): void
7374
$publisherName = 'dasdsadas';
7475

7576
$entity = (new Hydrator())->hydrate(
76-
(new EntityInspector(new AnnotationReader()))->entity(BlogPostStub::class),
77+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(BlogPostStub::class),
7778
[
7879
'doesnotexist' => resolve(true),
7980
'id' => $id,
@@ -121,7 +122,7 @@ public function testHydrateWithJoinsIgnoringNonExistingFields(): void
121122
$publisherName = 'dasdsadas';
122123

123124
$entity = (new Hydrator())->hydrate(
124-
(new EntityInspector(new AnnotationReader()))->entity(BlogPostStub::class),
125+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(BlogPostStub::class),
125126
[
126127
'id' => $id,
127128
'author_id' => $authorId,

tests/RepositoryTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Prophecy\Prophecy\ObjectProphecy;
1313
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
1414
use WyriHaximus\React\SimpleORM\ClientInterface;
15+
use WyriHaximus\React\SimpleORM\Configuration;
1516
use WyriHaximus\React\SimpleORM\EntityInspector;
1617
use WyriHaximus\React\SimpleORM\Query\Order;
1718
use WyriHaximus\React\SimpleORM\Query\Where;
@@ -53,7 +54,7 @@ public function testCount(): void
5354
assert($client instanceof ClientInterface);
5455

5556
$repository = new Repository(
56-
(new EntityInspector(new AnnotationReader()))->entity(UserStub::class),
57+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(UserStub::class),
5758
$client,
5859
new QueryFactory()
5960
);
@@ -79,7 +80,7 @@ public function testCountWithContraints(): void
7980
assert($client instanceof ClientInterface);
8081

8182
$repository = new Repository(
82-
(new EntityInspector(new AnnotationReader()))->entity(UserStub::class),
83+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(UserStub::class),
8384
$client,
8485
new QueryFactory()
8586
);
@@ -113,7 +114,7 @@ public function testCountWithJoins(): void
113114
assert($client instanceof ClientInterface);
114115

115116
$repository = new Repository(
116-
(new EntityInspector(new AnnotationReader()))->entity(BlogPostStub::class),
117+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(BlogPostStub::class),
117118
$client,
118119
new QueryFactory()
119120
);
@@ -167,7 +168,7 @@ public function testFetchWithJoins(): void
167168
assert($client instanceof ClientInterface);
168169

169170
$repository = new Repository(
170-
(new EntityInspector(new AnnotationReader()))->entity(BlogPostStub::class),
171+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(BlogPostStub::class),
171172
$client,
172173
new QueryFactory()
173174
);
@@ -194,7 +195,7 @@ public function testFetchWithJoinsLazyLoadComments(): void
194195
assert($client instanceof ClientInterface);
195196

196197
$this->client->repository(CommentStub::class)->shouldBeCalled()->willReturn(
197-
new Repository((new EntityInspector(new AnnotationReader()))->entity(CommentStub::class), $client, new QueryFactory())
198+
new Repository((new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(CommentStub::class), $client, new QueryFactory())
198199
);
199200

200201
$this->client->query(Argument::that(static function (ExpressionInterface $expression): bool {
@@ -342,7 +343,7 @@ public function testFetchWithJoinsLazyLoadComments(): void
342343
]));
343344

344345
$repository = new Repository(
345-
(new EntityInspector(new AnnotationReader()))->entity(BlogPostStub::class),
346+
(new EntityInspector(new Configuration(''), new AnnotationReader()))->entity(BlogPostStub::class),
346347
$client,
347348
new QueryFactory()
348349
);

0 commit comments

Comments
 (0)