1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Generator; |
13
|
|
|
|
14
|
|
|
use Dflydev\DotAccessConfiguration\Configuration; |
15
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceInterface; |
16
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceSet; |
17
|
|
|
|
18
|
|
|
final class GeneratorManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Configuration |
22
|
|
|
*/ |
23
|
|
|
private $siteConfiguration; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var GeneratorInterface[] |
27
|
|
|
*/ |
28
|
|
|
private $generators = []; |
29
|
|
|
|
30
|
|
|
public function __construct(Configuration $siteConfiguration) |
31
|
|
|
{ |
32
|
|
|
$this->siteConfiguration = $siteConfiguration; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function registerGenerator(string $name, GeneratorInterface $generator) |
36
|
|
|
{ |
37
|
|
|
$this->generators[$name] = $generator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws \InvalidArgumentException |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function generate(SourceInterface $source, SourceSet $sourceSet) |
46
|
|
|
{ |
47
|
|
|
$data = $source->data(); |
48
|
|
|
|
49
|
|
|
$generators = []; |
50
|
|
|
$isGenerator = $source->isGenerator(); |
51
|
|
|
if ($generatorNames = $data->get('generator')) { |
52
|
|
|
if (!$isGenerator) { |
53
|
|
|
$source->setIsGenerator(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ((array) $generatorNames as $generatorName) { |
57
|
|
|
if (!isset($this->generators[$generatorName])) { |
58
|
|
|
throw new \InvalidArgumentException( |
59
|
|
|
"Requested generator '$generatorName' could not be found; was it registered?" |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$generators[] = $this->generators[$generatorName]; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
if ($isGenerator) { |
67
|
|
|
$source->setIsNotGenerator(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$targetSources = [$source]; |
74
|
|
|
|
75
|
|
|
foreach ($generators as $generator) { |
76
|
|
|
$newTargetSources = []; |
77
|
|
|
foreach ($targetSources as $targetSource) { |
78
|
|
|
foreach ((array) $generator->generate($targetSource) as $generatedSource) { |
79
|
|
|
$generatedSource->setIsGenerated(); |
80
|
|
|
$newTargetSources[] = $generatedSource; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$targetSources = $newTargetSources; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($targetSources as $generatedSource) { |
87
|
|
|
$sourceSet->mergeSource($generatedSource); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|