|
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
|
|
|
* @param Loader $loader |
|
22
|
|
|
* @param string $path |
|
23
|
|
|
* @param string $dataPath |
|
24
|
|
|
* @return Application |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function build(Loader $loader, string $path, string $dataPath):Application |
|
27
|
|
|
{ |
|
28
|
|
|
$manifest = static::resolveManifestPath($path); |
|
29
|
|
|
$manifestConfig = Config::loadConfig($manifest) ?? []; |
|
30
|
|
|
if (array_key_exists('import', $manifestConfig)) { |
|
31
|
|
|
static::importClassLoader($loader, $manifestConfig['import'], $path); |
|
32
|
|
|
} |
|
33
|
|
|
$applicationClass = $manifestConfig['application'] ?? Application::class; |
|
34
|
|
|
$application = new $applicationClass($path, $manifestConfig, $loader, $dataPath); |
|
35
|
|
|
return $application; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 获取Manifest路径 |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function resolveManifestPath(string $path):string |
|
45
|
|
|
{ |
|
46
|
|
|
$manifest = PathResolver::resolve($path.'/manifest'); |
|
47
|
|
|
if ($manifest === null) { |
|
48
|
|
|
FileSystem::copyDir(SUDA_RESOURCE.'/app', $path); |
|
49
|
|
|
$manifest = PathResolver::resolve($path.'/manifest'); |
|
50
|
|
|
} |
|
51
|
|
|
if ($manifest === null) { |
|
52
|
|
|
throw new ApplicationException(sprintf('missing manifest in %s', $path), ApplicationException::ERR_MANIFAST_IS_EMPTY); |
|
53
|
|
|
} else { |
|
54
|
|
|
return $manifest; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function importClassLoader(Loader $loader, array $import, string $relativePath) |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($import as $name => $path) { |
|
61
|
|
|
$path = Resource::getPathByRelativePath($path, $relativePath); |
|
62
|
|
|
if (is_numeric($name)) { |
|
63
|
|
|
$loader->addIncludePath($path); |
|
64
|
|
|
} elseif (is_file($path)) { |
|
65
|
|
|
$loader->import($path); |
|
66
|
|
|
} else { |
|
67
|
|
|
$loader->addIncludePath($path, $name); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|