-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGeneratorManager.php
75 lines (60 loc) · 1.89 KB
/
GeneratorManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/** @noinspection PhpUnusedPrivateMethodInspection */
declare(strict_types=1);
/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/
namespace App;
use App\Contracts\GeneratorContract;
use App\Exceptions\InvalidArgumentException;
use App\Generators\OpenAIChatGenerator;
use App\Generators\OpenAIGenerator;
use Illuminate\Support\Manager;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Tappable;
/**
* @mixin \App\Contracts\GeneratorContract
*/
final class GeneratorManager extends Manager
{
use Conditionable;
use Tappable;
public function getDefaultDriver(): string
{
return $this->config->get('ai-commit.generator');
}
/**
* {@inheritDoc}
*
* @noinspection MissingParentCallInspection
* @noinspection PhpMissingParentCallCommonInspection
*/
protected function createDriver($driver): GeneratorContract
{
if (isset($this->customCreators[$driver])) {
return $this->callCustomCreator($driver);
}
$config = $this->config->get("ai-commit.generators.$driver");
$studlyName = Str::studly($config['driver'] ?? $driver);
if (method_exists($this, $method = "create{$studlyName}Driver")) {
return $this->{$method}($config);
}
if (class_exists($class = "App\\Generators\\{$studlyName}Generator")) {
return new $class($config);
}
throw new InvalidArgumentException("Driver [$driver] not supported.");
}
private function createOpenAIDriver(array $config): OpenAIGenerator
{
return new OpenAIGenerator($config);
}
private function createOpenAIChatDriver(array $config): OpenAIChatGenerator
{
return new OpenAIChatGenerator($config);
}
}