Completed
Push — 2.x ( 26569f...004d76 )
by Akihito
02:57
created

AbstractModule::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
nc 1
cc 1
eloc 3
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Ray\Aop\AbstractMatcher;
10
use Ray\Aop\Matcher;
11
use Ray\Aop\Pointcut;
12
use Ray\Aop\PriorityPointcut;
13
14
abstract class AbstractModule
15
{
16
    /**
17
     * @var Matcher
18
     */
19
    protected $matcher;
20
21
    /**
22
     * @var AbstractModule
23
     */
24
    protected $lastModule;
25
26
    /**
27
     * @var Container
28
     */
29
    private $container;
30
31 48
    public function __construct(
32
        AbstractModule $module = null
33
    ) {
34 48
        $this->lastModule = $module;
35 48
        $this->activate();
36 47
        if ($module) {
37 5
            $this->container->merge($module->getContainer());
38
        }
39 47
    }
40
41
    /**
42
     * Install module
43
     *
44
     * @param AbstractModule $module
45
     */
46 41
    public function install(AbstractModule $module)
47
    {
48 41
        $this->getContainer()->merge($module->getContainer());
49 41
    }
50
51
    /**
52
     * Override module
53
     *
54
     * @param AbstractModule $module
55
     */
56 1
    public function override(AbstractModule $module)
57
    {
58 1
        $module->getContainer()->merge($this->container);
59 1
        $this->container = $module->getContainer();
60 1
    }
61
62
    /**
63
     * Return container
64
     *
65
     * @return Container
66
     */
67 49
    public function getContainer()
68
    {
69 49
        if (! $this->container) {
70 2
            $this->activate();
71
        }
72
73 49
        return $this->container;
74
    }
75
76
    /**
77
     * Bind interceptor
78
     *
79
     * @param AbstractMatcher $classMatcher
80
     * @param AbstractMatcher $methodMatcher
81
     * @param array           $interceptors
82
     */
83 40
    public function bindInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
84
    {
85 40
        $pointcut = new Pointcut($classMatcher, $methodMatcher, $interceptors);
86 40
        $this->container->addPointcut($pointcut);
87 40
        foreach ($interceptors as $interceptor) {
88 40
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
89
        }
90 40
    }
91
92
    /**
93
     * Bind interceptor early
94
     *
95
     * @param AbstractMatcher $classMatcher
96
     * @param AbstractMatcher $methodMatcher
97
     * @param array           $interceptors
98
     */
99 1
    public function bindPriorityInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
100
    {
101 1
        $pointcut = new PriorityPointcut($classMatcher, $methodMatcher, $interceptors);
102 1
        $this->container->addPointcut($pointcut);
103 1
        foreach ($interceptors as $interceptor) {
104 1
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
105
        }
106 1
    }
107
108
    /**
109
     * Rename binding name
110
     *
111
     * @param string $interface       Interface
112
     * @param string $newName         New binding name
113
     * @param string $sourceName      Original binding name
114
     * @param string $targetInterface Original interface
115
     */
116 1
    public function rename($interface, $newName, $sourceName = Name::ANY, $targetInterface = '')
117
    {
118 1
        $targetInterface = $targetInterface ?: $interface;
119 1
        $this->lastModule->getContainer()->move($interface, $sourceName, $targetInterface, $newName);
120 1
    }
121
122
    /**
123
     * Configure binding
124
     */
125
    abstract protected function configure();
126
127
    /**
128
     * Bind interface
129
     *
130
     * @param string $interface
131
     *
132
     * @return Bind
133
     */
134 48
    protected function bind($interface = '')
135
    {
136 48
        $bind = new Bind($this->getContainer(), $interface);
137
138 47
        return $bind;
139
    }
140
141 49
    private function activate()
142
    {
143 49
        $this->container = new Container;
144 49
        $this->matcher = new Matcher;
145 49
        $this->configure();
146 48
    }
147
}
148