PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / htmlburger / wpemerge / src / View / ViewService.php

ViewService.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/htmlburger/wpemerge/src/View/ViewService.php

228 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmerge
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2019 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\HandlerFactory;
15 use WPEmerge\Helpers\MixedType;
16
17 /**
18 * Provide general view-related functionality.
19 */
20 class ViewService {
21 /**
22 * Configuration.
23 *
24 * @var array<string, mixed>
25 */
26 protected $config = [];
27
28 /**
29 * View engine.
30 *
31 * @var ViewEngineInterface
32 */
33 protected $engine = null;
34
35 /**
36 * Handler factory.
37 *
38 * @var HandlerFactory
39 */
40 protected $handler_factory = null;
41
42 /**
43 * Global variables.
44 *
45 * @var array
46 */
47 protected $globals = [];
48
49 /**
50 * View composers.
51 *
52 * @var array
53 */
54 protected $composers = [];
55
56 /**
57 * Constructor.
58 *
59 * @codeCoverageIgnore
60 * @param array<string, mixed> $config
61 * @param ViewEngineInterface $engine
62 * @param HandlerFactory $handler_factory
63 */
64 public function __construct( $config, ViewEngineInterface $engine, HandlerFactory $handler_factory ) {
65 $this->config = $config;
66 $this->engine = $engine;
67 $this->handler_factory = $handler_factory;
68 }
69
70 /**
71 * Get global variables.
72 *
73 * @return array
74 */
75 public function getGlobals() {
76 return $this->globals;
77 }
78
79 /**
80 * Set a global variable.
81 *
82 * @param string $key
83 * @param mixed $value
84 * @return void
85 */
86 public function addGlobal( $key, $value ) {
87 $this->globals[ $key ] = $value;
88 }
89
90 /**
91 * Set an array of global variables.
92 *
93 * @param array $globals
94 * @return void
95 */
96 public function addGlobals( $globals ) {
97 foreach ( $globals as $key => $value ) {
98 $this->addGlobal( $key, $value );
99 }
100 }
101
102 /**
103 * Get view composer.
104 *
105 * @param string $view
106 * @return Handler[]
107 */
108 public function getComposersForView( $view ) {
109 $view = $this->engine->canonical( $view );
110
111 $composers = [];
112
113 foreach ( $this->composers as $composer ) {
114 if ( in_array( $view, $composer['views'], true ) ) {
115 $composers[] = $composer['composer'];
116 }
117 }
118
119 return $composers;
120 }
121
122 /**
123 * Add view composer.
124 *
125 * @param string|string[] $views
126 * @param string|Closure $composer
127 * @return void
128 */
129 public function addComposer( $views, $composer ) {
130 $views = array_map( function ( $view ) {
131 return $this->engine->canonical( $view );
132 }, MixedType::toArray( $views ) );
133
134 $handler = $this->handler_factory->make( $composer, 'compose', $this->config['namespace'] );
135
136 $this->composers[] = [
137 'views' => $views,
138 'composer' => $handler,
139 ];
140 }
141
142 /**
143 * Composes a view instance with contexts in the following order: Global, Composers, Local.
144 *
145 * @param ViewInterface $view
146 * @return void
147 */
148 public function compose( ViewInterface $view ) {
149 $global = ['global' => $this->getGlobals()];
150 $local = $view->getContext();
151
152 $view->with( $global );
153
154 $composers = $this->getComposersForView( $view->getName() );
155 foreach ( $composers as $composer ) {
156 $composer->execute( $view );
157 }
158
159 $view->with( $local );
160 }
161
162 /**
163 * Check if a view exists.
164 *
165 * @param string $view
166 * @return boolean
167 */
168 public function exists( $view ) {
169 return $this->engine->exists( $view );
170 }
171
172 /**
173 * Return a canonical string representation of the view name.
174 *
175 * @param string $view
176 * @return string
177 */
178 public function canonical( $view ) {
179 return $this->engine->canonical( $view );
180 }
181
182 /**
183 * Create a view instance from the first view name that exists.
184 *
185 * @param string|string[] $views
186 * @return ViewInterface
187 */
188 public function make( $views ) {
189 return $this->engine->make( MixedType::toArray( $views ) );
190 }
191
192 /**
193 * Trigger core hooks for a partial, if any.
194 *
195 * @codeCoverageIgnore
196 * @param string $name
197 * @return void
198 */
199 public function triggerPartialHooks( $name ) {
200 if ( ! function_exists( 'apply_filters' ) ) {
201 // We are not in a WordPress environment - skip triggering hooks.
202 return;
203 }
204
205 $core_partial = '/^(header|sidebar|footer)(?:-(.*?))?(\.|$)/i';
206 $matches = [];
207 $is_partial = preg_match( $core_partial, $name, $matches );
208
209 if ( $is_partial && apply_filters( "wpemerge.partials.{$matches[1]}.hook", true ) ) {
210 do_action( "get_{$matches[1]}", $matches[2] );
211 }
212 }
213
214 /**
215 * Render a view.
216 *
217 * @codeCoverageIgnore
218 * @param string|string[] $views
219 * @param array<string, mixed> $context
220 * @return void
221 */
222 public function render( $views, $context = [] ) {
223 $view = $this->make( $views )->with( $context );
224 $this->triggerPartialHooks( $view->getName() );
225 echo $view->toString();
226 }
227 }
228