Passed
Push — master ( b817d9...04ae87 )
by 世昌
02:23
created

ApplicationBuilder::loadManifest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\application\builder;
3
4
use suda\framework\Config;
5
use suda\application\Resource;
6
use suda\application\Application;
7
use suda\framework\loader\Loader;
8
use suda\framework\config\PathResolver;
9
use suda\framework\filesystem\FileSystem;
10
use suda\application\exception\ApplicationException;
11
12
/**
13
 * 应用程序
14
 * Class ApplicationBuilder
15
 * @package suda\application\builder
16
 */
17
class ApplicationBuilder
18
{
19
20
    /**
21
     * 创建应用
22
     * @param Loader $loader
23
     * @param string $path
24
     * @param string $manifast
25
     * @param string $dataPath
26
     * @return Application
27
     */
28
    public static function build(Loader $loader, string $path, string $manifast, string $dataPath):Application
29
    {
30
        $manifestConfig = static::loadManifest($manifast);
31
        if (array_key_exists('import', $manifestConfig)) {
32
            static::importClassLoader($loader, $manifestConfig['import'], $path);
33
        }
34
        $applicationClass = $manifestConfig['application'] ?? Application::class;
35
        /** @var Application $application */
36
        $application = new $applicationClass($path, $manifestConfig, $loader, $dataPath);
37
        return $application;
38
    }
39
40
    /**
41
     * 加载App主配置
42
     * @param string $path
43
     * @return array|null
44
     */
45
    public static function loadManifest(string $path)
46
    {
47
        $manifest = static::resolveManifest($path);
48
        return Config::loadConfig($manifest) ?? [];
49
    }
50
51
    /**
52
     * 获取Manifest路径
53
     *
54
     * @param string $path
55
     * @return string
56
     */
57
    protected static function resolveManifest(string $path):string
58
    {
59
        $manifest = PathResolver::resolve($path);
60
        if ($manifest === null) {
61
            FileSystem::copyDir(SUDA_RESOURCE.'/app', $path);
62
            $manifest = PathResolver::resolve($path);
63
        }
64
        if ($manifest === null) {
65
            throw new ApplicationException(
66
                sprintf('missing manifest in %s', dirname($path)),
67
                ApplicationException::ERR_MANIFEST_IS_EMPTY
68
            );
69
        } else {
70
            return $manifest;
71
        }
72
    }
73
74
    public static function importClassLoader(Loader $loader, array $import, string $relativePath)
75
    {
76
        foreach ($import as $name => $path) {
77
            $path = Resource::getPathByRelativePath($path, $relativePath);
78
            if (is_numeric($name) && is_dir($path)) {
79
                $loader->addIncludePath($path);
80
            } elseif (is_file($path)) {
81
                $loader->import($path);
82
            } else {
83
                $loader->addIncludePath($path, $name);
84
            }
85
        }
86
    }
87
}
88