Skip to content

Commit 0dc99d1

Browse files
committed
configure storage and database
1 parent a398717 commit 0dc99d1

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

config/native-php.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
return [
4+
'running' => env('NATIVE_PHP_RUNNING', false),
5+
6+
'storage_path' => env('NATIVE_PHP_STORAGE_PATH'),
7+
8+
'database_path' => env('NATIVE_PHP_DATABASE_PATH'),
9+
410
'secret' => env('NATIVE_PHP_SECRET'),
511

612
'api_url' => env('NATIVE_PHP_API_URL', 'http://localhost:4000/api/'),

src/NativeServiceProvider.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
namespace Native\Laravel;
44

5+
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
56
use Illuminate\Foundation\Console\ServeCommand;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Facades\Config;
9+
use Illuminate\Support\Facades\Storage;
610
use Spatie\LaravelPackageTools\Package;
711
use Spatie\LaravelPackageTools\PackageServiceProvider;
812

913
class NativeServiceProvider extends PackageServiceProvider
1014
{
15+
protected $passThrough = [
16+
'NATIVE_PHP_SECRET',
17+
'NATIVE_PHP_RUNNING',
18+
'NATIVE_PHP_STORAGE_PATH',
19+
'NATIVE_PHP_DATABASE_PATH',
20+
];
21+
1122
public function configurePackage(Package $package): void
1223
{
1324
$package
@@ -17,8 +28,41 @@ public function configurePackage(Package $package): void
1728
->publishesServiceProvider('NativeAppServiceProvider');
1829
}
1930

20-
public function bootingPackage()
31+
public function packageRegistered()
32+
{
33+
foreach ($this->passThrough as $env) {
34+
ServeCommand::$passthroughVariables[] = $env;
35+
}
36+
37+
if (config('native-php.running')) {
38+
$this->configureApp();
39+
}
40+
}
41+
42+
protected function configureApp()
2143
{
22-
ServeCommand::$passthroughVariables[] = 'NATIVE_PHP_SECRET';
44+
$oldStoragePath = $this->app->storagePath();
45+
46+
$this->app->useStoragePath(config('native-php.storage_path'));
47+
48+
// Patch all config values that contain the old storage path
49+
$config = Arr::dot(config()->all());
50+
51+
foreach ($config as $key => $value) {
52+
if (is_string($value) && str_contains($value, $oldStoragePath)) {
53+
$newValue = str_replace($oldStoragePath, config('native-php.storage_path'), $value);
54+
config([$key => $newValue]);
55+
}
56+
}
57+
58+
config(['database.connections.nativephp' => [
59+
'driver' => 'sqlite',
60+
'url' => env('DATABASE_URL'),
61+
'database' => config('native-php.database_path'),
62+
'prefix' => '',
63+
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
64+
]]);
65+
66+
config(['database.default' => 'nativephp']);
2367
}
2468
}

0 commit comments

Comments
 (0)