Skip to content

Commit ecd704c

Browse files
authored
Merge pull request #129 from WyriHaximus/template-types
Template types
2 parents 34f2c60 + 5915180 commit ecd704c

40 files changed

+381
-477
lines changed

src/Adapter/Postgres.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020

2121
final class Postgres implements AdapterInterface
2222
{
23-
private PgClient $client;
24-
2523
private EngineInterface $engine;
2624

27-
public function __construct(PgClient $client)
25+
public function __construct(private PgClient $client)
2826
{
29-
$this->client = $client;
3027
$this->engine = new PostgresEngine();
3128
}
3229

src/Annotation/Clause.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ final class Clause
2424
private ?string $foreign_function = null;
2525
//phpcs:enable
2626

27-
/**
28-
* @param string[]|null[] $clause
29-
*/
27+
/** @param string[]|null[] $clause */
3028
public function __construct(array $clause)
3129
{
3230
/** @psalm-suppress RawObjectIteration */
@@ -54,31 +52,31 @@ public function foreignKey(): string
5452
}
5553

5654
/** @phpstan-ignore-next-line */
57-
public function localCast(): ?string
55+
public function localCast(): string|null
5856
{
5957
//phpcs:disable
6058
return $this->local_cast;
6159
//phpcs:enable
6260
}
6361

6462
/** @phpstan-ignore-next-line */
65-
public function foreignCast(): ?string
63+
public function foreignCast(): string|null
6664
{
6765
//phpcs:disable
6866
return $this->foreign_cast;
6967
//phpcs:enable
7068
}
7169

7270
/** @phpstan-ignore-next-line */
73-
public function localFunction(): ?string
71+
public function localFunction(): string|null
7472
{
7573
//phpcs:disable
7674
return $this->local_function;
7775
//phpcs:enable
7876
}
7977

8078
/** @phpstan-ignore-next-line */
81-
public function foreignFunction(): ?string
79+
public function foreignFunction(): string|null
8280
{
8381
//phpcs:disable
8482
return $this->foreign_function;

src/Annotation/InnerJoin.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ final class InnerJoin implements JoinInterface
2424

2525
private bool $lazy = self::IS_NOT_LAZY;
2626

27-
/**
28-
* @param string[]|array[]|bool[] $innerJoin
29-
*/
27+
/** @param string[]|array[]|bool[] $innerJoin */
3028
public function __construct(array $innerJoin)
3129
{
3230
/** @psalm-suppress RawObjectIteration */
@@ -54,9 +52,7 @@ public function lazy(): bool
5452
return $this->lazy;
5553
}
5654

57-
/**
58-
* @return Clause[]
59-
*/
55+
/** @return Clause[] */
6056
public function clause(): array
6157
{
6258
return $this->clause;

src/Annotation/JoinInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function property(): string;
1717

1818
public function lazy(): bool;
1919

20-
/**
21-
* @return Clause[]
22-
*/
20+
/** @return Clause[] */
2321
public function clause(): array;
2422
}

src/Annotation/LeftJoin.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ final class LeftJoin implements JoinInterface
2424

2525
private bool $lazy = self::IS_NOT_LAZY;
2626

27-
/**
28-
* @param string[]|array[]|bool[] $leftJoin
29-
*/
27+
/** @param string[]|array[]|bool[] $leftJoin */
3028
public function __construct(array $leftJoin)
3129
{
3230
/** @psalm-suppress RawObjectIteration */
@@ -54,9 +52,7 @@ public function lazy(): bool
5452
return $this->lazy;
5553
}
5654

57-
/**
58-
* @return Clause[]
59-
*/
55+
/** @return Clause[] */
6056
public function clause(): array
6157
{
6258
return $this->clause;

src/Annotation/Table.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ final class Table
1616
{
1717
private string $table;
1818

19-
/**
20-
* @param string[] $table
21-
*/
19+
/** @param string[] $table */
2220
public function __construct(array $table)
2321
{
2422
$this->table = current($table); /** @phpstan-ignore-line */

src/Client.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@
1111
use React\Promise\PromiseInterface;
1212
use Rx\Observable;
1313

14-
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
1514
use function array_key_exists;
1615
use function React\Promise\resolve;
1716

1817
final class Client implements ClientInterface
1918
{
20-
private AdapterInterface $adapter;
21-
2219
private EntityInspector $entityInspector;
2320

24-
/** @var array<string, Repository> */
2521
private array $repositories = [];
2622

27-
private MiddlewareRunner $middlewareRunner;
23+
private Connection $connection;
2824

2925
private QueryFactory $queryFactory;
3026

@@ -38,31 +34,30 @@ public static function createWithAnnotationReader(AdapterInterface $adapter, Con
3834
return new self($adapter, $configuration, $annotationReader, ...$middleware);
3935
}
4036

41-
private function __construct(AdapterInterface $adapter, Configuration $configuration, Reader $annotationReader, MiddlewareInterface ...$middleware)
37+
private function __construct(private AdapterInterface $adapter, Configuration $configuration, Reader $annotationReader, MiddlewareInterface ...$middleware)
4238
{
43-
$this->adapter = $adapter;
4439
$this->entityInspector = new EntityInspector($configuration, $annotationReader);
4540
$this->queryFactory = new QueryFactory($adapter->engine());
4641

47-
$this->middlewareRunner = new MiddlewareRunner(...$middleware);
42+
$this->connection = new Connection($this->adapter, new MiddlewareRunner(...$middleware));
4843
}
4944

45+
/**
46+
* @template T
47+
* @param class-string<T> $entity
48+
* @return RepositoryInterface<T>
49+
*/
5050
public function repository(string $entity): RepositoryInterface
5151
{
5252
if (! array_key_exists($entity, $this->repositories)) {
53-
$this->repositories[$entity] = new Repository($this->entityInspector->entity($entity), $this, $this->queryFactory);
53+
$this->repositories[$entity] = new Repository($this->entityInspector->entity($entity), $this, $this->queryFactory, $this->connection);
5454
}
5555

5656
return $this->repositories[$entity];
5757
}
5858

5959
public function query(ExpressionInterface $query): Observable
6060
{
61-
return unwrapObservableFromPromise($this->middlewareRunner->query(
62-
$query,
63-
function (ExpressionInterface $query): PromiseInterface {
64-
return resolve($this->adapter->query($query));
65-
}
66-
));
61+
return $this->connection->query($query);
6762
}
6863
}

src/ClientInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99

1010
interface ClientInterface
1111
{
12+
13+
/**
14+
* @template T
15+
* @param class-string<T> $entity
16+
* @return RepositoryInterface<T>
17+
*/
1218
public function repository(string $entity): RepositoryInterface;
1319

20+
/**
21+
* @deprecated This function will disappear at initial release
22+
*/
1423
public function query(ExpressionInterface $query): Observable;
1524
}

src/Configuration.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
final class Configuration
88
{
9-
private string $tablePrefix = '';
10-
11-
public function __construct(string $tablePrefix)
9+
public function __construct(private string $tablePrefix = '')
1210
{
13-
$this->tablePrefix = $tablePrefix;
1411
}
1512

1613
public function tablePrefix(): string

src/Connection.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WyriHaximus\React\SimpleORM;
6+
7+
use Doctrine\Common\Annotations\AnnotationReader;
8+
use Doctrine\Common\Annotations\Reader;
9+
use Latitude\QueryBuilder\ExpressionInterface;
10+
use Latitude\QueryBuilder\QueryFactory;
11+
use React\Promise\PromiseInterface;
12+
use Rx\Observable;
13+
14+
use function array_key_exists;
15+
use function React\Promise\resolve;
16+
17+
/**
18+
* @internal
19+
*/
20+
final readonly class Connection
21+
{
22+
public function __construct(
23+
private AdapterInterface $adapter,
24+
private MiddlewareRunner $middlewareRunner
25+
)
26+
{
27+
28+
}
29+
30+
public function query(ExpressionInterface $query): Observable
31+
{
32+
return Observable::fromPromise($this->middlewareRunner->query(
33+
$query,
34+
function (ExpressionInterface $query): PromiseInterface {
35+
return resolve($this->adapter->query($query));
36+
},
37+
))->mergeAll();
38+
}
39+
}

0 commit comments

Comments
 (0)