Completed
Push — 2.x ( b6ae1b...61ad4a )
by Akihito
9s
created

AbstractModule   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.56%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 1
cbo 5
dl 0
loc 134
ccs 40
cts 41
cp 0.9756
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A override() 0 5 1
A bindPriorityInterceptor() 0 8 2
configure() 0 1 ?
A bind() 0 6 1
A install() 0 4 1
A getContainer() 0 8 2
A activate() 0 6 1
A __construct() 0 9 2
A rename() 0 5 2
A bindInterceptor() 0 8 2
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 Container
23
     */
24
    private $container;
25
26
    /**
27
     * @var AbstractModule
28
     */
29
    protected $lastModule;
30
31 45
    public function __construct(
32
        AbstractModule $module = null
33
    ) {
34 45
        $this->lastModule = $module;
35 45
        $this->activate();
36 44
        if ($module) {
37 5
            $this->container->merge($module->getContainer());
38
        }
39 44
    }
40
41
    /**
42
     * Configure binding
43
     */
44
    abstract protected function configure();
45
46
    /**
47
     * Bind interface
48
     *
49
     * @param string $interface
50
     *
51
     * @return Bind
52
     */
53 37
    protected function bind($interface = '')
54
    {
55 37
        $bind = new Bind($this->getContainer(), $interface);
56
57 36
        return $bind;
58
    }
59
60
    /**
61
     * Install module
62
     *
63
     * @param AbstractModule $module
64
     */
65 38
    public function install(AbstractModule $module)
66
    {
67 38
        $this->getContainer()->merge($module->getContainer());
68 38
    }
69
70
    /**
71
     * Override module
72
     *
73
     * @param AbstractModule $module
74
     */
75 1
    public function override(AbstractModule $module)
76
    {
77 1
        $module->getContainer()->merge($this->container);
78 1
        $this->container = $module->getContainer();
79 1
    }
80
81
    /**
82
     * Return container
83
     *
84
     * @return Container
85
     */
86 45
    public function getContainer()
87
    {
88 45
        if (! $this->container) {
89
            $this->activate();
90
        }
91
92 45
        return $this->container;
93
    }
94
95
    /**
96
     * Bind interceptor
97
     *
98
     * @param AbstractMatcher $classMatcher
99
     * @param AbstractMatcher $methodMatcher
100
     * @param array           $interceptors
101
     */
102 37
    public function bindInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
103
    {
104 37
        $pointcut = new Pointcut($classMatcher, $methodMatcher, $interceptors);
105 37
        $this->container->addPointcut($pointcut);
106 37
        foreach ($interceptors as $interceptor) {
107 37
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
108
        }
109 37
    }
110
111
    /**
112
     * Bind interceptor early
113
     *
114
     * @param AbstractMatcher $classMatcher
115
     * @param AbstractMatcher $methodMatcher
116
     * @param array           $interceptors
117
     */
118 1
    public function bindPriorityInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
119
    {
120 1
        $pointcut = new PriorityPointcut($classMatcher, $methodMatcher, $interceptors);
121 1
        $this->container->addPointcut($pointcut);
122 1
        foreach ($interceptors as $interceptor) {
123 1
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
124
        }
125 1
    }
126
127 45
    private function activate()
128
    {
129 45
        $this->container = new Container;
130 45
        $this->matcher = new Matcher;
131 45
        $this->configure();
132 44
    }
133
134
    /**
135
     * Rename binding name
136
     *
137
     * @param string $interface       Interface
138
     * @param string $newName         New binding name
139
     * @param string $sourceName      Original binding name
140
     * @param string $targetInterface Original interface
141
     */
142 1
    public function rename($interface, $newName, $sourceName = Name::ANY, $targetInterface = '')
143
    {
144 1
        $targetInterface = $targetInterface ?: $interface;
145 1
        $this->lastModule->getContainer()->move($interface, $sourceName, $targetInterface, $newName);
146 1
    }
147
}
148