-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathInjector.php
More file actions
37 lines (29 loc) · 896 Bytes
/
Injector.php
File metadata and controls
37 lines (29 loc) · 896 Bytes
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
<?php
namespace Task;
class Injector
{
protected $container;
public function __construct(\Pimple $container)
{
$this->container = $container;
}
public function __invoke(array $arguments, $bindTo = null)
{
$callback = array_pop($arguments);
if (!is_callable($callback)) {
throw new \InvalidArgumentException("Last element must be callable");
}
if ($callback instanceof \Closure) {
$callback = $callback->bindTo($bindTo);
}
# Can't do this with array_map because exceptions are swallowed (see
# https://bugs.php.net/bug.php?id=55416).
$args = [];
foreach ($arguments as $id) {
$args[] = $this->container[$id];
}
return function () use ($callback, $args) {
return call_user_func_array($callback, $args);
};
}
}