Completed
Push — 164 ( 46f240...8fff67 )
by Akihito
02:00
created

AbstractModule::bindPriorityInterceptor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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