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:
<?php
namespace MvcCore\View;
trait Rendering {
public function & RenderScript ($relativePath = '') {
return $this->Render(static::$scriptsDir, $relativePath);
}
public function & RenderLayout ($relativePath = '') {
return $this->Render(static::$layoutsDir, $relativePath);
}
public function & RenderLayoutAndContent ($relativePath = '', & $content = NULL) {
if ($relativePath === NULL) return $content;
$this->__protected['content'] = & $content;
return $this->Render(static::$layoutsDir, $relativePath);
}
public function SetUpRender ($renderMode = \MvcCore\IView::RENDER_WITH_OB_FROM_ACTION_TO_LAYOUT, $controllerOrActionNameDashed = NULL, $actionNameDashed = NULL) {
$this->__protected['renderArgs'] = func_get_args();
$helpers = & $this->__protected['helpers'];
$buildInHelpersInit = & $this->__protected['buildInHelpersInit'];
if (!$buildInHelpersInit) {
$buildInHelpersInit = TRUE;
$this->setUpRenderBuildInHelpers($helpers);
}
foreach (self::$_globalHelpers as $helperNamePascalCase => $helperRecord) {
$helperNameCamelCase = lcfirst($helperNamePascalCase);
if (isset($helpers[$helperNameCamelCase])) continue;
$instance = & $helperRecord[0];
$implementsIHelper = $helperRecord[1];
$needsClosureFn = $helperRecord[2];
if ($implementsIHelper)
$instance->SetView($this);
if ($needsClosureFn) {
$helpers[$helperNameCamelCase] = function () use (& $instance, $helperNamePascalCase) {
return call_user_func_array([$instance, $helperNamePascalCase], func_get_args());
};
} else {
$helpers[$helperNameCamelCase] = & $instance;
}
}
return $this;
}
public function & Render ($typePath = '', $relativePath = '') {
if (!$typePath)
$typePath = static::$scriptsDir;
$result = '';
$relativePath = $this->correctRelativePath(
$typePath, $relativePath
);
$viewScriptFullPath = static::GetViewScriptFullPath($typePath, $relativePath);
if (!file_exists($viewScriptFullPath)) {
throw new \InvalidArgumentException(
"[".get_class()."] Template not found in path: `{$viewScriptFullPath}`."
);
}
$renderedFullPaths = & $this->__protected['renderedFullPaths'];
$renderedFullPaths[] = $viewScriptFullPath;
list($renderMode) = $this->__protected['renderArgs'];
$renderModeWithOb = ($renderMode & \MvcCore\IView::RENDER_WITH_OB_FROM_ACTION_TO_LAYOUT) != 0;
if ($renderModeWithOb)
ob_start();
$result = call_user_func(function ($viewPath, $controller, $helpers) {
extract($helpers, EXTR_SKIP);
unset($helpers);
extract($this->__protected['store'], EXTR_SKIP);
include($viewPath);
}, $viewScriptFullPath, $this->controller, $this->__protected['helpers']);
if ($renderModeWithOb) {
$result = ob_get_clean();
\array_pop($renderedFullPaths);
return $result;
} else {
$result = '';
\array_pop($renderedFullPaths);
return $result;
}
}
public static function GetViewScriptFullPath ($typePath = '', $corectedRelativePath = '') {
if (self::$_viewScriptsFullPathBase === NULL)
self::initViewScriptsFullPathBase();
return implode('/', [
self::$_viewScriptsFullPathBase,
$typePath,
$corectedRelativePath . static::$extension
]);
}
public function SetUpStore (\MvcCore\IView $view, $overwriteExistingKeys = TRUE) {
$currentStore = & $this->__protected['store'];
$viewStore = & $view->__protected['store'];
if ($overwriteExistingKeys) {
$this->__protected['store'] = array_merge($currentStore, $viewStore);
} else {
foreach ($viewStore as $key => & $value)
if (!array_key_exists($key, $currentStore))
$currentStore[$key] = & $value;
}
return $this;
}
public function & GetContent () {
list(
$renderMode,
$controllerOrActionNameDashed,
$actionNameDashed
) = $this->__protected['renderArgs'];
$renderModeWithOb = ($renderMode & \MvcCore\IView::RENDER_WITH_OB_FROM_ACTION_TO_LAYOUT) != 0;
if ($renderModeWithOb) {
return $this->__protected['content'];
} else {
$viewScriptPath = $this->controller->GetViewScriptPath($controllerOrActionNameDashed, $actionNameDashed);
$viewClass = $this->controller->GetApplication()->GetViewClass();
$actionView = $viewClass::CreateInstance()
->SetController($this->controller)
->SetUpStore($this, TRUE)
->SetUpRender(
$renderMode, $controllerOrActionNameDashed, $actionNameDashed
);
$actionView->RenderScript($viewScriptPath);
$result = '';
return $result;
}
}
public function & Evaluate ($content) {
if ($content === NULL || mb_strlen(strval($content)) === 0)
return '';
ob_start();
try {
eval(' ?'.'>'.$content.'<'.'?php ');
} catch (\Exception $e) {
throw $e;
} catch (\Throwable $e) {
throw $e;
}
$content = ob_get_clean();
return $content;
}
protected function setUpRenderBuildInHelpers (& $helpers) {
$router = $this->controller->GetRouter();
$helpers += [
'url' => function ($controllerActionOrRouteName = 'Index:Index', array $params = []) use (& $router) {
return $router->Url($controllerActionOrRouteName, $params);
},
'assetUrl' => function ($path = '') use (& $router) {
return $router->Url('Controller:Asset', ['path' => $path]);
},
'escapeHtml' => function ($str, $encoding = 'UTF-8') {
return $this->EscapeHtml($str, $encoding);
},
'escapeHtmlText' => function ($str, $encoding = 'UTF-8') {
return $this->EscapeHtmlText($str, $encoding);
},
'escapeHtmlAttr' => function ($str, $double = TRUE, $encoding = 'UTF-8') {
return $this->EscapeHtmlAttr($str, $double, $encoding);
},
'escapeXml' => function ($str, $encoding = 'UTF-8') {
return $this->EscapeXml($str, $encoding);
},
'escapeJs' => function ($str, $flags = 0, $depth = 512) {
return $this->EscapeJs($str, $flags, $depth);
},
'escapeCss' => function ($str) {
return $this->EscapeCss($str);
},
'escapeICal' => function ($str) {
return $this->EscapeICal($str);
},
];
}
}