Skip to content
9 changes: 9 additions & 0 deletions config/nativephp-internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
*/
'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'),

/**
* Configuration for the Zephpyr API.
*/
'zephpyr' => [
'host' => env('ZEPHPYR_HOST', 'https://zephpyr.com'),
'token' => env('ZEPHPYR_TOKEN'),
'key' => env('ZEPHPYR_KEY'),
],

/**
* The credentials to use Apples Notarization service.
*/
Expand Down
8 changes: 8 additions & 0 deletions config/nativephp.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'GITHUB_*',
'DO_SPACES_*',
'*_SECRET',
'ZEPHPYR_*',
'NATIVEPHP_UPDATER_PATH',
'NATIVEPHP_APPLE_ID',
'NATIVEPHP_APPLE_ID_PASS',
Expand All @@ -60,6 +61,8 @@
* You may use glob / wildcard patterns here.
*/
'cleanup_exclude_files' => [
'build',
'temp',
'content',
'node_modules',
'*/tests',
Expand Down Expand Up @@ -136,4 +139,9 @@
'postbuild' => [
// 'rm -rf public/build',
],

/**
* Custom PHP binary path.
*/
'binary_path' => env('NATIVEPHP_PHP_BINARY_PATH', null),
];
7 changes: 7 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Native\Laravel;

use Native\Laravel\Client\Client;
use Phar;

class App
{
Expand Down Expand Up @@ -63,6 +64,12 @@ public function clearRecentDocuments(): void
$this->client->delete('app/recent-documents');
}

public function isRunningBundled(): bool
{
return Phar::running() !== '';

}

public function openAtLogin(?bool $open = null): bool
{
if ($open === null) {
Expand Down
7 changes: 4 additions & 3 deletions src/ChildProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function start(
* @param string|string[] $cmd
* @return $this
*/
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self
{
$cmd = is_array($cmd) ? array_values($cmd) : [$cmd];

Expand All @@ -90,6 +90,7 @@ public function php(string|array $cmd, string $alias, ?array $env = null, ?bool
'cwd' => base_path(),
'env' => $env,
'persistent' => $persistent,
'iniSettings' => $iniSettings,
])->json();

return $this->fromRuntimeProcess($process);
Expand All @@ -99,13 +100,13 @@ public function php(string|array $cmd, string $alias, ?array $env = null, ?bool
* @param string|string[] $cmd
* @return $this
*/
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self
{
$cmd = is_array($cmd) ? array_values($cmd) : [$cmd];

$cmd = ['artisan', ...$cmd];

return $this->php($cmd, $alias, env: $env, persistent: $persistent);
return $this->php($cmd, $alias, env: $env, persistent: $persistent, iniSettings: $iniSettings);
}

public function stop(?string $alias = null): void
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/ChildProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function start(
bool $persistent = false
): self;

public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self;
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self;

public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self;
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self;

public function stop(?string $alias = null): void;

Expand Down
22 changes: 22 additions & 0 deletions src/Events/ChildProcess/StartupError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Native\Laravel\Events\ChildProcess;

use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class StartupError implements ShouldBroadcastNow
{
use Dispatchable, SerializesModels;

public function __construct(public string $alias, public string $error) {}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
1 change: 1 addition & 0 deletions src/Facades/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @method static void addRecentDocument(string $path)
* @method static array recentDocuments()
* @method static void clearRecentDocuments()
* @method static bool isRunningBundled()
* @method static bool openAtLogin(?bool $open = null)
*/
class App extends Facade
Expand Down
8 changes: 6 additions & 2 deletions src/Fakes/ChildProcessFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ public function php(
array|string $cmd,
string $alias,
?array $env = null,
?bool $persistent = false
?bool $persistent = false,
?array $iniSettings = null
): self {
$this->phps[] = [
'cmd' => $cmd,
'alias' => $alias,
'env' => $env,
'persistent' => $persistent,
'iniSettings' => $iniSettings,
];

return $this;
Expand All @@ -93,13 +95,15 @@ public function artisan(
array|string $cmd,
string $alias,
?array $env = null,
?bool $persistent = false
?bool $persistent = false,
?array $iniSettings = null
): self {
$this->artisans[] = [
'cmd' => $cmd,
'alias' => $alias,
'env' => $env,
'persistent' => $persistent,
'iniSettings' => $iniSettings,
];

return $this;
Expand Down
1 change: 1 addition & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ 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');

Expand Down
12 changes: 6 additions & 6 deletions src/QueueWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ public function up(string|QueueConfig $config): void
throw new \InvalidArgumentException("Invalid queue configuration alias [$config]");
}

$this->childProcess->php(
$this->childProcess->artisan(
[
'-d',
"memory_limit={$config->memoryLimit}M",
'artisan',
'queue:work',
"--name={$config->alias}",
'--queue='.implode(',', $config->queuesToConsume),
"--memory={$config->memoryLimit}",
"--timeout={$config->timeout}",
],
$config->alias,
'queue_'.$config->alias,
persistent: true,
iniSettings: [
'memory_limit' => "{$config->memoryLimit}M",
]
);
}

public function down(string $alias): void
{
$this->childProcess->stop($alias);
$this->childProcess->stop('queue_'.$alias);
}
}
12 changes: 6 additions & 6 deletions tests/Fakes/FakeChildProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@
$fake->php('cmdA', 'aliasA', ['envA'], true);
$fake->php('cmdB', 'aliasB', ['envB'], false);

$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasA' &&
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasA' &&
$cmd === 'cmdA' &&
$env === ['envA'] &&
$persistent === true);

$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasB' &&
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasB' &&
$cmd === 'cmdB' &&
$env === ['envB'] &&
$persistent === false);

try {
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasC');
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasC');
} catch (AssertionFailedError) {
return;
}
Expand All @@ -107,18 +107,18 @@
$fake->artisan('cmdA', 'aliasA', ['envA'], true);
$fake->artisan('cmdB', 'aliasB', ['envB'], false);

$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasA' &&
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasA' &&
$cmd === 'cmdA' &&
$env === ['envA'] &&
$persistent === true);

$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasB' &&
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasB' &&
$cmd === 'cmdB' &&
$env === ['envB'] &&
$persistent === false);

try {
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasC');
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasC');
} catch (AssertionFailedError) {
return;
}
Expand Down
20 changes: 12 additions & 8 deletions tests/QueueWorker/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@

it('hits the child process with relevant queue config to spin up a new queue worker', function () {
ChildProcess::fake();
$config = new QueueConfig('some_worker', ['default'], 128, 61);

$workerName = 'some_worker';

$config = new QueueConfig($workerName, ['default'], 128, 61);

QueueWorker::up($config);

ChildProcess::assertPhp(function (array $cmd, string $alias, $env, $persistent) {
ChildProcess::assertArtisan(function (array $cmd, string $alias, $env, $persistent, $iniSettings) use ($workerName) {
expect($cmd)->toBe([
'-d',
'memory_limit=128M',
'artisan',
'queue:work',
"--name={$alias}",
"--name={$workerName}",
'--queue=default',
'--memory=128',
'--timeout=61',
]);

expect($alias)->toBe('some_worker');
expect($iniSettings)->toBe([
'memory_limit' => '128M',
]);

expect($alias)->toBe('queue_some_worker');
expect($env)->toBeNull();
expect($persistent)->toBeTrue();

Expand All @@ -35,5 +39,5 @@

QueueWorker::down('some_worker');

ChildProcess::assertStop('some_worker');
ChildProcess::assertStop('queue_some_worker');
});
Loading