1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WPEmerge\View; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use WPEmerge\Facades\ViewEngine; |
7
|
|
|
use WPEmerge\Helpers\Handler; |
8
|
|
|
use WPEmerge\Helpers\MixedType; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provide general view-related functionality. |
12
|
|
|
*/ |
13
|
|
|
class ViewService { |
14
|
|
|
/** |
15
|
|
|
* Global variables. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $globals = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* View composers. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $composers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get global variables. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
2 |
|
public function getGlobals() { |
34
|
2 |
|
return $this->globals; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set a global variable. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @param mixed $value |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
1 |
|
public function addGlobal( $key, $value ) { |
46
|
1 |
|
$this->globals[ $key ] = $value; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set an array of global variables. |
51
|
|
|
* |
52
|
|
|
* @param array $globals |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
1 |
|
public function addGlobals( $globals ) { |
56
|
1 |
|
foreach ( $globals as $key => $value ) { |
57
|
1 |
|
$this->addGlobal( $key, $value ); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get view composer. |
63
|
|
|
* |
64
|
|
|
* @param string $view |
65
|
|
|
* @return Handler[] |
66
|
|
|
*/ |
67
|
1 |
|
public function getComposersForView( $view ) { |
68
|
1 |
|
$view = ViewEngine::canonical( $view ); |
69
|
|
|
|
70
|
1 |
|
$composers = []; |
71
|
|
|
|
72
|
1 |
|
foreach ( $this->composers as $composer ) { |
73
|
1 |
|
if ( in_array( $view, $composer['views'] ) ) { |
74
|
1 |
|
$composers[] = $composer['composer']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $composers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add view composer. |
83
|
|
|
* |
84
|
|
|
* @param string|string[] $views |
85
|
|
|
* @param string|Closure $composer |
86
|
|
|
* @return void |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function addComposer( $views, $composer ) { |
90
|
1 |
|
$views = array_map( function( $view ) { |
91
|
1 |
|
return ViewEngine::canonical( $view ); |
92
|
1 |
|
}, MixedType::toArray( $views ) ); |
93
|
1 |
|
$handler = new Handler( $composer, 'compose' ); |
94
|
|
|
|
95
|
1 |
|
$this->composers[] = [ |
96
|
1 |
|
'views' => $views, |
97
|
1 |
|
'composer' => $handler, |
98
|
|
|
]; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the composed context for a view. |
103
|
|
|
* Passes all arguments to the composer. |
104
|
|
|
* |
105
|
|
|
* @param ViewInterface $view |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
1 |
|
public function compose( ViewInterface $view ) { |
109
|
1 |
|
$composers = $this->getComposersForView( $view->getName() ); |
110
|
|
|
|
111
|
1 |
|
foreach ( $composers as $composer ) { |
112
|
1 |
|
$composer->execute( $view ); |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create a view instance. |
118
|
|
|
* |
119
|
|
|
* @param string|string[] $views |
120
|
|
|
* @return ViewInterface |
121
|
|
|
*/ |
122
|
1 |
|
public function make( $views ) { |
123
|
1 |
|
$views = MixedType::toArray( $views ); |
124
|
1 |
|
return ViewEngine::make( $views ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|