| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\View; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LogicException; |
| 7 |
|
| 8 |
class View |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Application Instance |
| 12 |
* @var \FluentCommunity\Framework\Foundation\Application $app |
| 13 |
*/ |
| 14 |
protected $app; |
| 15 |
|
| 16 |
/** |
| 17 |
* View file path |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $path; |
| 22 |
|
| 23 |
/** |
| 24 |
* View data |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $data = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Shared data inall the views |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected static $sharedData = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* Construct the view instamce |
| 37 |
* @param \FluentCommunity\Framework\Foundation\Application $app |
| 38 |
*/ |
| 39 |
public function __construct($app) |
| 40 |
{ |
| 41 |
$this->app = $app; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Generate and echo/print a view file |
| 46 |
* @param string $path |
| 47 |
* @param array $data |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function render($path, $data = []) |
| 51 |
{ |
| 52 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 53 |
echo $this->make($path, $data); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Generate a view file |
| 58 |
* @param string $path |
| 59 |
* @param array $data |
| 60 |
* @return string [generated html] |
| 61 |
* @throws Exception |
| 62 |
*/ |
| 63 |
public function make($path, $data = []) |
| 64 |
{ |
| 65 |
$path = str_replace('.php', '', $path); |
| 66 |
|
| 67 |
// If full path is given, then just use it |
| 68 |
if (file_exists($this->path = $path . '.php')) { |
| 69 |
return $this->prepareData($data); |
| 70 |
} |
| 71 |
|
| 72 |
if (strpos($path, ':')) { |
| 73 |
return $this->loadViewFrom($path, $data); |
| 74 |
} |
| 75 |
|
| 76 |
if (file_exists($this->resolveFilePath($path))) { |
| 77 |
return $this->prepareData($data); |
| 78 |
} |
| 79 |
|
| 80 |
$this->handleViewNotFoundException($this->path); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Load view from specified location. |
| 85 |
* |
| 86 |
* @param string $path |
| 87 |
* @param array $data |
| 88 |
* @return string |
| 89 |
* @throws Exception |
| 90 |
*/ |
| 91 |
public function loadViewFrom($path, $data) |
| 92 |
{ |
| 93 |
$pieces = explode(':', $path); |
| 94 |
|
| 95 |
$pluginRoot = reset($pieces); |
| 96 |
|
| 97 |
if (is_dir($root = WP_PLUGIN_DIR . '/' . $pluginRoot)) { |
| 98 |
|
| 99 |
if (is_dir($views = $root . '/app/Views')) { |
| 100 |
|
| 101 |
$path = $views . '/' . end($pieces); |
| 102 |
|
| 103 |
$path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 104 |
|
| 105 |
if (file_exists($this->path = ($path . '.php'))) { |
| 106 |
return $this->prepareData($data); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->handleViewNotFoundException($path); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Throw view not found exception. |
| 116 |
* |
| 117 |
* @return void |
| 118 |
* @throws \Exception |
| 119 |
*/ |
| 120 |
protected function handleViewNotFoundException($path) |
| 121 |
{ |
| 122 |
throw new Exception("The view file [{$path}] doesn't exists!"); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Prepare data for view. |
| 127 |
* |
| 128 |
* @param array $data |
| 129 |
* @return self |
| 130 |
*/ |
| 131 |
protected function prepareData($data) |
| 132 |
{ |
| 133 |
$this->data = array_merge( |
| 134 |
$this->data, static::$sharedData, $data |
| 135 |
); |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Resolve the view file path |
| 142 |
* @param string $path |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
protected function resolveFilePath($path) |
| 146 |
{ |
| 147 |
$path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 148 |
|
| 149 |
return $this->path = $this->app['path.views'] . $path .'.php'; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Evaluate the view file |
| 154 |
* @param \FluentCommunity\Framework\Foundation\Application $app |
| 155 |
* @return $this |
| 156 |
*/ |
| 157 |
protected function renderContent($app) |
| 158 |
{ |
| 159 |
ob_start(); |
| 160 |
extract($this->data, EXTR_SKIP); |
| 161 |
require $this->getNormalizedPath(); |
| 162 |
return ltrim(ob_get_clean()); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get normalized path. |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
protected function getNormalizedPath() |
| 171 |
{ |
| 172 |
$ds = DIRECTORY_SEPARATOR; |
| 173 |
|
| 174 |
$path = str_replace($ds.$ds, $ds, $this->path); |
| 175 |
|
| 176 |
return $path; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Share global data for any view |
| 181 |
* @param string $key |
| 182 |
* @param mixed $value |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
public function share($key, $value) |
| 186 |
{ |
| 187 |
static::$sharedData[$key] = $value; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Reset all shared view data (useful for test isolation). |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public static function resetSharedData() |
| 196 |
{ |
| 197 |
static::$sharedData = []; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Provides a fluent interface to set data |
| 202 |
* @param array|string $name |
| 203 |
* @param mixed $data |
| 204 |
* @return $this |
| 205 |
*/ |
| 206 |
public function with($name, $data = []) |
| 207 |
{ |
| 208 |
if (is_array($name)) { |
| 209 |
foreach ($name as $key => $value) { |
| 210 |
$this->__set($key, $value); |
| 211 |
} |
| 212 |
} else { |
| 213 |
$this->__set($name, $data); |
| 214 |
} |
| 215 |
|
| 216 |
return $this; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Set view path (used for micro). |
| 221 |
* |
| 222 |
* @param string $path |
| 223 |
*/ |
| 224 |
public function setViewPath($path) |
| 225 |
{ |
| 226 |
$this->app['path.views'] = $path; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get the resolved view path. |
| 231 |
* |
| 232 |
* @return string |
| 233 |
*/ |
| 234 |
public function getResolvedPath() |
| 235 |
{ |
| 236 |
return $this->path; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Getter for the view. |
| 241 |
* |
| 242 |
* @param string $key |
| 243 |
*/ |
| 244 |
public function __get($key) |
| 245 |
{ |
| 246 |
if (isset($this->data[$key])) { |
| 247 |
return $this->data[$key]; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Setter for the view |
| 253 |
* @param string $key |
| 254 |
* @param mixed $value |
| 255 |
*/ |
| 256 |
public function __set($key, $value) |
| 257 |
{ |
| 258 |
$this->data[$key] = $value; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Return the view data. |
| 263 |
* |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
public function toArray() |
| 267 |
{ |
| 268 |
return $this->data; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Dump the view result. |
| 273 |
* |
| 274 |
* @return string |
| 275 |
*/ |
| 276 |
public function __toString() |
| 277 |
{ |
| 278 |
return $this->renderContent($this->app); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Prevent unserialization (legacy PHP <7.4) |
| 283 |
*/ |
| 284 |
public function __wakeup() |
| 285 |
{ |
| 286 |
$this->handleUnserializeNotAllowedException(); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Prevent unserialization (PHP >=7.4) |
| 291 |
*/ |
| 292 |
public function __unserialize(array $data) |
| 293 |
{ |
| 294 |
$this->handleUnserializeNotAllowedException(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Handle unserialization error. |
| 299 |
* |
| 300 |
* @return void |
| 301 |
* @throws \LogicException |
| 302 |
*/ |
| 303 |
protected function handleUnserializeNotAllowedException() |
| 304 |
{ |
| 305 |
throw new LogicException(static::class . ' cannot be unserialized.'); |
| 306 |
} |
| 307 |
} |
| 308 |
|