Skip to content

Commit

Permalink
Template types
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Mar 18, 2024
1 parent 34f2c60 commit 5915180
Show file tree
Hide file tree
Showing 40 changed files with 381 additions and 477 deletions.
5 changes: 1 addition & 4 deletions src/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@

final class Postgres implements AdapterInterface
{
private PgClient $client;

private EngineInterface $engine;

public function __construct(PgClient $client)
public function __construct(private PgClient $client)
{
$this->client = $client;
$this->engine = new PostgresEngine();
}

Expand Down
12 changes: 5 additions & 7 deletions src/Annotation/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class Clause
private ?string $foreign_function = null;
//phpcs:enable

/**
* @param string[]|null[] $clause
*/
/** @param string[]|null[] $clause */
public function __construct(array $clause)
{
/** @psalm-suppress RawObjectIteration */
Expand Down Expand Up @@ -54,31 +52,31 @@ public function foreignKey(): string
}

/** @phpstan-ignore-next-line */
public function localCast(): ?string
public function localCast(): string|null
{
//phpcs:disable
return $this->local_cast;
//phpcs:enable
}

/** @phpstan-ignore-next-line */
public function foreignCast(): ?string
public function foreignCast(): string|null
{
//phpcs:disable
return $this->foreign_cast;
//phpcs:enable
}

/** @phpstan-ignore-next-line */
public function localFunction(): ?string
public function localFunction(): string|null
{
//phpcs:disable
return $this->local_function;
//phpcs:enable
}

/** @phpstan-ignore-next-line */
public function foreignFunction(): ?string
public function foreignFunction(): string|null
{
//phpcs:disable
return $this->foreign_function;
Expand Down
8 changes: 2 additions & 6 deletions src/Annotation/InnerJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class InnerJoin implements JoinInterface

private bool $lazy = self::IS_NOT_LAZY;

/**
* @param string[]|array[]|bool[] $innerJoin
*/
/** @param string[]|array[]|bool[] $innerJoin */
public function __construct(array $innerJoin)
{
/** @psalm-suppress RawObjectIteration */
Expand Down Expand Up @@ -54,9 +52,7 @@ public function lazy(): bool
return $this->lazy;
}

/**
* @return Clause[]
*/
/** @return Clause[] */
public function clause(): array
{
return $this->clause;
Expand Down
4 changes: 1 addition & 3 deletions src/Annotation/JoinInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public function property(): string;

public function lazy(): bool;

/**
* @return Clause[]
*/
/** @return Clause[] */
public function clause(): array;
}
8 changes: 2 additions & 6 deletions src/Annotation/LeftJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class LeftJoin implements JoinInterface

private bool $lazy = self::IS_NOT_LAZY;

/**
* @param string[]|array[]|bool[] $leftJoin
*/
/** @param string[]|array[]|bool[] $leftJoin */
public function __construct(array $leftJoin)
{
/** @psalm-suppress RawObjectIteration */
Expand Down Expand Up @@ -54,9 +52,7 @@ public function lazy(): bool
return $this->lazy;
}

/**
* @return Clause[]
*/
/** @return Clause[] */
public function clause(): array
{
return $this->clause;
Expand Down
4 changes: 1 addition & 3 deletions src/Annotation/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ final class Table
{
private string $table;

/**
* @param string[] $table
*/
/** @param string[] $table */
public function __construct(array $table)
{
$this->table = current($table); /** @phpstan-ignore-line */
Expand Down
25 changes: 10 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
use React\Promise\PromiseInterface;
use Rx\Observable;

use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
use function array_key_exists;
use function React\Promise\resolve;

final class Client implements ClientInterface
{
private AdapterInterface $adapter;

private EntityInspector $entityInspector;

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

private MiddlewareRunner $middlewareRunner;
private Connection $connection;

private QueryFactory $queryFactory;

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

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

$this->middlewareRunner = new MiddlewareRunner(...$middleware);
$this->connection = new Connection($this->adapter, new MiddlewareRunner(...$middleware));
}

/**
* @template T
* @param class-string<T> $entity
* @return RepositoryInterface<T>
*/
public function repository(string $entity): RepositoryInterface
{
if (! array_key_exists($entity, $this->repositories)) {
$this->repositories[$entity] = new Repository($this->entityInspector->entity($entity), $this, $this->queryFactory);
$this->repositories[$entity] = new Repository($this->entityInspector->entity($entity), $this, $this->queryFactory, $this->connection);
}

return $this->repositories[$entity];
}

public function query(ExpressionInterface $query): Observable
{
return unwrapObservableFromPromise($this->middlewareRunner->query(
$query,
function (ExpressionInterface $query): PromiseInterface {
return resolve($this->adapter->query($query));
}
));
return $this->connection->query($query);
}
}
9 changes: 9 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@

interface ClientInterface
{

/**
* @template T
* @param class-string<T> $entity
* @return RepositoryInterface<T>
*/
public function repository(string $entity): RepositoryInterface;

/**
* @deprecated This function will disappear at initial release
*/
public function query(ExpressionInterface $query): Observable;
}
5 changes: 1 addition & 4 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

final class Configuration
{
private string $tablePrefix = '';

public function __construct(string $tablePrefix)
public function __construct(private string $tablePrefix = '')
{
$this->tablePrefix = $tablePrefix;
}

public function tablePrefix(): string
Expand Down
39 changes: 39 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace WyriHaximus\React\SimpleORM;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Latitude\QueryBuilder\ExpressionInterface;
use Latitude\QueryBuilder\QueryFactory;
use React\Promise\PromiseInterface;
use Rx\Observable;

use function array_key_exists;
use function React\Promise\resolve;

/**
* @internal
*/
final readonly class Connection
{
public function __construct(
private AdapterInterface $adapter,
private MiddlewareRunner $middlewareRunner
)
{

}

public function query(ExpressionInterface $query): Observable
{
return Observable::fromPromise($this->middlewareRunner->query(
$query,
function (ExpressionInterface $query): PromiseInterface {
return resolve($this->adapter->query($query));
},
))->mergeAll();
}
}
8 changes: 1 addition & 7 deletions src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@

final class Field
{
private string $name;

private string $type;

public function __construct(string $name, string $type)
public function __construct(private string $name, private string $type)
{
$this->name = $name;
$this->type = $type;
}

public function name(): string
Expand Down
20 changes: 3 additions & 17 deletions src/Entity/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@

final class Join
{
private InspectedEntityInterface $entity;

private string $type;

private string $property;

private bool $lazy;

/** @var array<Clause> */
private array $clause;

public function __construct(InspectedEntityInterface $entity, string $type, string $property, bool $lazy, Clause ...$clause)
public function __construct(private InspectedEntityInterface $entity, private string $type, private string $property, private bool $lazy, Clause ...$clause)
{
$this->entity = $entity;
$this->type = $type;
$this->property = $property;
$this->lazy = $lazy;
$this->clause = $clause;
$this->clause = $clause;
}

public function entity(): InspectedEntityInterface
Expand All @@ -49,9 +37,7 @@ public function lazy(): bool
return $this->lazy;
}

/**
* @return Clause[]
*/
/** @return Clause[] */
public function clause(): array
{
return $this->clause;
Expand Down
18 changes: 6 additions & 12 deletions src/EntityInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@
use function array_key_exists;
use function current;
use function method_exists;
use function WyriHaximus\iteratorOrArrayToArray;

final class EntityInspector
{
private Configuration $configuration;
private Reader $annotationReader;

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

public function __construct(Configuration $configuration, Reader $annotationReader)
public function __construct(private Configuration $configuration, private Reader $annotationReader)
{
$this->configuration = $configuration;
$this->annotationReader = $annotationReader;
}

public function entity(string $entity): InspectedEntityInterface
Expand All @@ -51,13 +45,13 @@ public function entity(string $entity): InspectedEntityInterface
* @phpstan-ignore-next-line
* @psalm-suppress ArgumentTypeCoercion
*/
$joins = iteratorOrArrayToArray($this->joins($class));
$joins = [...$this->joins($class)];
/** @psalm-suppress ArgumentTypeCoercion */
$this->entities[$entity] = new InspectedEntity(
$entity,
$this->configuration->tablePrefix() . $tableAnnotation->table(),
iteratorOrArrayToArray($this->fields($class, $joins)), /** @phpstan-ignore-line */
$joins
[...$this->fields($class, $joins)], /** @phpstan-ignore-line */
$joins,
);
}

Expand Down Expand Up @@ -99,7 +93,7 @@ private function fields(ReflectionClass $class, array $joins): iterable
}

return (string) current($property->getDocBlockTypes());
})($roaveProperty)
})($roaveProperty),
);
}
}
Expand All @@ -123,7 +117,7 @@ private function joins(ReflectionClass $class): iterable
$annotation->type(),
$annotation->property(),
$annotation->lazy(),
...$annotation->clause()
...$annotation->clause(),
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace WyriHaximus\React\SimpleORM;

/**
* @property string $id
*/
interface EntityInterface
{
public function id(): string;
}
Loading

0 comments on commit 5915180

Please sign in to comment.