-
Notifications
You must be signed in to change notification settings - Fork 12
6.4. Resolver Traits
A resolver follows a container pattern and is used throughout the framework to manage dependancies within classes. The main difference between this and containers found in other frameworks is that you can mount resolvers via traits.
use Cradle\Resolver\ResolverTrait;
class MyClass
{
use ResolverTrait;
}Out of the box, resolvers can be used to instantiate classes like the following example.
use Cradle\Event\EventHandler;
$handler = $this->resolve(EventHandler::class, ...$args);If you wanted to make this into a static class, where it returns the same instance every time we can do so like the following.
$this->addResolver(EventHandler::class, function(...$args) {
static $class = null;
if (is_null($class)) {
$class = new EventHandler(...$args);
}
return $class;
});There's no need to setup a static class pattern for every class. `ResolverTrait`
also comes with a method that does just that.
$this->resolveShared(EventHandler::class, ...$args);What the example above also shows that there's nothing stopping us from out
right stubbing the EventHandler class. The following shows how this can be
done.
$this->addResolver(EventHandler::class, function(...$args) {
return new EventHandlerStub(...$args);;
});Resolvers can also resolve static methods like the following.
$this->resolveStatic(MyClass::class, 'aStaticMethod', ...$args);
$this->resolve('MyClass::aStaticMethod', ...$args);In fact you can use Resolvers for anything that needs a resolution.
$this->addResolver('foo', function($number) {
return 'bar' . $number;
});
$this->resolve('foo', 1); //--> 'bar1'2.B. Reference: Validation Types
2.D. Reference: Indexes & Relations
3.A. Reference: Cradle on Shared Hosts
3.B. Reference: Command Line Tools
3.C. Reference: Architecture Recommendations
4.4. Intro to Handlebars Templating
4.B. Reference: Handlebars Helpers
4.C. Reference: Doon Interfaces
4.D. Reference: Global Package Methods