NativePHP has come a long way—its latest version is 2.1 (as of this writing). But if you’ve ever tried to tweak your app, you know the frustration: every update can overwrite your custom changes, leaving you to repeat tedious steps over and over.
What if there was a better way? A way to automate repetitive tasks, safeguard your tweaks, and wield ultimate control over your app build?
Enter: the NativePHP child command.
Step 1: Forge Your Command
php artisan make:command NativeRunChild
Step 2: Empower Your Command
Now comes the fun part: you teach your command what to do. Anything repetitive—copying files, patching updates, preparing resources—can be automated.
Here’s a simple example that copies files before running your app (adapt it for whatever heroic tasks you need):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
/**
* Class NativeRunChild
* @package App\Console\Commands
*/
class NativeRunChild extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'native:run-child';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Unleashing the Power of NativePHP Child Commands';
/**
* Execute the console command.
*/
public function handle()
{
// Run notification sound tasks
$result = $this->customNotificationSound();
// Only proceed if previous tasks succeeded
if ($result === self::SUCCESS) {
$this->components->task('Running NativePHP main runtime', function () {
$exitCode = $this->call('native:run');
return $exitCode === 0;
});
}
return $result;
}
/**
* @return int
*/
public function customNotificationSound(): int
{
// Android notification sound
$androidSource = public_path('sounds/notification.mp3');
$androidDestinationDir = base_path('nativephp/android/app/src/main/res/raw');
$androidDestinationFile = $androidDestinationDir . '/notification.mp3';
// Ensure Android directory exists
$this->components->task('Ensuring Android directories exist', function () use ($androidDestinationDir) {
if (!File::exists($androidDestinationDir)) {
File::makeDirectory($androidDestinationDir, 0755, true);
}
return true;
});
// Copy Android sound
$this->components->task('Copying Android notification sound', function () use ($androidSource, $androidDestinationFile) {
if (!File::exists($androidSource)) {
$this->error("Android source sound missing: $androidSource");
return false;
}
if (!File::exists($androidDestinationFile)) {
File::copy($androidSource, $androidDestinationFile);
}
return true;
});
// Ios notification sound
$iosSource = public_path('sounds/notification.caf');
$iosDestinationDir = base_path('nativephp/ios/NativePHP');
$iosDestinationFile = $iosDestinationDir . '/notification.caf';
// Ensure iOS directory exists
$this->components->task('Ensuring iOS directories exist', function () use ($iosDestinationDir) {
if (!File::exists($iosDestinationDir)) {
File::makeDirectory($iosDestinationDir, 0755, true);
}
return true;
});
// Copy iOS sound
$this->components->task('Copying iOS notification sound', function () use ($iosSource, $iosDestinationFile) {
if (!File::exists($iosSource)) {
$this->error("iOS source sound missing: $iosSource");
return false;
}
if (!File::exists($iosDestinationFile)) {
File::copy($iosSource, $iosDestinationFile);
}
return true;
});
return self::SUCCESS;
}
}
A Word of Caution
With great power comes great responsibility. Any change you make in the child command or the app folders can potentially break your app.
To restore the original functionality and revert any changes made by this command, run:
php artisan native:install --force