|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package WPEmerge |
|
4
|
|
|
* @author Atanas Angelov <[email protected]> |
|
5
|
|
|
* @copyright 2018 Atanas Angelov |
|
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
|
7
|
|
|
* @link https://wpemerge.com/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WPEmerge\View; |
|
11
|
|
|
|
|
12
|
|
|
use Closure; |
|
13
|
|
|
use WPEmerge\Helpers\Handler; |
|
14
|
|
|
use WPEmerge\Helpers\MixedType; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide general view-related functionality. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ViewService { |
|
20
|
|
|
/** |
|
21
|
|
|
* View engine. |
|
22
|
|
|
* |
|
23
|
|
|
* @var ViewEngineInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $engine = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Global variables. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $globals = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* View composers. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $composers = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @codeCoverageIgnore |
|
45
|
|
|
* @param ViewEngineInterface $engine |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( $engine ) { |
|
48
|
|
|
$this->engine = $engine; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get global variables. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getGlobals() { |
|
57
|
2 |
|
return $this->globals; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set a global variable. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @param mixed $value |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function addGlobal( $key, $value ) { |
|
68
|
1 |
|
$this->globals[ $key ] = $value; |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set an array of global variables. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $globals |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function addGlobals( $globals ) { |
|
78
|
1 |
|
foreach ( $globals as $key => $value ) { |
|
79
|
1 |
|
$this->addGlobal( $key, $value ); |
|
80
|
|
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get view composer. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $view |
|
87
|
|
|
* @return array<Handler> |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getComposersForView( $view ) { |
|
90
|
1 |
|
$view = $this->engine->canonical( $view ); |
|
91
|
|
|
|
|
92
|
1 |
|
$composers = []; |
|
93
|
|
|
|
|
94
|
1 |
|
foreach ( $this->composers as $composer ) { |
|
95
|
1 |
|
if ( in_array( $view, $composer['views'], true ) ) { |
|
96
|
1 |
|
$composers[] = $composer['composer']; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $composers; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add view composer. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string|array<string> $views |
|
107
|
|
|
* @param string|Closure $composer |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function addComposer( $views, $composer ) { |
|
111
|
1 |
|
$views = array_map( function ( $view ) { |
|
112
|
1 |
|
return $this->engine->canonical( $view ); |
|
113
|
1 |
|
}, MixedType::toArray( $views ) ); |
|
114
|
|
|
|
|
115
|
1 |
|
$handler = new Handler( $composer, 'compose', '\\App\\ViewComposers\\' ); |
|
116
|
|
|
|
|
117
|
1 |
|
$this->composers[] = [ |
|
118
|
1 |
|
'views' => $views, |
|
119
|
1 |
|
'composer' => $handler, |
|
120
|
|
|
]; |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Composes a view instance with contexts in the following order: Global, Composers, Local. |
|
125
|
|
|
* |
|
126
|
|
|
* @param ViewInterface $view |
|
127
|
|
|
* @return void |
|
128
|
|
|
*/ |
|
129
|
4 |
|
public function compose( ViewInterface $view ) { |
|
130
|
4 |
|
$global = ['global' => $this->getGlobals()]; |
|
131
|
4 |
|
$local = $view->getContext(); |
|
132
|
|
|
|
|
133
|
4 |
|
$view->with( $global ); |
|
134
|
|
|
|
|
135
|
4 |
|
$composers = $this->getComposersForView( $view->getName() ); |
|
136
|
4 |
|
foreach ( $composers as $composer ) { |
|
137
|
3 |
|
$composer->execute( $view ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
4 |
|
$view->with( $local ); |
|
141
|
4 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Create a view instance. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string|array<string> $views |
|
147
|
|
|
* @return ViewInterface |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function make( $views ) { |
|
150
|
1 |
|
return $this->engine->make( MixedType::toArray( $views ) ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Trigger core hooks for a partial, if any. |
|
155
|
|
|
* |
|
156
|
|
|
* @codeCoverageIgnore |
|
157
|
|
|
* @param string $name |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function triggerPartialHooks( $name ) { |
|
161
|
|
|
$core_partial = '/^(header|sidebar|footer)(?:-(.*?))?(\.|$)/i'; |
|
162
|
|
|
if ( preg_match( $core_partial, $name, $matches ) && apply_filters( "wpemerge.partials.{$matches[1]}.hook", true ) ) { |
|
163
|
|
|
do_action( "get_{$matches[1]}", $matches[2] ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|