1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415:
<?php
namespace Coast;
use Coast\Path;
use Coast\File;
use Coast\Dir;
use Coast\View\Content;
class View implements \Coast\App\Access, \Coast\App\Executable
{
use \Coast\App\Access\Implementation;
use \Coast\App\Executable\Implementation;
protected $_dirs = [];
protected $_extName = 'php';
protected $_partialSeparator = '_';
protected $_contexts = [];
protected $_active;
protected $_extensions = [];
protected $_files = [];
public function __construct(array $options = array())
{
foreach ($options as $name => $value) {
if ($name[0] == '_') {
throw new \Coast\Exception("Access to '{$name}' is prohibited");
}
$this->$name($value);
}
}
public function dir($group, Dir $dir = null)
{
if (func_num_args() > 0) {
if (!$group) {
$group = 'default';
}
$this->_dirs[$group] = $dir;
$this->_meta($group, $dir);
return $this;
}
return isset($this->_dirs[$group])
? $this->_dirs[$group]
: null;
}
public function dirs(array $dirs = null)
{
if (func_num_args() > 0) {
foreach ($dirs as $group => $dir) {
$this->dir($group, $dir);
}
return $this;
}
return $this->_dirs;
}
public function extension($group, $source, $target)
{
array_unshift($this->_extensions, [
$this->_dirs[$group]
->dir($source),
$this->_dirs[isset($target[1]) ? $target[1] : $group]
->dir($target[0]),
]);
return $this;
}
public function extensions($group = null, array $extensions = null)
{
if (func_num_args() > 0) {
foreach ($extensions as $source => $target) {
$this->extension($group, $source, $target);
}
return $this;
}
return $this->_extensions;
}
public function extName($extName = null)
{
if (func_num_args() > 0) {
$this->_extName = $extName;
return $this;
}
return $this->_extName;
}
public function partialSeparator($partialSeparator = null)
{
if (func_num_args() > 0) {
$this->_partialSeparator = $partialSeparator;
return $this;
}
return $this->_partialSeparator;
}
protected function _meta($group, Dir $dir)
{
$meta = $dir->file('_.php');
if (!$meta->exists()) {
return;
}
$meta = include $meta->name();
if (isset($meta['extensions'])) {
$this->extensions($group, $meta['extensions']);
}
}
public function script($path, $group = null)
{
$path = new \Coast\Path($path);
if (isset($this->_active)) {
if (!isset($group)) {
$path = $path->isRelative()
? $path->toAbsolute($this->_active->script->path)
: $path;
$group = $this->_active->script->group;
} else if (!$path->isAbsolute()) {
$path = new \Coast\Path("/{$path}");
}
} else {
if (!$path->isAbsolute()) {
$path = new \Coast\Path("/{$path}");
}
if (!isset($group)) {
$group = 'default';
}
}
if (!isset($this->_dirs[$group])) {
throw new View\Exception("View group '{$group}' does not exist");
}
return (object) [
'key' => "{$group}:{$path}",
'group' => $group,
'path' => $path,
'file' => null,
];
}
public function files($script)
{
if (!isset($this->_files[$script->key])) {
$files = [];
$file = isset($script->file)
? $script->file
: $this->_dirs[$script->group]->file("{$script->path}.{$this->_extName}");
if ($file->exists()) {
array_push($files, $file);
}
foreach ($this->_extensions as $extension) {
if (!$file->isWithin($extension[0])) {
continue;
}
$file = $extension[1]->file($file->toRelative($extension[0]));
if ($file->exists()) {
array_push($files, $file);
}
}
$this->_files[$script->key] = $files;
}
return $this->_files[$script->key];
}
public function render($path, array $params = array(), $group = null, Content $previous = null)
{
if ($path instanceof File) {
$script = (object) [
'key' => $path->name(),
'group' => 'default',
'path' => new Path('/'),
'file' => $path,
];
} else {
$script = $this->script($path, $group);
}
array_unshift($this->_contexts, (object) ([
'script' => $script,
'params' => $params,
'vars' => [],
'outer' => null,
'block' => null,
'content' => new Content(),
'previous' => $previous,
'buffers' => 0,
'renders' => [],
]));
$this->_active = &$this->_contexts[0];
array_unshift($this->_active->renders, (object) [
'script' => $script,
'depth' => 0,
'params' => [],
'isPartial' => false,
]);
$content = $this->_render();
array_shift($this->_active->renders);
$this->_active->content->block(
$this->_active->block,
$content
);
$this->_active->block = null;
while ($this->_active->buffers > 0) {
$this->end();
}
$content = $this->_active->content;
if (isset($this->_active->outer)) {
list($path, $params, $group) = $this->_active->outer;
$content = $this->render($path, $params, $group, $content);
}
array_shift($this->_contexts);
if (count($this->_contexts)) {
$this->_active = &$this->_contexts[0];
} else {
$this->_active = null;
}
return $content;
}
protected function _render()
{
$render = &$this->_active->renders[0];
$script = $render->script;
$depth = $render->depth;
$params = $render->params;
$files = $this->files($script);
try {
if (!count($files)) {
if (!count($this->_active->renders)) {
throw new View\Failure("View script '{$script->group}::{$script->path}' does not exist");
} else if (!$render->isPartial) {
throw new View\Failure("View script '{$script->group}::{$script->path}' does not exist");
} else {
return;
}
}
if (!isset($files[$depth])) {
throw new View\Exception("View script '{$script->group}::{$script->path}' parent at depth '{$depth}' does not exist");
}
return $this->_run($files[$depth], array_merge(
$this->_active->params,
$params
));
} catch (\Exception $e) {
$this->_contexts = [];
$this->_active = null;
throw $e;
}
}
protected function _run(File $__file, array $__params = array())
{
$this->start();
try {
extract($__params);
include (string) $__file;
} catch (\Exception $e) {
if (isset($this->_active)) {
while ($this->_active->buffers > 0) {
echo $this->end();
}
}
throw $e;
}
return $this->end();
}
public function params(array $params)
{
$this->_active->params = array_merge(
$this->_active->params,
$params
);
}
public function pass(array $params)
{
$this->_active->content->params($params);
}
public function partial($name, $params = array())
{
array_unshift($this->_active->renders, (object) [
'script' => $this->script("{$this->_active->script->path}{$this->_partialSeparator}{$name}"),
'depth' => 0,
'params' => $params,
'isPartial' => true,
]);
$content = $this->_render();
array_shift($this->_active->renders);
return $content;
}
public function parent($depth = 1)
{
$render = &$this->_active->renders[0];
$render->depth += $depth;
return $this->_render();
}
public function inner($path, array $params = array(), $group = null)
{
if (!isset($this->_active)) {
throw new View\Exception("Cannot call View::inner() outside of rendering context");
}
$params = array_merge(
$this->_active->params,
$params
);
return $this->render($path, $params, $group);
}
public function outer($path, array $params = array(), $group = null)
{
if (!isset($this->_active)) {
throw new View\Exception("Cannot call View::outer() outside of rendering context");
}
$params = array_merge(
$this->_active->params,
$params
);
$this->_active->outer = [$path, $params, $group, false];
}
protected function block($name = null)
{
$content = $this->end();
if (strlen($content) > 0 ) {
$this->_active->content->block($this->_active->block, $content);
}
$name = isset($name)
? $name
: $this->_active->content->next();
$this->_active->block = $name;
$this->start();
}
protected function content($name = null)
{
if (!isset($this->_active->previous)) {
return;
}
return isset($name)
? $this->_active->previous->block($name)
: $this->_active->previous;
}
protected function start()
{
ob_start();
$this->_active->buffers++;
}
protected function end()
{
$this->_active->buffers--;
return ob_get_clean();
}
public function escape($string)
{
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
}
public function encode($string)
{
return mb_convert_encoding($string, 'UTF-8');
}
public function strip($string)
{
return strip_tags($string);
}
public function execute(\Coast\Request $req, \Coast\Response $res)
{
$path = $req->path();
$path = '/' . (strlen($path) ? $path : 'index');
try {
return $res->html($this->render($path, [
'req' => $req,
'res' => $res,
]));
} catch (View\Failure $e) {
return;
}
}
}