Passed
Push — master ( 934657...c3ba90 )
by 世昌
02:07
created

Builder::checkPack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\module;
3
4
use Iterator;
5
use ZipArchive;
6
use nebula\application\config\Config;
7
use nebula\application\module\Module;
8
use nebula\application\filesystem\FileSystem;
9
10
/**
11
 * 模块构建工具
12
 */
13
class Builder
14
{
15
    /**
16
     * 从配置文件构建模块
17
     *
18
     * @param string $path
19
     * @return Module
20
     */
21
    public static function build(string $path):Module
22
    {
23
        $config = new Config;
24
        $config->load($path);
25
        if (!$config->has('name')) {
26
            $config->set('name', basename(dirname($path)));
27
        }
28
        $module = new Module(dirname($path), $config);
29
        return $module;
30
    }
31
    
32
    public static function check(string $path): ?string
33
    {
34
        return Config::resolve($path.'/module');
35
    }
36
37
    public static function checkPack(string $path, string $unpackPath): ?string
38
    {
39
        $zip=new ZipArchive;
40
        if ($zip->open($inputFile, ZipArchive::CHECKCONS)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inputFile seems to be never defined.
Loading history...
41
            $unzipPath = $unpackPath.'/'. pathinfo($path, PATHINFO_FILENAME) .'-'.substr(md5_file($path), 0, 8);
42
            $zip->extractTo($unzipPath);
43
            $zip->close();
44
            return Config::resolve($unzipPath.'/module');
45
        }
46
        return null;
47
    }
48
49
    /**
50
     * 扫描模块
51
     *
52
     * @param array $scanPaths
53
     * @param string $extractPath
54
     * @return Iterator
55
     */
56
    public static function scan(array $scanPaths, string $extractPath): Iterator
57
    {
58
        foreach ($scanPaths as $modulesPath) {
59
            foreach (FileSystem::read($modulesPath) as $path) {
60
                if (is_file($path)) {
61
                    $extension = pathinfo($modulePath, PATHINFO_EXTENSION);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modulePath seems to be never defined.
Loading history...
62
                    if (
63
                        $extension === 'mod' ||
64
                        $extension === 'module') {
65
                        if ($configPath = static::checkPack($path, $extractPath)) {
66
                           yield static::build($configPath);
67
                        }
68
                    }
69
                } elseif (is_dir($path)) {
70
                    if ($configPath = static::check($path)) {
71
                        yield static::build($configPath);
72
                    }
73
                }
74
            }
75
        }
76
    }
77
}
78