Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^8.2",
"illuminate/contracts": "^11.0|^12.0",
"illuminate/support": "^11.0|^12.0",
"nativephp/mobile": "^3.0"
"nativephp/mobile": "^3.0|^4.0"
},
"require-dev": {
"larastan/larastan": "^3.0",
Expand Down
2 changes: 1 addition & 1 deletion configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private function rewriteComposerJson(
'php' => '^8.2',
'illuminate/contracts' => '^11.0|^12.0',
'illuminate/support' => '^11.0|^12.0',
'nativephp/mobile' => '^3.0',
'nativephp/mobile' => '^3.0|^4.0',
],
'require-dev' => [
'larastan/larastan' => '^3.0',
Expand Down
45 changes: 30 additions & 15 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function manifest(): array

public function isAvailable(): bool
{
if (! $this->app->bound('nativephp.mobile.bridge')) {
return false;
}
if ($this->app->bound('nativephp.mobile.bridge')) {
$bridge = $this->app->make('nativephp.mobile.bridge');

$bridge = $this->app->make('nativephp.mobile.bridge');
return is_object($bridge) && is_callable([$bridge, 'call']);
}

return is_object($bridge) && is_callable([$bridge, 'call']);
return function_exists('nativephp_call');
}

/**
Expand All @@ -54,22 +54,37 @@ public function isAvailable(): bool
*/
private function callBridge(string $function, array $payload): array
{
$bridge = $this->app->make('nativephp.mobile.bridge');
if ($this->app->bound('nativephp.mobile.bridge')) {
$bridge = $this->app->make('nativephp.mobile.bridge');

if (is_object($bridge) && is_callable([$bridge, 'call'])) {
/** @var mixed $response */
$response = $bridge->call($function, $payload);

return is_array($response) ? $response : ['value' => $response];
}
}

if (! is_object($bridge) || ! is_callable([$bridge, 'call'])) {
if (! function_exists('nativephp_call')) {
throw new RuntimeException('The NativePHP mobile bridge is not available for {{ vendor }}/{{ package }}.');
}

/** @var mixed $response */
$response = $bridge->call($function, $payload);
$response = nativephp_call($function, json_encode($payload, JSON_THROW_ON_ERROR));

/** @var mixed $decoded */
$decoded = json_decode((string) $response, true);

if (! is_array($decoded)) {
return ['value' => $decoded];
}

if (array_key_exists('data', $decoded)) {
/** @var mixed $data */
$data = $decoded['data'];

if (is_array($response)) {
/** @var array<string, mixed> $response */
return $response;
return is_array($data) ? $data : ['value' => $data];
}

return [
'value' => $response,
];
return $decoded;
}
}
55 changes: 46 additions & 9 deletions stubs/plugin.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare(strict_types=1);
namespace {{ namespace }};

use Illuminate\Contracts\Foundation\Application;
use RuntimeException;
use {{ namespace }}\Contracts\{{ plugin }}Contract;

final class Plugin implements {{ plugin }}Contract
Expand All @@ -16,23 +17,59 @@ final class Plugin implements {{ plugin }}Contract

public function example(array $payload = []): array
{
$bridge = $this->app->make('nativephp.mobile.bridge');
return $this->callBridge('{{ plugin }}.Example', $payload);
}

public function isAvailable(): bool
{
if ($this->app->bound('nativephp.mobile.bridge')) {
$bridge = $this->app->make('nativephp.mobile.bridge');

/** @var array<string, mixed> $response */
$response = $bridge->call('{{ plugin }}.Example', $payload);
return is_object($bridge) && is_callable([$bridge, 'call']);
}

return $response;
return function_exists('nativephp_call');
}

public function isAvailable(): bool
/**
* @param array<string, mixed> $payload
*
* @return array<string, mixed>
*/
private function callBridge(string $function, array $payload): array
{
if (! $this->app->bound('nativephp.mobile.bridge')) {
return false;
if ($this->app->bound('nativephp.mobile.bridge')) {
$bridge = $this->app->make('nativephp.mobile.bridge');

if (is_object($bridge) && is_callable([$bridge, 'call'])) {
/** @var mixed $response */
$response = $bridge->call($function, $payload);

return is_array($response) ? $response : ['value' => $response];
}
}

$bridge = $this->app->make('nativephp.mobile.bridge');
if (! function_exists('nativephp_call')) {
throw new RuntimeException('The NativePHP mobile bridge is not available for {{ vendor }}/{{ package }}.');
}

$response = nativephp_call($function, json_encode($payload, JSON_THROW_ON_ERROR));

/** @var mixed $decoded */
$decoded = json_decode((string) $response, true);

if (! is_array($decoded)) {
return ['value' => $decoded];
}

if (array_key_exists('data', $decoded)) {
/** @var mixed $data */
$data = $decoded['data'];

return is_array($data) ? $data : ['value' => $data];
}

return is_object($bridge) && is_callable([$bridge, 'call']);
return $decoded;
}

public function manifest(): array
Expand Down
Loading