diff --git a/config/nativephp-internal.php b/config/nativephp-internal.php index 4210df92..84893661 100644 --- a/config/nativephp-internal.php +++ b/config/nativephp-internal.php @@ -1,6 +1,11 @@ env('NATIVEPHP_ENVIRONMENT', 'electron'), + /** * An internal flag to indicate if the app is running in the NativePHP * environment. This is used to determine if the app should use the diff --git a/src/App.php b/src/App.php index 15fba4e6..a1b6e1ec 100644 --- a/src/App.php +++ b/src/App.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class App { diff --git a/src/Client/Client.php b/src/Clients/Electron.php similarity index 89% rename from src/Client/Client.php rename to src/Clients/Electron.php index e444ea4a..36ff3182 100644 --- a/src/Client/Client.php +++ b/src/Clients/Electron.php @@ -1,12 +1,13 @@ request([ + 'invoke' => $resource, + ]); + } + + public function post(string $resource, array $data = []) + { + return $this->request([ + 'invoke' => $resource, + 'data' => $data, + ]); + } + + public function delete(string $resource, array $data = []) + { + return $this->request([ + 'invoke' => $resource, + 'data' => $data, + ]); + } + + protected function request($message) + { + // Connect to the socket, send the data and get a response and shutdown + $client = stream_socket_client('tcp://127.0.0.1:9000'); + + stream_socket_sendto($client, json_encode($message)); + + $response = base_convert(stream_socket_recvfrom($client, 1500000), 2, 10); + + stream_socket_shutdown($client, STREAM_SHUT_RDWR); + + return $response; + } +} diff --git a/src/Clipboard.php b/src/Clipboard.php index a6d73268..12117aa7 100644 --- a/src/Clipboard.php +++ b/src/Clipboard.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Clipboard { diff --git a/src/ContextMenu.php b/src/ContextMenu.php index e0e8fa72..36a20d65 100644 --- a/src/ContextMenu.php +++ b/src/ContextMenu.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; use Native\Laravel\Menu\Menu; class ContextMenu diff --git a/src/Contracts/Client.php b/src/Contracts/Client.php new file mode 100644 index 00000000..51f9963b --- /dev/null +++ b/src/Contracts/Client.php @@ -0,0 +1,12 @@ +registerNativeClient(); + if (config('app.debug')) { app(LogWatcher::class)->register(); } @@ -67,6 +72,16 @@ protected function configureApp() config(['queue.default' => 'database']); } + protected function registerNativeClient() + { + $this->app->singleton(Client::class, function ($app) { + return match ($app['config']->get('nativephp-internal.environment')) { + 'tauri' => new Tauri(), + 'electron' => new Electron(), + }; + }); + } + protected function rewriteStoragePath() { if (config('app.debug')) { diff --git a/src/Notification.php b/src/Notification.php index fd4d023a..319c2dee 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Notification { @@ -18,7 +18,7 @@ public function __construct(protected Client $client) public static function new() { - return new static(new Client()); + return new static(app(Client::class)); } public function title(string $title): self diff --git a/src/Process.php b/src/Process.php index 7a189f9b..0efc68f2 100644 --- a/src/Process.php +++ b/src/Process.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Process { diff --git a/src/ProgressBar.php b/src/ProgressBar.php index b4a67607..2ff1a242 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class ProgressBar { diff --git a/src/Screen.php b/src/Screen.php index b0f4e7f2..ab9dbddd 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Screen { diff --git a/src/Settings.php b/src/Settings.php index 0c7b6eec..bc27116e 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Settings { diff --git a/src/Shell.php b/src/Shell.php index 38dedca0..963b3ea6 100644 --- a/src/Shell.php +++ b/src/Shell.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; class Shell { diff --git a/src/System.php b/src/System.php index 65a2c0c4..cd0b1a79 100644 --- a/src/System.php +++ b/src/System.php @@ -2,7 +2,7 @@ namespace Native\Laravel; -use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\Client; use Native\Laravel\DataObjects\Printer; class System diff --git a/src/Windows/Window.php b/src/Windows/Window.php index b8f5926c..a70d8bd3 100644 --- a/src/Windows/Window.php +++ b/src/Windows/Window.php @@ -2,10 +2,10 @@ namespace Native\Laravel\Windows; -use Native\Laravel\Client\Client; use Native\Laravel\Concerns\HasDimensions; use Native\Laravel\Concerns\HasUrl; use Native\Laravel\Concerns\HasVibrancy; +use Native\Laravel\Contracts\Client; class Window { diff --git a/src/Windows/WindowManager.php b/src/Windows/WindowManager.php index 6216f74c..5fb0bd2f 100644 --- a/src/Windows/WindowManager.php +++ b/src/Windows/WindowManager.php @@ -2,8 +2,8 @@ namespace Native\Laravel\Windows; -use Native\Laravel\Client\Client; use Native\Laravel\Concerns\DetectsWindowId; +use Native\Laravel\Contracts\Client; class WindowManager {