hamzi/portflow
PortFlow connects serial hardware (thermal printers, barcode/RFID scanners, scales, IoT boards) to Laravel via a driver-based architecture. Parse raw bytes into typed events, queue routes, and printing workflows, with Web Serial API support for browser integration.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require hamzi/portflow:^0.7.0
php artisan vendor:publish --provider="Hamzi\PortFlow\PortFlowServiceProvider" --tag="portflow-config"
config/portflow.php) with new webhook driver and CLI tool settings.First Use Case:
use Hamzi\PortFlow\Facades\PortFlow;
// Configure webhook endpoint
$webhook = PortFlow::connect('webhook', [
'url' => 'https://your-app.com/api/device-webhook',
'secret' => 'your_shared_secret',
]);
// Simulate receiving data (e.g., from a hardware device)
$webhook->receive([
'device_id' => 'USB001',
'data' => 'BARCODE_123',
'timestamp' => now(),
]);
php artisan portflow:diagnose
Lists connected devices, driver statuses, and buffer stats.Where to Look First:
config/portflow.php (new webhook driver and cli tool settings).app/Drivers/ (custom drivers now support shouldPersistBuffer() for buffer optimizations).vendor/bin/portflow (interactive diagnostic commands).Webhook Integration (New in v0.7.0):
PortFlow::on('webhook.received', function ($payload) {
if (!hash_equals(config('portflow.webhook.secret'), $payload['signature'])) {
abort(403);
}
// Process payload (e.g., store in DB or trigger actions)
});
curl -X POST https://your-app.com/api/device-webhook \
-H "Content-Type: application/json" \
-d '{"device_id":"SENSOR_001","data":"temperature=25","signature":"..."}'
Diagnostic CLI Tool (New in v0.7.0):
php artisan portflow:diagnose --list-devices
php artisan portflow:diagnose --buffer-stats
php artisan portflow:diagnose --interactive
(Useful for debugging live systems without restarting.)Buffer Persistence Optimizations:
config/portflow.php:
'buffer' => [
'enabled' => true,
'persist_drivers' => ['escpos', 'barcode'], // Only persist for these drivers
'max_size' => 1024, // KB
],
shouldPersistBuffer() in your driver:
class CustomDriver extends AbstractDriver {
public function shouldPersistBuffer(): bool {
return $this->config['persist_buffer'] ?? false;
}
}
Cleaner Architecture:
PortFlowServiceProvider now registers the webhook driver and CLI tool by default. Override in AppServiceProvider if needed:
public function boot() {
$this->app->make('Hamzi\PortFlow\PortFlowServiceProvider')->registerWebhookDriver();
}
PortFlow::on('webhook.validated', fn($payload) => Log::info('Valid payload received'));
PortFlow::on('webhook.invalid', fn($payload) => Log::warning('Invalid payload rejected'));
Frontend-Backend Sync (Updated):
// Frontend: Listen for webhook-triggered events via Laravel Echo
Echo.channel('device-updates')
.listen('DeviceStatusUpdated', (e) => {
console.log('Printer status:', e.status);
});
PortFlow::on('webhook.received', function ($payload) {
dispatch(new ProcessWebhookJob($payload));
});
webhook tab (if using the telescope logging driver).PortFlow::mockDriver('webhook') to simulate incoming payloads:
PortFlow::mockDriver('webhook', [
'receive' => fn($payload) => $payload['data'] === 'TEST_123',
]);
webhook.secret in config/portflow.php periodically and use Laravel's signed helper for additional protection:
$signedPayload = request()->input('payload');
$decoded = base64_decode($signedPayload);
Webhook Driver Quirks:
secret in config/portflow.php matches the client's signature. Use hash_equals() to avoid timing attacks.Route::middleware(['throttle:10,1'])->post('/api/device-webhook', ...);
post_max_size. Stream data or chunk it:
$webhook->receiveChunked($data, 1024); // New in v0.7.0
Buffer Persistence:
php artisan portflow:diagnose --buffer-stats
Disable for high-frequency, low-latency drivers (e.g., real-time sensors).storage/app/portflow_buffers directory is writable and backed up.CLI Tool Limitations:
sudo if needed (not recommended for production):
sudo php artisan portflow:diagnose
/dev/ttyUSB0 vs. COM3) may differ. Use the --normalize-paths flag:
php artisan portflow:diagnose --normalize-paths
Breaking Changes in v0.7.0:
shouldPersistBuffer() if they support buffer persistence. Old drivers will default to false.data.received event is now split into webhook.received (raw) and webhook.validated (processed). Update your listeners.PortFlow::rawWrite() method is deprecated. Use $driver->write() directly.Diagnostic Tool False Positives:
Webhook Debugging:
PortFlow::setLogLevel(Log::DEBUG);
storage/logs/portflow.log for raw payloads and validation errors.{
"device_id": "TEST_DEVICE",
"data": "sample_data",
"signature": "sha256:your_shared_secret"
}
Buffer Issues:
php artisan portflow:diagnose --buffer-usage
How can I help you explore Laravel packages today?