|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Articulate\Concise; |
| 5 | + |
| 6 | +use Articulate\Concise\Contracts\EntityMapper; |
| 7 | +use Articulate\Concise\Contracts\Mapper; |
| 8 | +use Articulate\Concise\Contracts\Repository; |
| 9 | +use Illuminate\Foundation\Application; |
| 10 | + |
| 11 | +final class Concise |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Illuminate\Foundation\Application |
| 15 | + */ |
| 16 | + private Application $app; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var array<class-string, \Articulate\Concise\Contracts\EntityMapper<*>> |
| 20 | + */ |
| 21 | + private array $entityMappers = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array<class-string, \Articulate\Concise\Contracts\Mapper<*>> |
| 25 | + */ |
| 26 | + private array $componentMappers = []; |
| 27 | + |
| 28 | + public function __construct(Application $app) |
| 29 | + { |
| 30 | + $this->app = $app; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Register a class mapper. |
| 35 | + * |
| 36 | + * @template MapperObject of object |
| 37 | + * |
| 38 | + * @param \Articulate\Concise\Contracts\Mapper<MapperObject> $mapper |
| 39 | + * @param bool $overwrite |
| 40 | + * |
| 41 | + * @return bool |
| 42 | + */ |
| 43 | + public function register(Mapper $mapper, bool $overwrite = false): bool |
| 44 | + { |
| 45 | + if ($mapper instanceof EntityMapper) { |
| 46 | + if ($overwrite === false && isset($this->entityMappers[$mapper->class()])) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + $this->entityMappers[$mapper->class()] = $mapper; |
| 51 | + |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + if ($overwrite === false && isset($this->componentMappers[$mapper->class()])) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + $this->componentMappers[$mapper->class()] = $mapper; |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get a registered entity mapper for the provided class. |
| 66 | + * |
| 67 | + * @template EntityObject of object |
| 68 | + * |
| 69 | + * @param class-string<EntityObject> $class |
| 70 | + * |
| 71 | + * @return \Articulate\Concise\Contracts\EntityMapper<EntityObject>|null |
| 72 | + */ |
| 73 | + public function entity(string $class): ?EntityMapper |
| 74 | + { |
| 75 | + /** @var \Articulate\Concise\Contracts\EntityMapper<EntityObject>|null $mapper */ |
| 76 | + $mapper = $this->entityMappers[$class] ?? null; |
| 77 | + |
| 78 | + return $mapper; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get an entity repository for the provided class. |
| 83 | + * |
| 84 | + * @template EntityObject of object |
| 85 | + * |
| 86 | + * @param class-string<EntityObject> $class |
| 87 | + * |
| 88 | + * @return \Articulate\Concise\Contracts\Repository<EntityObject>|null |
| 89 | + * |
| 90 | + * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 91 | + */ |
| 92 | + public function repository(string $class): ?Repository |
| 93 | + { |
| 94 | + /** @var \Articulate\Concise\Contracts\EntityMapper<EntityObject>|null $mapper */ |
| 95 | + $mapper = $this->entity($class); |
| 96 | + |
| 97 | + if ($mapper === null) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $repository = $mapper->repository(); |
| 102 | + |
| 103 | + if ($repository === null) { |
| 104 | + return new EntityRepository( |
| 105 | + $mapper, |
| 106 | + $this->app->make('db')->connection($mapper->connection()), |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return $this->app->make($repository, [ |
| 111 | + 'mapper' => $mapper, |
| 112 | + 'connection' => $this->app->make('db')->connection($mapper->connection()), |
| 113 | + ]); |
| 114 | + } |
| 115 | +} |
0 commit comments