|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use App\Domain\Content; |
|
6
|
|
|
use App\Domain\PathPart; |
|
7
|
|
|
use App\UI\UserInterface; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
10
|
|
|
|
|
11
|
|
|
final class Kernel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param \App\UI\UserInterface $ui |
|
15
|
|
|
* |
|
16
|
|
|
* @throws \RuntimeException |
|
17
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
18
|
|
|
* @throws \Symfony\Component\Yaml\Exception\ParseException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __invoke(UserInterface $ui): void |
|
21
|
|
|
{ |
|
22
|
|
|
$config = (array) Yaml::parseFile($this->getProjectDir().DIRECTORY_SEPARATOR.'config/app.yml'); |
|
23
|
|
|
$rootPathPart = $this->getRootPathPart($config['path_part']); |
|
24
|
|
|
|
|
25
|
|
|
foreach ((array) $config['sources'] as $sources) { |
|
26
|
|
|
foreach ((array) $sources as $sourceClassName => $sourceData) { |
|
27
|
|
|
|
|
28
|
|
|
/** @var \App\Domain\Source $source */ |
|
29
|
|
|
$source = new $sourceClassName($ui, $sourceData['config'] ?? []); |
|
30
|
|
|
|
|
31
|
|
|
// Add the root path part to the contents' path |
|
32
|
|
|
$contents = $source->getContents() |
|
33
|
|
|
->map(function (Content $content) use ($rootPathPart) { |
|
34
|
|
|
$content->getPath()->add($rootPathPart); |
|
35
|
|
|
|
|
36
|
|
|
return $content; |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
foreach ((array) $sourceData['platforms'] as $platforms) { |
|
40
|
|
|
foreach ((array) $platforms as $platformClassName => $platformConfig) { |
|
41
|
|
|
|
|
42
|
|
|
/** @var \App\Domain\Platform $platform */ |
|
43
|
|
|
$platform = new $platformClassName($ui, $platformConfig ?? []); |
|
44
|
|
|
$platform->synchronizeContents($contents, $rootPathPart); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \RuntimeException |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getProjectDir(): string |
|
57
|
|
|
{ |
|
58
|
|
|
if (!($projectRoot = getenv('PROJECT_ROOT'))) { |
|
59
|
|
|
throw new \RuntimeException('The environment variable "PROJECT_ROOT" has not been defined.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return rtrim($projectRoot, DIRECTORY_SEPARATOR); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array $config |
|
67
|
|
|
* |
|
68
|
|
|
* @return \App\Domain\PathPart |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \RuntimeException |
|
71
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function getRootPathPart(array $config): PathPart |
|
74
|
|
|
{ |
|
75
|
|
|
$config['substitutions'] = [ |
|
76
|
|
|
'%project_root%' => $this->getProjectDir() |
|
77
|
|
|
] + ($config['substitutions'] ?? []); |
|
78
|
|
|
$rootPathPart = new PathPart($config); |
|
79
|
|
|
|
|
80
|
|
|
// Try to create the root directory... 'cause if it fails, nothing will work. |
|
81
|
|
|
(new Filesystem())->mkdir($rootPathPart->getPath()); |
|
82
|
|
|
|
|
83
|
|
|
return $rootPathPart; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.