php-standard-library/interoperability
Lightweight PHP interoperability helpers for working with common standards and cross-package integrations. Provides small, reusable utilities to bridge libraries and normalize behavior without heavy dependencies.
composer require php-standard-library/interoperability
vendor/php-standard-library/interoperability/src/Contracts/vendor/php-standard-library/interoperability/src/Adapters/vendor/php-standard-library/interoperability/src/Utilities/use PHPStandardLibrary\Interoperability\Contracts\EventDispatcherInterface;
// Laravel's event dispatcher
$laravelDispatcher = new LaravelEventDispatcher();
// Third-party dispatcher
$thirdPartyDispatcher = new ThirdPartyEventDispatcher();
// Wrap with adapter for consistency
$standardDispatcher = new \PHPStandardLibrary\Interoperability\Adapters\EventDispatcherAdapter(
$thirdPartyDispatcher
);
$standardDispatcher->dispatch(new MyEvent());
Problem: Integrate a non-Laravel cache library with Laravel’s cache facade. Solution:
use PHPStandardLibrary\Interoperability\Contracts\CacheInterface;
use PHPStandardLibrary\Interoperability\Adapters\CacheAdapter;
class LaravelCacheWrapper implements CacheInterface {
public function __construct(private CacheAdapter $adapter) {}
public function get($key) {
return $this->adapter->get($key);
}
// Delegate other methods...
}
Pattern: Use MiddlewareInterface to abstract framework-specific middleware.
use PHPStandardLibrary\Interoperability\Contracts\MiddlewareInterface;
class AuthMiddleware implements MiddlewareInterface {
public function handle($request, Closure $next) {
if (!$request->user()) {
abort(403);
}
return $next($request);
}
}
// Register in Laravel via:
$app->bind(
\PHPStandardLibrary\Interoperability\Contracts\MiddlewareInterface::class,
AuthMiddleware::class
);
Use Case: Decouple event listeners from Laravel’s Illuminate\Events\Dispatcher.
use PHPStandardLibrary\Interoperability\Contracts\EventDispatcherInterface;
class OrderProcessedListener {
public function __construct(private EventDispatcherInterface $dispatcher) {}
public function handle(Order $order) {
$this->dispatcher->dispatch(new OrderProcessedEvent($order));
}
}
Leverage ServiceProviderInterface to register dependencies portably:
use PHPStandardLibrary\Interoperability\Contracts\ServiceProviderInterface;
class DatabaseServiceProvider implements ServiceProviderInterface {
public function register(Container $container) {
$container->singleton(
\PDO::class,
fn() => new PDO('mysql:host=...')
);
}
}
Illuminate\Contracts\Cache\Store implements CacheInterface.CacheAdapter to bridge implementations:
$adapter = new \PHPStandardLibrary\Interoperability\Adapters\LaravelCacheAdapter(
Cache::store('file')
);
// Bad: Tight coupling
new MyService(new LaravelEventDispatcher());
// Good: Interface-based
new MyService(new EventDispatcherAdapter(new ThirdPartyDispatcher()));
Adapter<Adapter<Service>>) can impact performance.ServiceProviderInterface. Explicitly bind interfaces:
$app->bind(
\PHPStandardLibrary\Interoperability\Contracts\CacheInterface::class,
fn($app) => new CacheAdapter($app->make(\Illuminate\Contracts\Cache\Store::class))
);
*Interface contracts:
$this->mock(CacheInterface::class)->shouldReceive('get')->once();
class DoctrineEntityManagerAdapter implements EntityManagerInterface {
public function __construct(private EntityManager $em) {}
// Implement methods...
}
instanceof checks or get_class() to verify implementations:
if (!$dispatcher instanceof EventDispatcherInterface) {
throw new \RuntimeException('Dispatcher must implement EventDispatcherInterface');
}
\Log::debug('Adapter: Delegating to underlying service', ['method' => __FUNCTION__]);
How can I help you explore Laravel packages today?