Passed
Push — master ( 4c0566...cfb65e )
by 世昌
02:26
created

ModuleLoaderUtil   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A importClassLoader() 0 3 1
A versionCompare() 0 7 2
A checkRequirements() 0 6 3
A checkModuleRequirements() 0 18 3
A checkFrameworkVersion() 0 7 3
1
<?php
2
3
4
namespace suda\application\loader;
5
6
7
use suda\application\Application;
8
use suda\application\builder\ApplicationBuilder;
9
use suda\application\exception\ApplicationException;
10
use suda\application\Module;
11
12
class ModuleLoaderUtil
13
{
14
    /**
15
     * 应用程序
16
     *
17
     * @var Application
18
     */
19
    protected $application;
20
21
    /**
22
     * 运行环境
23
     *
24
     * @var Module
25
     */
26
    protected $module;
27
28
    /**
29
     * 模块加载器
30
     *
31
     * @param Application $application
32
     * @param Module $module
33
     */
34
    public function __construct(Application $application, Module $module)
35
    {
36
        $this->module = $module;
37
        $this->application = $application;
38
    }
39
40
    /**
41
     * 检查依赖项目
42
     *
43
     * @return void
44
     */
45
    protected function checkRequirements()
46
    {
47
        $this->checkFrameworkVersion();
48
        if ($require = $this->module->getProperty('require')) {
49
            foreach ($require as $module => $version) {
50
                $this->checkModuleRequirements($module, $version);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param string $module
57
     * @param string $version
58
     */
59
    private function checkModuleRequirements(string $module, string $version)
60
    {
61
        $target = $this->application->find($module);
62
        if ($target === null) {
63
            throw new ApplicationException(
64
                sprintf('%s module need %s %s but not exist', $this->module->getFullName(), $module, $version),
65
                ApplicationException::ERR_MODULE_REQUIREMENTS
66
            );
67
        }
68
        if (static::versionCompare($version, $target->getVersion()) !== true) {
69
            throw new ApplicationException(
70
                sprintf(
71
                    '%s module need %s version %s',
72
                    $this->module->getFullName(),
73
                    $target->getName(),
74
                    $target->getVersion()
75
                ),
76
                ApplicationException::ERR_MODULE_REQUIREMENTS
77
            );
78
        }
79
    }
80
81
    /**
82
     * 检查模块需求
83
     *
84
     * @return void
85
     */
86
    protected function checkFrameworkVersion()
87
    {
88
        if ($version = $this->module->getProperty('suda')) {
89
            if (static::versionCompare($version, SUDA_VERSION) !== true) {
90
                throw new ApplicationException(
91
                    sprintf('%s module need suda version %s', $this->module->getFullName(), $version),
92
                    ApplicationException::ERR_FRAMEWORK_VERSION
93
                );
94
            }
95
        }
96
    }
97
98
    /**
99
     * 导入 ClassLoader 配置
100
     *
101
     * @param array $import
102
     * @param string $relativePath
103
     * @return void
104
     */
105
    protected function importClassLoader(array $import, string $relativePath)
106
    {
107
        ApplicationBuilder::importClassLoader($this->application->loader(), $import, $relativePath);
108
    }
109
110
    /**
111
     * 比较版本
112
     *
113
     * @param string $version 比较用的版本,包含比较符号
114
     * @param string $compire 对比的版本
115
     * @return bool
116
     */
117
    public static function versionCompare(string $version, string $compire)
118
    {
119
        if (preg_match('/^(<=?|>=?|<>|!=)(.+)$/i', $version, $match)) {
120
            list($s, $op, $ver) = $match;
121
            return  version_compare($compire, $ver, $op);
122
        }
123
        return version_compare($compire, $version, '>=');
124
    }
125
}