8000 Rewrite database path and storage differently · NativePHP/laravel@04470bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 04470bc

Browse files
committed
Rewrite database path and storage differently
1 parent 87aa725 commit 04470bc

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/Commands/MigrateCommand.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Native\Laravel\Commands;
4+
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Database\Migrations\Migrator;
7+
use Native\Laravel\NativeServiceProvider;
8+
use Illuminate\Database\Console\Migrations\MigrateCommand as BaseMigrateCommand;
9+
10+
class MigrateCommand extends BaseMigrateCommand
11+
{
12+
protected $description = 'Run the database migrations in the NativePHP development environment';
13+
14+
public function __construct(Migrator $migrator, Dispatcher $dispatcher)
15+
{
16+
$this->signature = 'native:'.$this->signature;
17+
18+
parent::__construct($migrator, $dispatcher);
19+
}
20+
21+
public function handle()
22+
{
23+
(new NativeServiceProvider($this->laravel))->rewriteDatabase();
24+
25+
parent::handle();
26+
}
27+
}

src/NativeServiceProvider.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Native\Laravel\Commands\LoadStartupConfigurationCommand;
7+
use Native\Laravel\Commands\MigrateCommand;
78
use Native\Laravel\Commands\MinifyApplicationCommand;
89
use Native\Laravel\Logging\LogWatcher;
910
use Spatie\LaravelPackageTools\Package;
@@ -16,6 +17,7 @@ public function configurePackage(Package $package): void
1617
$package
1718
->name('nativephp')
1819
->hasCommands([
20+
MigrateCommand::class,
1921
MinifyApplicationCommand::class,
2022
LoadStartupConfigurationCommand::class,
2123
])
@@ -28,6 +30,10 @@ public function packageRegistered()
2830
{
2931
$this->mergeConfigFrom($this->package->basePath('/../config/nativephp-internal.php'), 'nativephp-internal');
3032

33+
$this->app->singleton(MigrateCommand::class, function ($app) {
34+
return new MigrateCommand($app['migrator'], $app['events']);
35+
});
36+
3137
if (config('nativephp-internal.running')) {
3238
$this->configureApp();
3339
}
@@ -39,6 +45,20 @@ protected function configureApp()
3945
app(LogWatcher::class)->register();
4046
}
4147

48+
$this->rewriteStoragePath();
49+
50+
$this->rewriteDatabase();
51+
52+
config(['session.driver' => 'file']);
53+
config(['queue.default' => 'database']);
54+
}
55+
56+
protected function rewriteStoragePath()
57+
{
58+
if (config('app.debug')) {
59+
return;
60+
}
61+
4262
$oldStoragePath = $this->app->storagePath();
4363

4464
$this->app->useStoragePath(config('nativephp-internal.storage_path'));
@@ -52,18 +72,28 @@ protected function configureApp()
5272
config([$key => $newValue]);
5373
}
5474
}
75+
}
76+
77+
public function rewriteDatabase()
78+
{
79+
$databasePath = config('nativephp-internal.database_path');
80+
81+
if (config('app.debug')) {
82+
$databasePath = database_path('nativephp.sqlite');
83+
84+
if (! file_exists($databasePath)) {
85+
touch($databasePath);
86+
}
87+
}
5588

5689
config(['database.connections.nativephp' => [
5790
'driver' => 'sqlite',
5891
'url' => env('DATABASE_URL'),
59-
'database' => config('nativephp-internal.database_path'),
92+
'database' => $databasePath,
6093
'prefix' => '',
6194
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
6295
]]);
6396

6497
config(['database.default' => 'nativephp']);
65-
config(['session.driver' => 'file']);
66-
67-
config(['queue.default' => 'database']);
6898
}
6999
}

0 commit comments

Comments
 (0)
0