Passed
Push — master ( e0597b...4dce29 )
by 世昌
02:35
created

Application::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application;
3
4
use Exception;
5
use nebula\NebulaApplication;
6
use nebula\application\Resource;
7
use nebula\component\loader\Path;
8
use nebula\application\config\Config;
9
use nebula\component\request\Request;
10
11
/**
12
 * 应用程序加载器
13
 *
14
 * 负责加载
15
 */
16
class Application
17
{
18
    /**
19
     * 应用
20
     *
21
     * @var Context
0 ignored issues
show
Bug introduced by
The type nebula\application\Context 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...
22
     */
23
    protected $context;
24
25
    /**
26
     * 资源管理
27
     *
28
     * @var Resource
29
     */
30
    protected $resource;
31
32
33
    /**
34
     * Get 资源管理
35
     *
36
     * @return  Resource
37
     */
38
    public function getResource()
39
    {
40
        return $this->resource;
41
    }
42
43
    /**
44
     * Set 资源管理
45
     *
46
     * @param  Resource  $resource  资源管理
47
     *
48
     * @return  self
49
     */
50
    public function setResource(Resource $resource)
51
    {
52
        $this->resource = $resource;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get 应用
59
     *
60
     * @return  Context
61
     */ 
62
    public function getContext()
63
    {
64
        return $this->context;
65
    }
66
67
    /**
68
     * Set 应用
69
     *
70
     * @param  Context  $context  应用
71
     *
72
     * @return  self
73
     */ 
74
    public function setContext(Context $context)
75
    {
76
        $this->context = $context;
77
78
        return $this;
79
    }
80
81
    /**
82
     * 初始化
83
     *
84
     * @return void
85
     */
86
    public function initialization()
87
    {
88
        $this->initResource();
89
        $this->importClassLoader($this->getConfig('app.import', []));
90
    }
91
92
    /**
93
     * 处理请求
94
     *
95
     * @param Request $request
96
     * @return boolean
97
     */
98
    public function onRequest(Request $request): bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

98
    public function onRequest(/** @scrutinizer ignore-unused */ Request $request): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        return false;
101
    }
102
103
    /**
104
     * 处理异常
105
     *
106
     * @param Exception $exception
107
     * @return boolean
108
     */
109
    public function onException(Exception $exception): bool
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function onException(/** @scrutinizer ignore-unused */ Exception $exception): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return false;
112
    }
113
114
    /**
115
     * 初始化资源
116
     *
117
     * @return void
118
     */
119
    protected function initResource()
120
    {
121
        $resource = new Resource;
122
        foreach ($this->getConfig('app.resource') as $path) {
123
            if (Path::isRelativePath($path)) {
124
                $path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP'));
125
            }
126
            $resource->addResourcePath($path);
127
        }
128
        $this->setResource($resource);
129
    }
130
131
    
132
    /**
133
     * 导入 ClassLoader 配置
134
     *
135
     * @param array $import
136
     * @return void
137
     */
138
    public function importClassLoader(array $import)
139
    {
140
        foreach ($import as $name => $path) {
141
            if (Path::isRelativePath($path)) {
142
                $path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP'));
143
            }
144
            if (\is_numeric($name)) {
145
                $this->getContext()->getClassLoader()->addIncludePath($path);
146
            } elseif (is_file($path)) {
147
                $this->getContext()->getClassLoader()->import($path);
148
            } else {
149
                $this->getContext()->getClassLoader()->addIncludePath($path, $name);
150
            }
151
        }
152
    }
153
154
    /**
155
     * 获取配置
156
     *
157
     * @param string $name
158
     * @param mixed $default
159
     * @return mixed
160
     */
161
    public function getConfig(string $name, $default = null)
162
    {
163
        return $this->getContext()->getConfig()->get($name, $default);
164
    }
165
}
166