|
4 | 4 | namespace Articulate\Concise\Support; |
5 | 5 |
|
6 | 6 | use Articulate\Concise\Concise; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Support\Collection; |
7 | 9 | use Illuminate\Support\ServiceProvider; |
8 | 10 |
|
9 | | -abstract class MapperServiceProvider extends ServiceProvider |
| 11 | +class MapperServiceProvider extends ServiceProvider |
10 | 12 | { |
11 | 13 | /** |
12 | 14 | * @var array<class-string, class-string<\Articulate\Concise\Contracts\EntityMapper<*>>> |
13 | 15 | */ |
14 | 16 | protected array $mappers = []; |
15 | 17 |
|
| 18 | + /** |
| 19 | + * Indicates if mappers should be discovered. |
| 20 | + * |
| 21 | + * @var bool |
| 22 | + */ |
| 23 | + protected static bool $shouldDiscoverMappers = true; |
| 24 | + |
| 25 | + /** |
| 26 | + * The configured mapper discovery paths. |
| 27 | + * |
| 28 | + * @var array<string> |
| 29 | + */ |
| 30 | + protected static array $mapperDiscoveryPaths = []; |
| 31 | + |
16 | 32 | public function register(): void |
17 | 33 | { |
18 | 34 | $this->app->afterResolving(Concise::class, function (Concise $concise) { |
19 | | - foreach ($this->mappers as $mapper) { |
| 35 | + $mappers = $this->getMappers(); |
| 36 | + |
| 37 | + foreach ($mappers as $mapper) { |
| 38 | + /** @phpstan-ignore argument.type */ |
20 | 39 | $concise->register($mapper); |
21 | 40 | } |
22 | 41 | }); |
23 | 42 | } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return array<class-string<\Articulate\Concise\Contracts\Mapper<*>>> |
| 46 | + */ |
| 47 | + private function getMappers(): array |
| 48 | + { |
| 49 | + return array_merge( |
| 50 | + $this->discoveredMappers(), |
| 51 | + $this->mappers |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the discovered mappers for the application. |
| 57 | + * |
| 58 | + * @return array<class-string<\Articulate\Concise\Contracts\Mapper<*>>> |
| 59 | + */ |
| 60 | + protected function discoveredMappers(): array |
| 61 | + { |
| 62 | + return $this->shouldDiscoverMappers() |
| 63 | + ? $this->discoverMappers() |
| 64 | + : []; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Determine if mappers should be automatically discovered. |
| 69 | + * |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | + public function shouldDiscoverMappers(): bool |
| 73 | + { |
| 74 | + return get_class($this) === __CLASS__ && static::$shouldDiscoverMappers === true; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Discover the mappers for the application. |
| 79 | + * |
| 80 | + * @return array<class-string<\Articulate\Concise\Contracts\Mapper<*>>> |
| 81 | + */ |
| 82 | + public function discoverMappers(): array |
| 83 | + { |
| 84 | + return (new Collection($this->discoverMappersWithin())) |
| 85 | + /** @phpstan-ignore argument.type */ |
| 86 | + ->flatMap(function (string $directory) { |
| 87 | + return glob($directory, GLOB_ONLYDIR); |
| 88 | + }) |
| 89 | + ->reject(function (string $directory) { |
| 90 | + return ! is_dir($directory); |
| 91 | + }) |
| 92 | + ->reduce(function ($discovered, $directory) { |
| 93 | + return array_merge_recursive( |
| 94 | + $discovered, |
| 95 | + DiscoverMappers::within($directory, $this->mapperDiscoveryBasePath()) |
| 96 | + ); |
| 97 | + }, []); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the directories that should be used to discover mappers. |
| 102 | + * |
| 103 | + * @return array<string> |
| 104 | + */ |
| 105 | + protected function discoverMappersWithin(): array |
| 106 | + { |
| 107 | + return static::$mapperDiscoveryPaths ?? [ |
| 108 | + $this->app->path('Mappers\Components'), |
| 109 | + $this->app->path('Mappers\Entities'), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Add the given mapper discovery paths to the application's mapper discovery paths. |
| 115 | + * |
| 116 | + * @param string|array<string> $paths |
| 117 | + * |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + public static function addMapperDiscoveryPaths(array|string $paths): void |
| 121 | + { |
| 122 | + static::$mapperDiscoveryPaths = array_values(array_unique( |
| 123 | + array_merge(static::$mapperDiscoveryPaths, Arr::wrap($paths)) |
| 124 | + )); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Set the globally configured mapper discovery paths. |
| 129 | + * |
| 130 | + * @param array<string> $paths |
| 131 | + * |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + public static function setMapperDiscoveryPaths(array $paths): void |
| 135 | + { |
| 136 | + static::$mapperDiscoveryPaths = $paths; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Get the base path to be used during mapper discovery. |
| 141 | + * |
| 142 | + * @return string |
| 143 | + */ |
| 144 | + protected function mapperDiscoveryBasePath(): string |
| 145 | + { |
| 146 | + return base_path(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Disable mapper discovery for the application. |
| 151 | + * |
| 152 | + * @return void |
| 153 | + */ |
| 154 | + public static function disableMapperDiscovery(): void |
| 155 | + { |
| 156 | + static::$shouldDiscoverMappers = false; |
| 157 | + } |
24 | 158 | } |
0 commit comments