| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentMail\Includes\View; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
class View |
| 8 |
{ |
| 9 |
protected $app; |
| 10 |
|
| 11 |
protected $path; |
| 12 |
|
| 13 |
protected $data = []; |
| 14 |
|
| 15 |
protected static $sharedData = []; |
| 16 |
|
| 17 |
public function __construct($app) |
| 18 |
{ |
| 19 |
$this->app = $app; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Generate and echo/print a view file |
| 24 |
* @param string $path |
| 25 |
* @param array $data |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function render($path, $data = []) |
| 29 |
{ |
| 30 |
echo $this->make($path, $data); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Generate a view file |
| 35 |
* @param string $path |
| 36 |
* @param array $data |
| 37 |
* @return string [generated html] |
| 38 |
* @throws Exception |
| 39 |
*/ |
| 40 |
public function make($path, $data = []) |
| 41 |
{ |
| 42 |
if (file_exists($this->path = $this->resolveFilePath($path))) { |
| 43 |
$this->data = $data; |
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
throw new Exception("The view file [{$this->path}] doesn't exists!"); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Resolve the view file path |
| 52 |
* @param string $path |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
protected function resolveFilePath($path) |
| 56 |
{ |
| 57 |
$path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 58 |
|
| 59 |
return $this->app['path.views'] . $path .'.php'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Evaluate the view file |
| 64 |
* @param string $path |
| 65 |
* @param string $data |
| 66 |
* @return $this |
| 67 |
*/ |
| 68 |
protected function renderContent() |
| 69 |
{ |
| 70 |
$renderOutput = function($app) { |
| 71 |
ob_start() && extract( |
| 72 |
$this->gatherData(), EXTR_SKIP |
| 73 |
); |
| 74 |
|
| 75 |
include $this->path; |
| 76 |
|
| 77 |
return ltrim(ob_get_clean()); |
| 78 |
}; |
| 79 |
|
| 80 |
return $renderOutput($this->app); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Gether shared & view data |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
protected function gatherData() |
| 88 |
{ |
| 89 |
return array_merge(static::$sharedData, $this->data); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Share global data for any view |
| 94 |
* @param string $key |
| 95 |
* @param mixed $value |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function share($key, $value) |
| 99 |
{ |
| 100 |
static::$sharedData[$key] = $value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Provides a fluent interface to set data |
| 105 |
* @param mixed $key |
| 106 |
* @param mixed $data |
| 107 |
* @return $this |
| 108 |
*/ |
| 109 |
public function with($name, $data = []) |
| 110 |
{ |
| 111 |
if (is_array($name)) { |
| 112 |
foreach ($name as $key => $value) { |
| 113 |
$this->__set($key, $value); |
| 114 |
} |
| 115 |
} else { |
| 116 |
$this->__set($name, $data); |
| 117 |
} |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Setter for the view |
| 124 |
* @param string $key |
| 125 |
* @param mixed $value |
| 126 |
*/ |
| 127 |
public function __set($key, $value) |
| 128 |
{ |
| 129 |
$this->data[$key] = $value; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Dump the view result |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function __toString() |
| 137 |
{ |
| 138 |
return $this->renderContent(); |
| 139 |
} |
| 140 |
} |
| 141 |
|