|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules\Mixer; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_input\Interfaces\IFiltered; |
|
7
|
|
|
use kalanis\kw_modules\Interfaces\ILoader; |
|
8
|
|
|
use kalanis\kw_modules\Interfaces\IMdTranslations; |
|
9
|
|
|
use kalanis\kw_modules\Interfaces\Lists\IModulesList; |
|
10
|
|
|
use kalanis\kw_modules\Interfaces\Lists\ISitePart; |
|
11
|
|
|
use kalanis\kw_modules\ModulesLists; |
|
12
|
|
|
use kalanis\kw_modules\ModuleException; |
|
13
|
|
|
use kalanis\kw_modules\Parser; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Processor |
|
18
|
|
|
* @package kalanis\kw_modules\Mixer |
|
19
|
|
|
* Process modules together |
|
20
|
|
|
*/ |
|
21
|
|
|
class Processor |
|
22
|
|
|
{ |
|
23
|
|
|
protected ILoader $loader; |
|
24
|
|
|
protected IModulesList $files; |
|
25
|
|
|
protected Parser\GetModules $parser; |
|
26
|
|
|
protected PassedParams\Factory $paramsFactory; |
|
27
|
|
|
|
|
28
|
17 |
|
public function __construct(ILoader $loader, IModulesList $files, ?IMdTranslations $lang = null, ?Parser\GetModules $parser = null) |
|
29
|
|
|
{ |
|
30
|
17 |
|
$this->loader = $loader; |
|
31
|
17 |
|
$this->files = $files; |
|
32
|
17 |
|
$this->parser = $parser ?: new Parser\GetModules($lang); |
|
33
|
17 |
|
$this->paramsFactory = new PassedParams\Factory($lang); |
|
34
|
17 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $content |
|
38
|
|
|
* @param IFiltered $inputs |
|
39
|
|
|
* @param int $level |
|
40
|
|
|
* @param array<int, string|int|float|bool> $sharedParams |
|
41
|
|
|
* @param array<string, string|int|float|bool|object> $constructParams params passed into __construct |
|
42
|
|
|
* @throws ModuleException |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function fill(string $content, IFiltered $inputs, int $level, array $sharedParams = [], array $constructParams = []): string |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->files->setModuleLevel($level); |
|
48
|
3 |
|
$this->parser->setContent($content)->process(); |
|
49
|
|
|
|
|
50
|
|
|
/** @var ModulesLists\Record[] $availableModules */ |
|
51
|
3 |
|
$availableModules = $this->files->listing(); |
|
52
|
3 |
|
$willChange = []; |
|
53
|
|
|
// in child there can be available only modules filtered by their name - process only allowed ones, let others be |
|
54
|
3 |
|
foreach ($this->parser->getFoundModules() as $item) { |
|
55
|
2 |
|
if (isset($availableModules[$item->getModuleName()])) { |
|
56
|
|
|
// known |
|
57
|
2 |
|
if (!$availableModules[$item->getModuleName()]->isEnabled()) { |
|
58
|
|
|
// disabled -> no content to render |
|
59
|
1 |
|
$willChange[] = $item; |
|
60
|
1 |
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// known, enabled -> will process |
|
64
|
2 |
|
$module = $this->loader->load($item->getModulePath(), $constructParams); |
|
65
|
2 |
|
if ($module) { |
|
66
|
2 |
|
$module->init($inputs, array_merge( |
|
67
|
2 |
|
$sharedParams, |
|
68
|
2 |
|
$availableModules[$item->getModuleName()]->getParams(), |
|
69
|
2 |
|
$this->paramsFactory->getClass($module)->change($item->getContent()), |
|
70
|
2 |
|
[ISitePart::KEY_LEVEL => $level] |
|
71
|
|
|
)); |
|
72
|
2 |
|
$module->process(); |
|
73
|
2 |
|
$item->setWhatWillReplace($module->output()->output()); |
|
74
|
2 |
|
$willChange[] = $item; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
// now exchange data in $content |
|
80
|
3 |
|
$toChange = (array) array_combine(array_map([$this, 'mapChangeFrom'], $willChange), array_map([$this, 'mapChangeTo'], $willChange)); |
|
81
|
3 |
|
return strtr($content, $toChange); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
public function mapChangeFrom(Parser\Record $record): string |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $record->getToChange(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public function mapChangeTo(Parser\Record $record): string |
|
90
|
|
|
{ |
|
91
|
2 |
|
return $record->getWillReplace(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|