1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Views; |
13
|
|
|
|
14
|
|
|
use Spiral\Views\Context\ValueDependency; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ContextGenerator creates all possible variations of context values. Use this class |
18
|
|
|
* to properly warm up views cache. |
19
|
|
|
*/ |
20
|
|
|
final class ContextGenerator |
21
|
|
|
{ |
22
|
|
|
/** @var ContextInterface */ |
23
|
|
|
private $context; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ContextInterface $context |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ContextInterface $context) |
29
|
|
|
{ |
30
|
|
|
$this->context = $context; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generate all possible context variations. |
35
|
|
|
* |
36
|
|
|
* @return ContextInterface[] |
37
|
|
|
*/ |
38
|
|
|
public function generate(): array |
39
|
|
|
{ |
40
|
|
|
$dependencies = $this->context->getDependencies(); |
41
|
|
|
|
42
|
|
|
return $this->rotate(new ViewContext(), $dependencies); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Rotate all possible context values using recursive tree walk. |
47
|
|
|
* |
48
|
|
|
* @param ContextInterface $context |
49
|
|
|
* @param DependencyInterface[] $dependencies |
50
|
|
|
* |
51
|
|
|
* @return ContextInterface[] |
52
|
|
|
*/ |
53
|
|
|
private function rotate(ContextInterface $context, array $dependencies): array |
54
|
|
|
{ |
55
|
|
|
if (empty($dependencies)) { |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$top = array_shift($dependencies); |
60
|
|
|
|
61
|
|
|
$variants = []; |
62
|
|
|
foreach ($top->getVariants() as $value) { |
63
|
|
|
$variant = $context->withDependency(new ValueDependency($top->getName(), $value)); |
64
|
|
|
|
65
|
|
|
if (empty($dependencies)) { |
66
|
|
|
$variants[] = $variant; |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($this->rotate($variant, $dependencies) as $inner) { |
71
|
|
|
$variants[] = $inner; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $variants; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|