PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.3
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / View / View.php

View.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.3, at vendor/wpfluent/framework/src/WPFluent/View/View.php

172 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Framework\View;
4
5 use Exception;
6
7 class View
8 {
9 /**
10 * Application Instance
11 * @var FluentSupport\Framework\Foundation\Application
12 */
13 protected $app;
14
15 /**
16 * View file path
17 *
18 * @var string
19 */
20 protected $path;
21
22 /**
23 * View data
24 * @var array
25 */
26 protected $data = [];
27
28 /**
29 * Shared data inall the views
30 * @var array
31 */
32 protected static $sharedData = [];
33
34 /**
35 * Construct the view instamce
36 * @param [type] $app [description]
37 */
38 public function __construct($app)
39 {
40 $this->app = $app;
41 }
42
43 /**
44 * Generate and echo/print a view file
45 * @param string $path
46 * @param array $data
47 * @return void
48 */
49 public function render($path, $data = [])
50 {
51 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
52 echo $this->make($path, $data);
53 }
54
55 /**
56 * Generate a view file
57 * @param string $path
58 * @param array $data
59 * @return string [generated html]
60 * @throws Exception
61 */
62 public function make($path, $data = [])
63 {
64 if (file_exists($this->path = $this->resolveFilePath($path))) {
65 $this->data = $data;
66 return $this;
67 }
68
69 throw new Exception("The view file [{$this->path}] doesn't exists!");
70 }
71
72 /**
73 * Resolve the view file path
74 * @param string $path
75 * @return string
76 */
77 protected function resolveFilePath($path)
78 {
79 $path = str_replace('.', DIRECTORY_SEPARATOR, $path);
80
81 return $this->app['path.views'] . $path .'.php';
82 }
83
84 /**
85 * Evaluate the view file
86 * @param string $path
87 * @param string $data
88 * @return $this
89 */
90 protected function renderContent()
91 {
92 $renderOutput = function($app) {
93 ob_start() && extract(
94 $this->gatherData(), EXTR_SKIP
95 );
96
97 include $this->path;
98
99 return ltrim(ob_get_clean());
100 };
101
102 return $renderOutput($this->app);
103 }
104
105 /**
106 * Gether shared & view data
107 * @return array
108 */
109 protected function gatherData()
110 {
111 return array_merge(static::$sharedData, $this->data);
112 }
113
114 /**
115 * Share global data for any view
116 * @param string $key
117 * @param mixed $value
118 * @return void
119 */
120 public function share($key, $value)
121 {
122 static::$sharedData[$key] = $value;
123 }
124
125 /**
126 * Provides a fluent interface to set data
127 * @param mixed $key
128 * @param mixed $data
129 * @return $this
130 */
131 public function with($name, $data = [])
132 {
133 if (is_array($name)) {
134 foreach ($name as $key => $value) {
135 $this->__set($key, $value);
136 }
137 } else {
138 $this->__set($name, $data);
139 }
140
141 return $this;
142 }
143
144 /**
145 * Set view path (used for micro)
146 * @param [type] $path [description]
147 */
148 public function setViewPath($path)
149 {
150 $this->app['path.views'] = $path;
151 }
152
153 /**
154 * Setter for the view
155 * @param string $key
156 * @param mixed $value
157 */
158 public function __set($key, $value)
159 {
160 $this->data[$key] = $value;
161 }
162
163 /**
164 * Dump the view result
165 * @return string
166 */
167 public function __toString()
168 {
169 return $this->renderContent();
170 }
171 }
172