Issues (94)

src/di/AbstractModule.php (3 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\AbstractMatcher;
8
use Ray\Aop\Matcher;
9
use Ray\Aop\Pointcut;
10
use Ray\Aop\PriorityPointcut;
0 ignored issues
show
The type Ray\Aop\PriorityPointcut was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Stringable;
12
13
use function assert;
14
use function class_exists;
15
use function interface_exists;
16
17
/**
18
 * @psalm-import-type BindableInterface from Types
19
 * @psalm-import-type PointcutList from Types
20
 * @psalm-import-type InterceptorClassList from Types
21
 */
22
abstract class AbstractModule implements Stringable
23
{
24
    /** @var Matcher */
25
    protected $matcher;
26
27
    /** @var ?AbstractModule */
28
    protected $lastModule;
29
    private ?Container $container = null;
30
31
    public function __construct(
32
        ?self $module = null
33
    ) {
34
        $this->lastModule = $module;
35
        $this->activate();
36
        if ($module instanceof self && $this->container instanceof Container) {
37
            $this->container->merge($module->getContainer());
38
        }
39
    }
40
41
    public function __toString(): string
42
    {
43
        return (new ModuleString())($this->getContainer(), $this->getContainer()->getPointcuts());
44
    }
45
46
    /**
47
     * Install module
48
     */
49
    public function install(self $module): void
50
    {
51
        $this->getContainer()->merge($module->getContainer());
52
    }
53
54
    /**
55
     * Override module
56
     */
57
    public function override(self $module): void
58
    {
59
        $module->getContainer()->merge($this->getContainer());
60
        $this->container = $module->getContainer();
61
    }
62
63
    /**
64
     * Return activated container
65
     */
66
    public function getContainer(): Container
67
    {
68
        if ($this->container === null) {
69
            $this->activate();
70
        }
71
72
        assert($this->container instanceof Container);
73
74
        return $this->container;
75
    }
76
77
    /**
78
     * Bind interceptor
79
     *
80
     * @param InterceptorClassList $interceptors
0 ignored issues
show
The type Ray\Di\InterceptorClassList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
     */
82
    public function bindInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors): void
83
    {
84
        $pointcut = new Pointcut($classMatcher, $methodMatcher, $interceptors);
85
        $this->getContainer()->addPointcut($pointcut);
86
        foreach ($interceptors as $interceptor) {
87
            if (class_exists($interceptor)) {
88
                (new Bind($this->getContainer(), $interceptor))->to($interceptor)->in(Scope::SINGLETON);
89
90
                return;
91
            }
92
93
            assert(interface_exists($interceptor));
94
            (new Bind($this->getContainer(), $interceptor))->in(Scope::SINGLETON);
95
        }
96
    }
97
98
    /**
99
     * Bind interceptor early
100
     *
101
     * @param InterceptorClassList $interceptors
102
     */
103
    public function bindPriorityInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors): void
104
    {
105
        $pointcut = new PriorityPointcut($classMatcher, $methodMatcher, $interceptors);
106
        $this->getContainer()->addPointcut($pointcut);
107
        foreach ($interceptors as $interceptor) {
108
            (new Bind($this->getContainer(), $interceptor))->to($interceptor)->in(Scope::SINGLETON);
109
        }
110
    }
111
112
    /**
113
     * Rename binding name
114
     *
115
     * @param string $interface       Interface
116
     * @param string $newName         New binding name
117
     * @param string $sourceName      Original binding name
118
     * @param string $targetInterface Original interface
119
     */
120
    public function rename(string $interface, string $newName, string $sourceName = Name::ANY, string $targetInterface = ''): void
121
    {
122
        $targetInterface = $targetInterface ?: $interface;
123
        if ($this->lastModule instanceof self) {
124
            $this->lastModule->getContainer()->move($interface, $sourceName, $targetInterface, $newName);
125
        }
126
    }
127
128
    /**
129
     * Configure binding
130
     *
131
     * @return void
132
     *
133
     * @noinspection ReturnTypeCanBeDeclaredInspection
134
     */
135
    abstract protected function configure();
136
137
    /**
138
     * Bind interface
139
     *
140
     * @param BindableInterface $interface
0 ignored issues
show
The type Ray\Di\BindableInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
141
     */
142
    protected function bind(string $interface = ''): Bind
143
    {
144
        return new Bind($this->getContainer(), $interface);
145
    }
146
147
    /**
148
     * Activate bindings
149
     */
150
    private function activate(): void
151
    {
152
        $this->container = new Container();
153
        $this->matcher = new Matcher();
154
        $this->configure();
155
    }
156
}
157