NativePHP extends the capabilities of Laravel into the world of desktop application development. In this article, we explore how to interact with your system through shell commands and how to listen to power events like sleep, resume, or shutdown to make your applications smarter and more integrated with the desktop environment.
🧠 Why Use Shell Commands in NativePHP?
Shell commands open up a wide range of possibilities. Whether it's fetching system info, opening files, or triggering scripts, shell access gives your desktop app a bridge to the OS.
✅ Common Use Cases:
-
Executing OS-level commands (e.g.,
ls
,dir
,open
,start
) -
Automating updates or system configurations
-
Launching external programs
-
Running background scripts
🛠️ Executing Shell Commands in NativePHP
NativePHP makes it easy to run shell commands using Laravel's Process
facade.
📦 Example:
use Illuminate\Support\Facades\Process;
$process = Process::run('echo Hello from NativePHP');
echo $process->output(); // Output: Hello from NativePHP
You can run complex commands, chain them, or even check for errors:
$process = Process::run('mkdir temp_folder');
if ($process->successful()) {
logger('Folder created!');
} else {
logger('Failed: ' . $process->errorOutput());
}
🔌 Monitoring Power Events (Sleep, Resume, etc.)
One of the coolest features of NativePHP is that it can hook into power-related system events. This lets you create smarter desktop apps that adapt to the user's machine state.
🛏️ Listen to Sleep Events
use Native\Laravel\Facades\Power;
Power::onSleep(function () {
logger('System is going to sleep...');
});
☀️ Resume Events
Power::onResume(function () {
logger('System just woke up!');
});
🔋 Low Battery or Power Connected/Disconnected
While not all systems support all types of power events, NativePHP can be extended to listen to these if supported by the OS.
🧪 Real-World Examples
-
Secure Logout on Sleep:
Power::onSleep(function () { Auth::logout(); logger('User logged out on sleep'); });
-
Auto-Backup on Resume:
Power::onResume(function () { BackupService::run(); logger('Backup triggered on resume'); });
-
Custom Notification via Shell:
Process::run('notify-send "App resumed from sleep!"');
🛡️ Security Considerations
-
Always sanitize user input if you're using it in a command.
-
Limit shell access to only what’s necessary.
-
Avoid exposing commands that can modify the system without proper authorization.
Combining Laravel’s simplicity with the power of the underlying OS makes NativePHP an incredibly versatile tool. Shell command execution and power event listeners are just the beginning. With this integration, your Laravel desktop apps can behave more like native tools—aware, responsive, and deeply connected to the user’s machine.
0 Comments