Introduction
The Streams Platform provides a powerful value handler pattern that let's you defer the value or handling of something to a handler
class. The resolver
service makes it easy to resolve the value from such handlers.
The resolver
is usually used in other parts of the system so it's helpful to understand how it works even though you may not use it directly.
Handlers are used heavily in the build process for UI builders.{.tip}
Handlers
Handlers is a generic term for a class that handles the value for an attribute that has an setter or mutator method like setFoo
.
Where a typical attribute in an array might look like:
$array = [
'example' => 'Test',
];
A value handler
for example might look like this:
$array = [
'example' => \Example\TestHandler::class,
];
In the above example the method is assumed handle
but you can define your own:
$array = [
'example' => `Example\TestHandler@value`,
];
Basic Usage
To start resolving values in your own class you need to include the \Anomaly\Streams\Platform\Support\Resolver
class.
resolve
$resolver = app(\Anomaly\Streams\Platform\Support\Resolver::class);
$resolver->resolve($target, array $arguments = [], array $options = []);
The resolve
method recursively resolves values within the target
value. The target
is called through the Service Container and supports class and method injection.
The arguments
will be available for injection as well.
$resolver = app(\Anomaly\Streams\Platform\Support\Resolver::class);
$resolver->resolve('Example\Test@value', compact('entry'));
A simple handler might look something like this:
<?php namespace Example;
class Test
{
public function value($entry)
{
return ucfirst($entry->name);
}
}