splash/bridge-builder
Laravel package for building and managing bridges between systems, providing tools to define connections, map data flows, and orchestrate integration logic in a structured, reusable way. Suitable for apps needing reliable cross-service communication and syncing.
Install the Package
composer require splash/bridge-builder
Verify PHP version (>=8.1) and extensions (curl, posix) are enabled.
Publish Configuration
php artisan vendor:publish --provider="Splash\BridgeBuilder\BridgeBuilderServiceProvider"
Edit config/bridge-builder.php to define:
phar_path: Directory to store generated PHARs (e.g., storage/app/phars).default_worker: Base class for workers (extends Splash\BridgeBuilder\Worker).Generate a Skeleton Worker
php artisan bridge:make Worker MyConnectorWorker
This creates:
app/Workers/MyConnectorWorker.php (Laravel service class).phar-skeleton/MyConnectorWorker/ (PHAR project structure).First Use Case: Queue Job Create a Laravel job to trigger the PHAR:
// app/Jobs/ProcessConnector.php
use Splash\BridgeBuilder\Facades\BridgeBuilder;
class ProcessConnector implements ShouldQueue {
public function handle() {
$result = BridgeBuilder::run('MyConnectorWorker', ['input' => 'data']);
// Handle $result (e.g., log, return to user)
}
}
Dispatch it:
ProcessConnector::dispatch();
Extract Logic Move connector logic (e.g., Stripe API calls) into a standalone class:
// app/Workers/StripePaymentWorker.php
namespace App\Workers;
use Splash\BridgeBuilder\Worker;
class StripePaymentWorker extends Worker {
public function handle($input) {
return \Stripe\Charge::create($input);
}
}
Generate PHAR
php artisan bridge:build StripePaymentWorker
Outputs storage/app/phars/stripe-payment-worker.phar.
Trigger from Laravel
// In a controller or job
$result = BridgeBuilder::run('stripe-payment-worker', [
'amount' => 1000,
'currency' => 'usd',
]);
Configure Laravel’s queue to execute PHARs:
// config/queue.php
'connections' => [
'phar' => [
'driver' => 'bridge',
'worker' => 'StripePaymentWorker',
'timeout' => 60,
],
],
Dispatch a job:
ProcessPayment::dispatch()->onConnection('phar');
BridgeBuilder::run('worker', json_encode(['key' => 'value']));
$output = json_decode(BridgeBuilder::run('worker', $input), true);
Worker::getContainer():
// In PHAR worker
$this->getContainer()->make('App\Services\Logger');
bootstrap.php):
$http = $this->getContainer()->make('Illuminate\Http\Client\PendingRequest');
$response = $http->get('https://api.example.com')->json();
try {
BridgeBuilder::run('worker', $input);
} catch (\Splash\BridgeBuilder\Exceptions\WorkerFailed $e) {
Log::error('Worker failed', ['error' => $e->getOutput()]);
}
throw new \RuntimeException(json_encode(['error' => 'Invalid input']));
Extend PHAR’s bootstrap.php to include Laravel facades:
// phar-skeleton/StripePaymentWorker/bootstrap.php
$container->singleton('App', function () {
return new \Illuminate\Foundation\Application();
});
Now use facades in the worker:
// In PHAR worker
Log::info('Processing payment...');
Load workers dynamically based on config:
// config/bridge-builder.php
'workers' => [
'stripe' => 'StripePaymentWorker',
'paypal' => 'PaypalPaymentWorker',
],
Run dynamically:
$workerName = config('bridge-builder.workers.default');
BridgeBuilder::run($workerName, $input);
Generate a PHAR with a CLI entry point:
// In Worker class
public function handle($input) {
if ($this->isCli()) {
// CLI-specific logic
}
return $this->process($input);
}
Run via:
php storage/app/phars/stripe-payment-worker.phar --input='{"amount":100}'
PHAR Execution Permissions
phar extension not enabled or Permission denied.phar.readonly=0 in php.ini and set executable permissions:
chmod +x storage/app/phars/*.phar
Dependency Conflicts
composer.json in the PHAR project to lock versions:
{
"require": {
"stripe/stripe-php": "^8.0",
"illuminate/support": "9.0.*"
}
}
State Management
// In PHAR worker
$cache = $this->getContainer()->make('Illuminate\Cache\Repository');
$value = $cache->get('key');
Debugging Challenges
// In Worker class
public function handle($input) {
file_put_contents(
storage_path('logs/phar-debug.log'),
print_r($input, true),
FILE_APPEND
);
}
stderr redirection in bridge:build config.PHAR Size Limits
Enable PHAR Debugging
Add to config/bridge-builder.php:
'debug' => env('BRIDGE_DEBUG', false),
This logs PHAR execution details to storage/logs/bridge-builder.log.
Inspect PHAR Contents List files in the PHAR:
phar -l storage/app/phars/worker.phar
Extract for testing:
phar -x worker.phar
Test Locally Run PHARs directly:
php storage/app/phars/worker.phar --input='{"test":123}'
Custom Worker Classes
Extend Splash\BridgeBuilder\Worker to add shared logic:
namespace App\Workers;
use Splash\BridgeBuilder\Worker;
class BaseWorker extends Worker {
protected function log($message) {
$this->getContainer()->make('App\Services\Logger')->info($message);
}
}
PHAR Pre/Post Hooks
Add logic before/after PHAR execution in app/Providers/BridgeBuilderServiceProvider.php:
public function boot() {
BridgeBuilder::preRun(function ($worker, $input) {
Log::debug("Running {$worker} with input: " . json_encode($input));
});
BridgeBuilder::postRun(function ($worker, $output, $exception) {
if ($exception) {
Alert::send("PHAR {$worker
How can I help you explore Laravel packages today?