|
4 | 4 |
|
5 | 5 | namespace Internal\DLoad\Module\Repository; |
6 | 6 |
|
7 | | -use Internal\DLoad\Module\Common\Config\Embed\Repository; |
8 | | -use Internal\DLoad\Module\Repository\Internal\GitHub\Factory as GithubFactory; |
| 7 | +use Internal\DLoad\Module\Common\Config\Embed\Repository as RepositoryConfig; |
9 | 8 |
|
10 | 9 | /** |
11 | 10 | * Factory service for creating repository instances from configuration. |
12 | 11 | * |
13 | | - * Handles the creation of appropriate repository implementations based on |
14 | | - * the repository type specified in the configuration. |
| 12 | + * Uses registered repository factories to create appropriate repository |
| 13 | + * implementations based on the repository type. |
15 | 14 | * |
16 | 15 | * ```php |
17 | 16 | * // Get the RepositoryProvider service |
|
26 | 25 | */ |
27 | 26 | final class RepositoryProvider |
28 | 27 | { |
| 28 | + /** @var RepositoryFactory[] $factories */ |
| 29 | + private array $factories = []; |
| 30 | + |
29 | 31 | /** |
30 | | - * @param GithubFactory $githubFactory Factory for creating GitHub repository instances |
| 32 | + * Adds a repository factory to the provider. |
31 | 33 | */ |
32 | | - public function __construct( |
33 | | - private readonly GithubFactory $githubFactory, |
34 | | - ) {} |
| 34 | + public function addRepositoryFactory(RepositoryFactory $factory): self |
| 35 | + { |
| 36 | + $this->factories[] = $factory; |
| 37 | + return $this; |
| 38 | + } |
35 | 39 |
|
36 | 40 | /** |
37 | 41 | * Creates a repository instance based on the provided configuration. |
38 | 42 | * |
39 | | - * @param Repository $config Repository configuration |
40 | | - * @return RepositoryInterface Created repository instance |
41 | | - * @throws \RuntimeException When an unknown repository type is specified |
| 43 | + * @param RepositoryConfig $config Repository configuration |
| 44 | + * @return Repository Created repository instance |
| 45 | + * @throws \RuntimeException When no factory supports the repository type |
42 | 46 | */ |
43 | | - public function getByConfig(Repository $config): RepositoryInterface |
| 47 | + public function getByConfig(RepositoryConfig $config): Repository |
44 | 48 | { |
45 | | - return match (\strtolower($config->type)) { |
46 | | - 'github' => $this->githubFactory->create($config->uri), |
47 | | - default => throw new \RuntimeException("Unknown repository type `$config->type`."), |
48 | | - }; |
| 49 | + foreach ($this->factories as $factory) { |
| 50 | + if ($factory->supports($config)) { |
| 51 | + return $factory->create($config); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + throw new \RuntimeException("No factory found for repository type `$config->type`."); |
49 | 56 | } |
50 | 57 | } |
0 commit comments