PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.5.3
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.5.3
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / framework / View / View.php

View.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.5.3, at framework/View/View.php

232 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\View;
4
5 use InvalidArgumentException;
6 use FluentForm\Framework\Exception\UnResolveableEntityException;
7
8 class View
9 {
10 /**
11 * The Application (Framework)
12 * @var FluentForm\Framework\Foundation\Application
13 */
14 protected $app;
15
16 /**
17 * Resolved path of view
18 * @var string
19 */
20 protected $path;
21
22
23 /**
24 * Passed data for the view
25 * @var array
26 */
27 protected $data = [];
28
29 /**
30 * Shared data for the view
31 * @var array
32 */
33 protected static $sharedData = [];
34
35 /**
36 * Registered composer callbacks for the view
37 * @var array
38 */
39 protected static $composerCallbacks = [];
40
41 /**
42 * Make an instance of the the class
43 * @param FluentForm\Framework\Foundation\Application $app
44 */
45 public function __construct($app)
46 {
47 $this->app = $app;
48 }
49
50 /**
51 * Add aditional view path mapped by a namespace
52 *
53 * This will allow a developer to add additional view location from any
54 * service provider (basically in booted method), so instead of loading
55 * the view from resources/views directory, one can instruct the framework
56 * to load a view from a different place registerd using following method:
57 * add a namespaced path: $this->app->addNamespace('Public', path('public'));
58 * once it is registered, then one can use: View::make('Public::view_name');
59 *
60 * @param string $namespace
61 * @param string $path
62 */
63 public function addNamespace($namespace, $path)
64 {
65 $key = 'path.view.extras';
66
67 $path = str_replace('.', DIRECTORY_SEPARATOR, $path);
68
69 try {
70 $this->app[$key] = array_merge(
71 $this->app[$key], [$namespace => $path]
72 );
73 } catch (UnResolveableEntityException $e) {
74 $this->app[$key] = array($namespace => $path);
75 }
76 }
77
78 /**
79 * Generate and echo/print a view file
80 * @param string $path
81 * @param array $data
82 * @return void
83 */
84 public function render($path, $data = [])
85 {
86 echo $this->make($path, $data);
87 }
88
89 /**
90 * Generate a view file
91 * @param string $path
92 * @param array $data
93 * @return string [generated html]
94 * @throws InvalidArgumentException
95 */
96 public function make($path, $data = [])
97 {
98 if (file_exists($this->path = $this->resolveFilePath($path))) {
99 $this->data = $data;
100 return $this;
101 }
102
103 throw new InvalidArgumentException("The view file [{$this->path}] doesn't exists!");
104 }
105
106 /**
107 * Resolve the view file path
108 * @param string $path
109 * @return string
110 */
111 protected function resolveFilePath($path)
112 {
113 $path = str_replace('.', DIRECTORY_SEPARATOR, $path);
114
115 if (strpos($path, '::') !== false) {
116 list($namespace, $path) = explode('::', $path);
117 $viewName = $this->app['path.view.extras'][$namespace].DIRECTORY_SEPARATOR.$path;
118 } else {
119 $viewName = $this->app->viewPath($path);
120 }
121
122 return $viewName.'.php';
123 }
124
125 /**
126 * Evaluate the view file
127 * @param string $path
128 * @param string $data
129 * @return $this
130 */
131 protected function renderContent()
132 {
133 $this->callComposerCallbacks();
134
135 $renderOutput = function($app) {
136 ob_start() && extract(
137 $this->gatherData(), EXTR_SKIP
138 );
139
140 include $this->path;
141
142 return ltrim(ob_get_clean());
143 };
144
145 return $renderOutput($this->app);
146 }
147
148 /**
149 * Call registered composer callbacks for this view
150 * @return void
151 */
152 protected function callComposerCallbacks()
153 {
154 if (array_key_exists($this->path, static::$composerCallbacks)) {
155 foreach (static::$composerCallbacks[$this->path] as $callback) {
156 $parser = $this->app->parseHandler($callback);
157 $parser($this);
158 }
159 }
160 }
161
162 /**
163 * Gether shared & view data
164 * @return array
165 */
166 protected function gatherData()
167 {
168 return array_merge(static::$sharedData, $this->data);
169 }
170
171 /**
172 * Share global data for any view
173 * @param string $key
174 * @param mixed $value
175 * @return void
176 */
177 public function share($key, $value)
178 {
179 static::$sharedData[$key] = $value;
180 }
181
182 /**
183 * Register view composer calbacks for specific view
184 * @param string $viewName
185 * @param closure $callback
186 * @return void
187 */
188 public function composer($viewName, $callback)
189 {
190 $path = $this->resolveFilePath($viewName);
191 static::$composerCallbacks[$path][] = $callback;
192 }
193
194 /**
195 * Provides a fluent interface to set data
196 * @param mixed $key
197 * @param mixed $data
198 * @return $this
199 */
200 public function with($name, $data = [])
201 {
202 if (is_array($name)) {
203 foreach ($name as $key => $value) {
204 $this->__set($key, $value);
205 }
206 } else {
207 $this->__set($name, $data);
208 }
209
210 return $this;
211 }
212
213 /**
214 * Setter for the view
215 * @param string $key
216 * @param mixed $value
217 */
218 public function __set($key, $value)
219 {
220 $this->data[$key] = $value;
221 }
222
223 /**
224 * Dump the view result
225 * @return string
226 */
227 public function __toString()
228 {
229 return $this->renderContent();
230 }
231 }
232