Passed
Push — master ( 82ff8b...c78fe7 )
by 世昌
02:14
created

ModuleBuilder::build()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 18
nop 2
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace suda\application\builder;
3
4
use Iterator;
5
use ZipArchive;
6
use suda\framework\Config;
7
use suda\application\Module;
8
use suda\application\Resource;
9
use suda\framework\config\PathResolver;
10
use suda\framework\filesystem\FileSystem;
11
12
/**
13
 * 模块构建工具
14
 */
15
class ModuleBuilder
16
{
17
    /**
18
     * 从配置文件构建模块
19
     *
20
     * @param string $path
21
     * @param string $propertyPath
22
     * @return Module
23
     */
24
    public static function build(string $path, string $propertyPath):Module
25
    {
26
        $property = Config::loadConfig($propertyPath, ['path' => $path]) ?? [];
27
        $name = dirname($path);
28
        $version = '1.0.0';
29
        $resource = './resource';
30
        if ($property) {
31
            if (array_key_exists('name', $property)) {
32
                $name = $property['name'];
33
            }
34
            if (array_key_exists('version', $property)) {
35
                $version = $property['version'];
36
            }
37
            if (array_key_exists('resource', $property)) {
38
                $resource = $property['resource'];
39
            }
40
        }
41
        $module = new Module($name, $version, $path, $property);
42
        $module->getResource()->addResourcePath(Resource::getPathByRelativePath($resource, $path));
43
        $path = $module->getResource()->getConfigResourcePath('config/config');
44
        if ($path !== null && ($config = Config::loadConfig($path, $property)) !== null) {
45
            $module->setConfig($config);
46
        }
47
        return $module;
48
    }
49
    
50
    /**
51
     * 检查模块配置
52
     *
53
     * @param string $path
54
     * @return string|null
55
     */
56
    public static function check(string $path): ?string
57
    {
58
        return PathResolver::resolve($path.'/module');
59
    }
60
61
    /**
62
     * 检查ZIP包
63
     *
64
     * @param string $path
65
     * @param string $unpackPath
66
     * @return string|null
67
     */
68
    public static function checkPack(string $path, string $unpackPath): ?string
69
    {
70
        $extension = pathinfo($path, PATHINFO_EXTENSION);
71
        if ($extension !== 'mod' && $extension !== 'module') {
72
            return null;
73
        }
74
        $zip = new ZipArchive;
75
        if ($zip->open($path, ZipArchive::CHECKCONS)) {
76
            $unzipPath = $unpackPath.'/'. pathinfo($path, PATHINFO_FILENAME) .'-'.substr(md5_file($path), 0, 8);
77
            $zip->extractTo($unzipPath);
78
            $zip->close();
79
            return PathResolver::resolve($unzipPath.'/module');
80
        }
81
        return null;
82
    }
83
84
    /**
85
     * 扫描模块
86
     *
87
     * @param string $modulesPath
88
     * @param string $extractPath
89
     * @return Iterator
90
     */
91
    public static function scan(string $modulesPath, string $extractPath): Iterator
92
    {
93
        $enabledPack = class_exists('ZipArchive');
94
        foreach (FileSystem::read($modulesPath) as $path) {
95
            if (is_file($path) && $enabledPack) {
96
                if ($configPath = static::checkPack($path, $extractPath)) {
97
                    yield static::build($path, $configPath);
98
                }
99
            } elseif (is_dir($path)) {
100
                if ($configPath = static::check($path)) {
101
                    yield static::build($path, $configPath);
102
                }
103
            }
104
        }
105
    }
106
}
107