PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.5.0
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.5.0
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.0, at framework/View/View.php

227 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 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 try {
68 $this->app[$key] = array_merge(
69 $this->app[$key], [$namespace => $path]
70 );
71 } catch (UnResolveableEntityException $e) {
72 $this->app[$key] = array($namespace => $path);
73 }
74 }
75
76 /**
77 * Generate and echo/print a view file
78 * @param string $path
79 * @param array $data
80 * @return void
81 */
82 public function render($path, $data = [])
83 {
84 echo $this->make($path, $data);
85 }
86
87 /**
88 * Generate a view file
89 * @param string $path
90 * @param array $data
91 * @return string [generated html]
92 * @throws InvalidArgumentException
93 */
94 public function make($path, $data = [])
95 {
96 if (file_exists($this->path = $this->resolveFilePath($path))) {
97 $this->data = $data;
98 return $this;
99 }
100
101 throw new InvalidArgumentException("The view file [{$this->path}] doesn't exists!");
102 }
103
104 /**
105 * Resolve the view file path
106 * @param string $path
107 * @return string
108 */
109 protected function resolveFilePath($path)
110 {
111 if (strpos($path, '::') !== false) {
112 list($namespace, $path) = explode('::', $path);
113 $viewName = $this->app['path.view.extras'][$namespace] .DIRECTORY_SEPARATOR. $path;
114 } else {
115 $viewName = $this->app->viewPath($path);
116 }
117
118 return str_replace('.', DIRECTORY_SEPARATOR, $viewName) . '.php';
119 }
120
121 /**
122 * Evaluate the view file
123 * @param string $path
124 * @param string $data
125 * @return $this
126 */
127 protected function renderContent()
128 {
129 $this->callComposerCallbacks();
130
131 $renderOutput = function($app) {
132 ob_start() && extract(
133 $this->gatherData(), EXTR_SKIP
134 );
135
136 include $this->path;
137
138 return ltrim(ob_get_clean());
139 };
140
141 return $renderOutput($this->app);
142 }
143
144 /**
145 * Call registered composer callbacks for this view
146 * @return void
147 */
148 protected function callComposerCallbacks()
149 {
150 if (array_key_exists($this->path, static::$composerCallbacks)) {
151 foreach (static::$composerCallbacks[$this->path] as $callback) {
152 $this->app->parseHandler($callback)($this);
153 }
154 }
155 }
156
157 /**
158 * Gether shared & view data
159 * @return array
160 */
161 protected function gatherData()
162 {
163 return array_merge(static::$sharedData, $this->data);
164 }
165
166 /**
167 * Share global data for any view
168 * @param string $key
169 * @param mixed $value
170 * @return void
171 */
172 public function share($key, $value)
173 {
174 static::$sharedData[$key] = $value;
175 }
176
177 /**
178 * Register view composer calbacks for specific view
179 * @param string $viewName
180 * @param closure $callback
181 * @return void
182 */
183 public function composer($viewName, $callback)
184 {
185 $path = $this->resolveFilePath($viewName);
186 static::$composerCallbacks[$path][] = $callback;
187 }
188
189 /**
190 * Provides a fluent interface to set data
191 * @param mixed $key
192 * @param mixed $data
193 * @return $this
194 */
195 public function with($name, $data = [])
196 {
197 if (is_array($name)) {
198 foreach ($name as $key => $value) {
199 $this->__set($key, $value);
200 }
201 } else {
202 $this->__set($name, $data);
203 }
204
205 return $this;
206 }
207
208 /**
209 * Setter for the view
210 * @param string $key
211 * @param mixed $value
212 */
213 public function __set($key, $value)
214 {
215 $this->data[$key] = $value;
216 }
217
218 /**
219 * Dump the view result
220 * @return string
221 */
222 public function __toString()
223 {
224 return $this->renderContent();
225 }
226 }
227