-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathOAuthInstaller.php
More file actions
175 lines (149 loc) · 5.86 KB
/
OAuthInstaller.php
File metadata and controls
175 lines (149 loc) · 5.86 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Tempest\Auth\Installer;
use Tempest\Auth\OAuth\SupportedOAuthProvider;
use Tempest\Core\PublishesFiles;
use Tempest\Process\ProcessExecutor;
use Tempest\Support\Filesystem\Exceptions\PathWasNotFound;
use Tempest\Support\Filesystem\Exceptions\PathWasNotReadable;
use Tempest\Support\Str\ImmutableString;
use function Tempest\root_path;
use function Tempest\src_path;
use function Tempest\Support\arr;
use function Tempest\Support\Filesystem\read_file;
use function Tempest\Support\Namespace\to_fqcn;
use function Tempest\Support\str;
final class OAuthInstaller
{
use PublishesFiles;
public function __construct(
private readonly ProcessExecutor $processExecutor,
) {}
public function install(): void
{
$providers = $this->getProviders();
if (count($providers) === 0) {
return;
}
$this->publishStubs(...$providers);
if ($this->confirm('Would you like to add the OAuth config variables to your .env file?', default: true)) {
$this->updateEnvFile(...$providers);
}
if ($this->confirm('Install composer dependencies?', default: true)) {
$this->installComposerDependencies(...$providers);
}
$this->console->instructions([
sprintf('<strong>The selected OAuth %s installed in your project</strong>', count($providers) > 1 ? 'providers are' : 'provider is'),
'',
'Next steps:',
'1. Update the .env file with your OAuth credentials',
'2. Implement the OAuth controller callback method',
'3. Review and customize the published files if needed',
'',
'<strong>Published files</strong>',
...arr($this->publishedFiles)->map(fn (string $file) => '<style="fg-green">→</style> ' . $file),
]);
}
/**
* @return list<SupportedOAuthProvider>
*/
private function getProviders(): array
{
return $this->ask(
question: 'Please choose an OAuth provider',
options: SupportedOAuthProvider::cases(),
multiple: true,
);
}
private function publishStubs(SupportedOAuthProvider ...$providers): void
{
foreach ($providers as $provider) {
$this->publishController($provider);
$this->publishConfig($provider);
$this->publishImports();
}
}
private function publishConfig(SupportedOAuthProvider $provider): void
{
$name = strtolower($provider->name);
$source = __DIR__ . "/../Installer/oauth/{$name}.config.stub.php";
$this->publish(
source: $source,
destination: src_path("Authentication/OAuth/{$name}.config.php"),
);
}
private function publishController(SupportedOAuthProvider $provider): void
{
$fileName = str($provider->getName())
->replace('Provider', '')
->append('Controller.php')
->toString();
$this->publish(
source: __DIR__ . '/oauth/OAuthControllerStub.php',
destination: src_path("Authentication/OAuth/{$fileName}"),
callback: function (string $source, string $destination) use ($provider) {
$providerFqcn = $provider::class;
$name = strtolower($provider->getName());
$userModelFqcn = to_fqcn(src_path('Authentication/User.php'), root: root_path());
$this->update(
path: $destination,
callback: fn (ImmutableString $contents) => $contents->replace(
search: [
"'tag_name'",
'redirect-route',
'callback-route',
"'user-model-fqcn'",
],
replace: [
"\\{$providerFqcn}::{$provider->name}",
"/auth/{$name}",
"/auth/{$name}/callback",
"\\{$userModelFqcn}::class",
],
),
);
},
);
}
private function installComposerDependencies(SupportedOAuthProvider ...$providers): void
{
$packages = arr($providers)
->map(fn (SupportedOAuthProvider $provider) => $provider->composerPackage())
->filter();
if ($packages->isNotEmpty()) {
$this->processExecutor->run("composer require {$packages->implode(' ')}");
}
}
private function updateEnvFile(SupportedOAuthProvider ...$providers): void
{
arr($providers)
->map(fn (SupportedOAuthProvider $provider) => $this->extractSettings($provider))
->filter()
->flatten()
->each(function (string $setting) {
foreach (['.env', '.env.example'] as $envFile) {
$this->update(
path: root_path($envFile),
callback: static fn (ImmutableString $contents): ImmutableString => $contents->contains($setting)
? $contents
: $contents->append(PHP_EOL, "{$setting}="),
ignoreNonExisting: true,
);
}
});
}
private function extractSettings(SupportedOAuthProvider $provider): array
{
$name = strtolower($provider->name);
$configPath = __DIR__ . "/../Installer/oauth/{$name}.config.stub.php";
try {
return str(read_file($configPath))
->matchAll("/env\('(OAUTH_[^']*)'/", matches: 1)
->map(fn (array $matches) => $matches[1] ?? null)
->filter()
->toArray();
} catch (PathWasNotFound|PathWasNotReadable) {
return [];
}
}
}