Completed
Push — master ( 01f29a...d17efc )
by Woody
14:14
created

InjectorConfig::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Cairon;
4
5
use Auryn\Injector;
6
7
class InjectorConfig
8
{
9
    /**
10
     * Create a new config instance
11
     *
12
     * @return static
13
     */
14
    public static function make(Injector $injector = null)
15
    {
16
        return new static($injector);
17
    }
18
19
    /**
20
     * @var Injector
21
     */
22
    private $injector;
23
24
    public function __construct(Injector $injector = null)
25
    {
26
        if (null === $injector) {
27
            $injector = new Injector();
28
        }
29
30
        $this->injector = $injector;
31
    }
32
33
    /**
34
     * Apply a configuration to the injector
35
     *
36
     * The configuration must either be callable, or be a class name that refers
37
     * to a callable class.
38
     *
39
     * @param callable|string $config
40
     *
41
     * @return self
42
     */
43
    public function apply($config)
44
    {
45
        if (false === is_callable($config)) {
46
            $config = $this->injector->make($config);
47
        }
48
49
        $config($this->injector);
50
51
        return $this;
52
    }
53
54
    /**
55
     * Apply a list of configurations to the injector
56
     *
57
     * @param array $configs
58
     *
59
     * @return self
60
     */
61
    public function configure(array $configs)
62
    {
63
        \array_map([$this, 'apply'], $configs);
64
65
        return $this;
66
    }
67
68
    /**
69
     * Fetch the injector
70
     *
71
     * @return Injector
72
     */
73
    public function injector()
74
    {
75
        return $this->injector;
76
    }
77
}
78