Safely Invoke Any Backbone Operation, From Anywhere
Generic invocation and introspection for Backbone services: turn any worker’s operation into something safely callable by id, with plain array parameters, from outside PHP’s own type system.
Why
A Backbone worker’s public methods are ordinary, statically-typed PHP methods — great for an in-process PHP caller, useless for anything else. Backbone Dispatcher adds a generic invocation layer on top: it turns a string operation id ("package.component.worker::operation") plus a plain associative array of parameters into a real, reflection-resolved, type-coerced method call on the right worker, and turns whatever comes back — a return value or an uncaught exception — into a plain, serializable shape.
That is what makes it possible for a caller with no concept of PHP reflection, PHP’s type system, or PHP exceptions — Python code across a phpy boundary, an HTTP client through Backbone API, or anything else — to invoke any Backbone operation and get back a predictable, serializable answer either way.
Installation
composer require derafu/backbone-dispatcher
Requirements
PHP 8.5+. Depends on derafu/backbone, derafu/cache and derafu/translation.
Dependency Injection
Import the production composition and supply the application’s existing Backbone
PackageRegistryInterface. The package does not create an empty registry:
imports:
- { resource: '../vendor/derafu/backbone-dispatcher/resources/config/backbone-dispatcher-services.yaml' }
parameters:
derafu.backbone_dispatcher.environment: prod
derafu.backbone_dispatcher.debug: false
derafu.backbone_dispatcher.project_dir: null
The YAML registers the inspector, object factory, serializer, all three dispatcher
tiers and both discovery entry points under their interfaces. It uses one
TaggedOperationPolicy for discovery and execution: only #[Operation] methods
are exposed. Override OperationPolicyInterface to choose a different policy.
The internal Caster, Resolver and Validator are concrete collaborators.
Applications using Derafu Kernel can map the three parameters to
%kernel.environment%, %kernel.debug% and %kernel.project_dir% respectively.
The package configuration itself does not require a kernel. Services are private;
the application can expose the entry points it retrieves directly.
Deserializer Contributions
Each domain registers its own deserializers. The tag’s type attribute is the
exact class or interface that the deserializer constructs, replacing a manually
assembled $deserializers map:
services:
App\Deserializer\InvoiceDeserializer:
arguments:
$invoiceFactory: '@App\Contract\InvoiceFactoryInterface'
tags:
- name: derafu_backbone_dispatcher.deserializer
type: App\Contract\InvoiceInterface
The indexed tagged collection feeds ObjectFactoryInterface; existing PHP callers
can still pass an array, and other iterables are supported too. The default
DeserializerInterface is FromArrayDeserializer, used as a fallback after
explicit contributions. Applications can replace it through that interface.
The object factory is lazy so a contributed deserializer can inject the same
ObjectFactoryInterface to construct nested objects. This avoids duplicating the
registry and supports recursive domain structures.
Optional Inspection Cache
Import backbone-dispatcher-cache-services.yaml instead of the base file to
include the base graph and decorate its shared inspector with CachedInspector:
imports:
- { resource: '../vendor/derafu/backbone-dispatcher/resources/config/backbone-dispatcher-cache-services.yaml' }
parameters:
derafu.backbone_dispatcher.inspector.ttl: 3600
The application must supply Psr\Cache\CacheItemPoolInterface; no backend is
selected or replaced. The decorator uses CacheKeyInterface from the imported
Cache configuration. A TTL of zero or less bypasses the pool. The base dispatcher
configuration uses ordinary inspection without requiring a pool.