-
Notifications
You must be signed in to change notification settings - Fork 88
/
CallableLocator.php
54 lines (48 loc) · 1.43 KB
/
CallableLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace League\Tactician\Handler\Locator;
use League\Tactician\Exception\MissingHandlerException;
/**
* This locator loads Handlers from a provided callable.
*
* At first glance, this might seem fairly useless but it's actually very
* useful to encapsulate DI containers without having to write a custom adapter
* for each one.
*
* Let's say you have a Symfony container or similar that works via a 'get'
* method. You can pass in an array style callable such as:
*
* $locator = new CallableLocator([$container, 'get'])
*
* This is easy to set up and will now automatically pipe the command name
* straight through to the $container->get() method without having to write
* the custom locator.
*
* Naturally, you can also pass in closures for further behavior tweaks.
*/
class CallableLocator implements HandlerLocator
{
/**
* @var callable
*/
private $callable;
/**
* @param callable $callable
*/
public function __construct(callable $callable)
{
$this->callable = $callable;
}
/**
* {@inheritdoc}
*/
public function getHandlerForCommand($commandName)
{
$callable = $this->callable;
$handler = $callable($commandName);
// Odds are the callable threw an exception but it always pays to check
if ($handler === null) {
throw MissingHandlerException::forCommand($commandName);
}
return $handler;
}
}