-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathNativeServiceProvider.php
243 lines (201 loc) · 7.97 KB
/
NativeServiceProvider.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace Native\Laravel;
use Illuminate\Console\Application;
use Illuminate\Foundation\Application as Foundation;
use Illuminate\Foundation\Http\Kernel;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Native\Laravel\ChildProcess as ChildProcessImplementation;
use Native\Laravel\Commands\DebugCommand;
use Native\Laravel\Commands\FreshCommand;
use Native\Laravel\Commands\LoadPHPConfigurationCommand;
use Native\Laravel\Commands\LoadStartupConfigurationCommand;
use Native\Laravel\Commands\MigrateCommand;
use Native\Laravel\Commands\SeedDatabaseCommand;
use Native\Laravel\Contracts\ChildProcess as ChildProcessContract;
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
use Native\Laravel\Contracts\PowerMonitor as PowerMonitorContract;
use Native\Laravel\Contracts\QueueWorker as QueueWorkerContract;
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
use Native\Laravel\DTOs\QueueConfig;
use Native\Laravel\Events\EventWatcher;
use Native\Laravel\Exceptions\Handler;
use Native\Laravel\GlobalShortcut as GlobalShortcutImplementation;
use Native\Laravel\Http\Middleware\PreventRegularBrowserAccess;
use Native\Laravel\Logging\LogWatcher;
use Native\Laravel\PowerMonitor as PowerMonitorImplementation;
use Native\Laravel\Windows\WindowManager as WindowManagerImplementation;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class NativeServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('nativephp')
->hasCommands([
DebugCommand::class,
FreshCommand::class,
MigrateCommand::class,
SeedDatabaseCommand::class,
])
->hasConfigFile()
->hasRoute('api')
->publishesServiceProvider('NativeAppServiceProvider');
}
public function packageRegistered()
{
$this->mergeConfigFrom($this->package->basePath('/../config/nativephp-internal.php'), 'nativephp-internal');
$this->app->singleton(FreshCommand::class, function ($app) {
return new FreshCommand($app['migrator']);
});
$this->app->singleton(MigrateCommand::class, function ($app) {
return new MigrateCommand($app['migrator'], $app['events']);
});
$this->app->bind(WindowManagerContract::class, function (Foundation $app) {
return $app->make(WindowManagerImplementation::class);
});
$this->app->bind(ChildProcessContract::class, function (Foundation $app) {
return $app->make(ChildProcessImplementation::class);
});
$this->app->bind(GlobalShortcutContract::class, function (Foundation $app) {
return $app->make(GlobalShortcutImplementation::class);
});
$this->app->bind(PowerMonitorContract::class, function (Foundation $app) {
return $app->make(PowerMonitorImplementation::class);
});
$this->app->bind(QueueWorkerContract::class, function (Foundation $app) {
return $app->make(QueueWorker::class);
});
if (config('nativephp-internal.running')) {
$this->app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
Handler::class
);
// Automatically prevent browser access
$this->app->make(Kernel::class)->pushMiddleware(
PreventRegularBrowserAccess::class,
);
Application::starting(function ($app) {
$app->resolveCommands([
LoadStartupConfigurationCommand::class,
LoadPHPConfigurationCommand::class,
MigrateCommand::class,
]);
});
$this->configureApp();
}
}
public function bootingPackage()
{
if (config('nativephp-internal.running')) {
$this->rewriteDatabase();
}
}
protected function configureApp()
{
if (config('app.debug')) {
app(LogWatcher::class)->register();
}
app(EventWatcher::class)->register();
$this->rewriteStoragePath();
$this->configureDisks();
config(['session.driver' => 'file']);
config(['queue.default' => 'database']);
// XXX: This logic may need to change when we ditch the internal web server
if (! $this->app->runningInConsole()) {
$this->fireUpQueueWorkers();
}
}
protected function rewriteStoragePath()
{
if (config('app.debug')) {
return;
}
$oldStoragePath = $this->app->storagePath();
$this->app->useStoragePath(config('nativephp-internal.storage_path'));
// Patch all config values that contain the old storage path
$config = Arr::dot(config()->all());
foreach ($config as $key => $value) {
if (is_string($value) && str_contains($value, $oldStoragePath)) {
$newValue = str_replace($oldStoragePath, config('nativephp-internal.storage_path'), $value);
config([$key => $newValue]);
}
}
}
public function rewriteDatabase()
{
$databasePath = config('nativephp-internal.database_path');
// Automatically create the database in development mode
if (config('app.debug')) {
$databasePath = database_path('nativephp.sqlite');
if (! file_exists($databasePath)) {
touch($databasePath);
Artisan::call('native:migrate');
}
}
config([
'database.connections.nativephp' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => $databasePath,
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
]);
config(['database.default' => 'nativephp']);
config(['queue.failed.database' => 'nativephp']);
config(['queue.batching.database' => 'nativephp']);
config(['queue.connections.database.connection' => 'nativephp']);
if (file_exists($databasePath)) {
DB::statement('PRAGMA journal_mode=WAL;');
DB::statement('PRAGMA busy_timeout=5000;');
}
}
public function removeDatabase()
{
$databasePath = config('nativephp-internal.database_path');
if (config('app.debug')) {
$databasePath = database_path('nativephp.sqlite');
}
@unlink($databasePath);
@unlink($databasePath.'-shm');
@unlink($databasePath.'-wal');
}
protected function configureDisks(): void
{
$disks = [
'NATIVEPHP_USER_HOME_PATH' => 'user_home',
'NATIVEPHP_APP_DATA_PATH' => 'app_data',
'NATIVEPHP_USER_DATA_PATH' => 'user_data',
'NATIVEPHP_DESKTOP_PATH' => 'desktop',
'NATIVEPHP_DOCUMENTS_PATH' => 'documents',
'NATIVEPHP_DOWNLOADS_PATH' => 'downloads',
'NATIVEPHP_MUSIC_PATH' => 'music',
'NATIVEPHP_PICTURES_PATH' => 'pictures',
'NATIVEPHP_VIDEOS_PATH' => 'videos',
'NATIVEPHP_RECENT_PATH' => 'recent',
];
foreach ($disks as $env => $disk) {
if (! env($env)) {
continue;
}
config([
'filesystems.disks.'.$disk => [
'driver' => 'local',
'root' => env($env, ''),
'throw' => false,
'links' => 'skip',
],
]);
}
}
protected function fireUpQueueWorkers(): void
{
$queueConfigs = QueueConfig::fromConfigArray(config('nativephp.queue_workers'));
foreach ($queueConfigs as $queueConfig) {
$this->app->make(QueueWorkerContract::class)->up($queueConfig);
}
}
}