| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\View; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
class View |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Application Instance |
| 11 |
* @var FluentCommunity\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 |
$path = str_replace('.php', '', $path); |
| 65 |
|
| 66 |
if (file_exists($this->path = $path .'.php')) { |
| 67 |
$this->data = array_merge($this->data, static::$sharedData, $data); |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
if (strpos($path, ':')) { |
| 72 |
return $this->loadViewFrom($path, $data); |
| 73 |
} |
| 74 |
|
| 75 |
if (strpos($path = str_replace('.', '/', $path), '/') !== false) { |
| 76 |
if (file_exists($this->path = $path .'.php')) { |
| 77 |
$this->data = array_merge($this->data, static::$sharedData, $data); |
| 78 |
return $this; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (file_exists($this->path = ($path . '.php'))) { |
| 83 |
$this->data = array_merge($this->data, static::$sharedData, $data); |
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
if (file_exists($this->path = $this->resolveFilePath($path))) { |
| 88 |
$this->data = array_merge($this->data, static::$sharedData, $data); |
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
throw new Exception("The view file [{$this->path}] doesn't exists!"); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Load view from specified location. |
| 97 |
* |
| 98 |
* @param string $path |
| 99 |
* @param array $data |
| 100 |
* @return string |
| 101 |
* @throws Exception |
| 102 |
*/ |
| 103 |
public function loadViewFrom($path, $data) |
| 104 |
{ |
| 105 |
$pieces = explode(':', $path); |
| 106 |
|
| 107 |
$ns = reset($pieces); |
| 108 |
|
| 109 |
if (is_dir($root = WP_PLUGIN_DIR . '/' . $ns)) { |
| 110 |
|
| 111 |
if (is_dir($views = $root . '/app/Views')) { |
| 112 |
|
| 113 |
$path = $views . '/' . end($pieces); |
| 114 |
|
| 115 |
$path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 116 |
|
| 117 |
if (file_exists($this->path = ($path . '.php'))) { |
| 118 |
$this->data = array_merge($this->data, static::$sharedData, $data); |
| 119 |
return $this; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
throw new Exception("The view file [{$this->path}] doesn't exists!"); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Resolve the view file path |
| 129 |
* @param string $path |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
protected function resolveFilePath($path) |
| 133 |
{ |
| 134 |
$path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 135 |
|
| 136 |
return $this->app['path.views'] . $path .'.php'; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Evaluate the view file |
| 141 |
* @param string $path |
| 142 |
* @param string $data |
| 143 |
* @return $this |
| 144 |
*/ |
| 145 |
protected function renderContent($app) |
| 146 |
{ |
| 147 |
ob_start(); |
| 148 |
extract($this->data, EXTR_SKIP); |
| 149 |
require $this->path; |
| 150 |
return ltrim(ob_get_clean()); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Share global data for any view |
| 155 |
* @param string $key |
| 156 |
* @param mixed $value |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function share($key, $value) |
| 160 |
{ |
| 161 |
static::$sharedData[$key] = $value; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Provides a fluent interface to set data |
| 166 |
* @param mixed $key |
| 167 |
* @param mixed $data |
| 168 |
* @return $this |
| 169 |
*/ |
| 170 |
public function with($name, $data = []) |
| 171 |
{ |
| 172 |
if (is_array($name)) { |
| 173 |
foreach ($name as $key => $value) { |
| 174 |
$this->__set($key, $value); |
| 175 |
} |
| 176 |
} else { |
| 177 |
$this->__set($name, $data); |
| 178 |
} |
| 179 |
|
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Set view path (used for micro) |
| 185 |
* @param [type] $path [description] |
| 186 |
*/ |
| 187 |
public function setViewPath($path) |
| 188 |
{ |
| 189 |
$this->app['path.views'] = $path; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Getter for the view |
| 194 |
* @param string $key |
| 195 |
* @param mixed $value |
| 196 |
*/ |
| 197 |
public function __get($key) |
| 198 |
{ |
| 199 |
if (isset($this->data[$key])) { |
| 200 |
return $this->data[$key]; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Setter for the view |
| 206 |
* @param string $key |
| 207 |
* @param mixed $value |
| 208 |
*/ |
| 209 |
public function __set($key, $value) |
| 210 |
{ |
| 211 |
$this->data[$key] = $value; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Dump the view result |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public function __toString() |
| 219 |
{ |
| 220 |
return $this->renderContent($this->app); |
| 221 |
} |
| 222 |
} |
| 223 |
|