← All changes
|
vendor/wpfluent/framework/src/WPFluent/View/View.php
+133
-48
1.21
→
2.1.0
View file →
| @@ -2,14 +2,15 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentBoards\Framework\View; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | +use LogicException; | |
| 6 | 7 | |
| 7 | 8 | class View |
| 8 | 9 | { |
| 9 | 10 | /** |
| 10 | 11 | * Application Instance |
| 11 | - * @var FluentBoards\Framework\Foundation\Application | |
| 12 | + * @var \FluentBoards\Framework\Foundation\Application $app | |
| 12 | 13 | */ |
| 13 | 14 | protected $app; |
| 14 | 15 | |
| 15 | 16 | /** |
| @@ -32,9 +33,9 @@ | ||
| 32 | 33 | protected static $sharedData = []; |
| 33 | 34 | |
| 34 | 35 | /** |
| 35 | 36 | * Construct the view instamce |
| 36 | - * @param [type] $app [description] | |
| 37 | + * @param \FluentBoards\Framework\Foundation\Application $app | |
| 37 | 38 | */ |
| 38 | 39 | public function __construct($app) |
| 39 | 40 | { |
| 40 | 41 | $this->app = $app; |
| @@ -61,36 +62,23 @@ | ||
| 61 | 62 | */ |
| 62 | 63 | public function make($path, $data = []) |
| 63 | 64 | { |
| 64 | 65 | $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 | 66 | |
| 71 | - if (strpos($path, ':')) { | |
| 72 | - return $this->loadViewFrom($path, $data); | |
| 67 | + // If full path is given, then just use it | |
| 68 | + if (file_exists($this->path = $path . '.php')) { | |
| 69 | + return $this->prepareData($data); | |
| 73 | 70 | } |
| 74 | 71 | |
| 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 | - } | |
| 72 | + if (strpos($path, ':')) { | |
| 73 | + return $this->loadViewFrom($path, $data); | |
| 74 | + } | |
| 81 | 75 | |
| 82 | - if (file_exists($this->path = ($path . '.php'))) { | |
| 83 | - $this->data = array_merge($this->data, static::$sharedData, $data); | |
| 84 | - return $this; | |
| 76 | + if (file_exists($this->resolveFilePath($path))) { | |
| 77 | + return $this->prepareData($data); | |
| 85 | 78 | } |
| 86 | 79 | |
| 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!"); | |
| 80 | + $this->handleViewNotFoundException($this->path); | |
| 93 | 81 | } |
| 94 | 82 | |
| 95 | 83 | /** |
| 96 | 84 | * Load view from specified location. |
| @@ -103,28 +91,53 @@ | ||
| 103 | 91 | public function loadViewFrom($path, $data) |
| 104 | 92 | { |
| 105 | 93 | $pieces = explode(':', $path); |
| 106 | 94 | |
| 107 | - $ns = reset($pieces); | |
| 95 | + $pluginRoot = reset($pieces); | |
| 108 | 96 | |
| 109 | - if (is_dir($root = WP_PLUGIN_DIR . '/' . $ns)) { | |
| 97 | + if (is_dir($root = WP_PLUGIN_DIR . '/' . $pluginRoot)) { | |
| 98 | + | |
| 99 | + if (is_dir($views = $root . '/app/Views')) { | |
| 110 | 100 | |
| 111 | - if (is_dir($views = $root . '/app/Views')) { | |
| 101 | + $path = $views . '/' . end($pieces); | |
| 102 | + | |
| 103 | + $path = str_replace('.', DIRECTORY_SEPARATOR, $path); | |
| 112 | 104 | |
| 113 | - $path = $views . '/' . end($pieces); | |
| 114 | - | |
| 115 | - $path = str_replace('.', DIRECTORY_SEPARATOR, $path); | |
| 105 | + if (file_exists($this->path = ($path . '.php'))) { | |
| 106 | + return $this->prepareData($data); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + $this->handleViewNotFoundException($path); | |
| 112 | + } | |
| 116 | 113 | |
| 117 | - if (file_exists($this->path = ($path . '.php'))) { | |
| 118 | - $this->data = array_merge($this->data, static::$sharedData, $data); | |
| 119 | - return $this; | |
| 120 | - } | |
| 121 | - } | |
| 122 | - } | |
| 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 | + } | |
| 123 | 124 | |
| 124 | - throw new Exception("The view file [{$this->path}] doesn't exists!"); | |
| 125 | - } | |
| 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 | + ); | |
| 126 | 136 | |
| 137 | + return $this; | |
| 138 | + } | |
| 139 | + | |
| 127 | 140 | /** |
| 128 | 141 | * Resolve the view file path |
| 129 | 142 | * @param string $path |
| 130 | 143 | * @return string |
| @@ -132,15 +145,14 @@ | ||
| 132 | 145 | protected function resolveFilePath($path) |
| 133 | 146 | { |
| 134 | 147 | $path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
| 135 | 148 | |
| 136 | - return $this->app['path.views'] . $path .'.php'; | |
| 149 | + return $this->path = $this->app['path.views'] . $path .'.php'; | |
| 137 | 150 | } |
| 138 | 151 | |
| 139 | 152 | /** |
| 140 | 153 | * Evaluate the view file |
| 141 | - * @param string $path | |
| 142 | - * @param string $data | |
| 154 | + * @param \FluentBoards\Framework\Foundation\Application $app | |
| 143 | 155 | * @return $this |
| 144 | 156 | */ |
| 145 | 157 | protected function renderContent($app) |
| 146 | 158 | { |
| @@ -145,12 +157,26 @@ | ||
| 145 | 157 | protected function renderContent($app) |
| 146 | 158 | { |
| 147 | 159 | ob_start(); |
| 148 | 160 | extract($this->data, EXTR_SKIP); |
| 149 | - require $this->path; | |
| 161 | + require $this->getNormalizedPath(); | |
| 150 | 162 | return ltrim(ob_get_clean()); |
| 151 | 163 | } |
| 152 | 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 | + | |
| 153 | 179 | /** |
| 154 | 180 | * Share global data for any view |
| 155 | 181 | * @param string $key |
| 156 | 182 | * @param mixed $value |
| @@ -161,10 +187,20 @@ | ||
| 161 | 187 | static::$sharedData[$key] = $value; |
| 162 | 188 | } |
| 163 | 189 | |
| 164 | 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 | + /** | |
| 165 | 201 | * Provides a fluent interface to set data |
| 166 | - * @param mixed $key | |
| 202 | + * @param array|string $name | |
| 167 | 203 | * @param mixed $data |
| 168 | 204 | * @return $this |
| 169 | 205 | */ |
| 170 | 206 | public function with($name, $data = []) |
| @@ -180,10 +216,11 @@ | ||
| 180 | 216 | return $this; |
| 181 | 217 | } |
| 182 | 218 | |
| 183 | 219 | /** |
| 184 | - * Set view path (used for micro) | |
| 185 | - * @param [type] $path [description] | |
| 220 | + * Set view path (used for micro). | |
| 221 | + * | |
| 222 | + * @param string $path | |
| 186 | 223 | */ |
| 187 | 224 | public function setViewPath($path) |
| 188 | 225 | { |
| 189 | 226 | $this->app['path.views'] = $path; |
| @@ -189,11 +226,21 @@ | ||
| 189 | 226 | $this->app['path.views'] = $path; |
| 190 | 227 | } |
| 191 | 228 | |
| 192 | 229 | /** |
| 193 | - * Getter for the view | |
| 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 | + * | |
| 194 | 242 | * @param string $key |
| 195 | - * @param mixed $value | |
| 196 | 243 | */ |
| 197 | 244 | public function __get($key) |
| 198 | 245 | { |
| 199 | 246 | if (isset($this->data[$key])) { |
| @@ -211,9 +258,20 @@ | ||
| 211 | 258 | $this->data[$key] = $value; |
| 212 | 259 | } |
| 213 | 260 | |
| 214 | 261 | /** |
| 215 | - * Dump the view result | |
| 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 | + * | |
| 216 | 274 | * @return string |
| 217 | 275 | */ |
| 218 | 276 | public function __toString() |
| 219 | 277 | { |
| @@ -218,5 +276,32 @@ | ||
| 218 | 276 | public function __toString() |
| 219 | 277 | { |
| 220 | 278 | return $this->renderContent($this->app); |
| 221 | 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 | + } | |
| 222 | 307 | } |