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:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom FlĂdr (https://github.com/mvccore/mvccore)
* @license https://mvccore.github.io/docs/mvccore/4.0.0/LICENCE.md
*/
namespace MvcCore;
require_once('Controller.php');
/**
* Core view:
* - static doctype storage
* - static storage for dir names with view scripts
* - possible to use for any controller/subcontroller/control/\MvcCore\Ext\Form
* - view prerender preparing and rendering
* - direct code evaluation
* - view helpers management
* - creating by predefined class names bases
* - instance storing and calling
* - views sub scripts relative path solving
* - Url() proxy method, AssetUrl() proxy method
* - magic calls:
* - __call() - helpers handling
* - __set() - to set anything in controller to get it back in view
* - __get() - to get anything in view previously completed in controller
* - no special view language implemented... why to use such stupid things...
*/
class View
{
/**
* View output document type HTML4
* @var string
*/
const DOCTYPE_HTML4 = 'HTML4';
/**
* View output document type XHTML
* @var string
*/
const DOCTYPE_XHTML = 'XHTML';
/**
* View output document type HTML5
* @var string
*/
const DOCTYPE_HTML5 = 'HTML5';
/**
* View script files extenion in Views application directory
* @var string
*/
const EXTENSION = '.phtml';
/**
* Document type (HTML, XHTML or anything you desire)
* @var string
*/
public static $Doctype = self::DOCTYPE_HTML5;
/**
* Controller action templates directory placed in '/App/Views' dir. For read & write.
* @var string
*/
public static $ScriptsDir = 'Scripts';
/**
* views helpers directory placed in '/App/Views' dir. For read & write.
* @var string
*/
public static $HelpersDir = 'Helpers';
/**
* Layout templates directory placed in '/App/Views' dir. For read & write.
* @var string
*/
public static $LayoutsDir = 'Layouts';
/**
* Helpers classes - base class names. For read & write.
* @var array
*/
public static $HelpersClassBases = array(/*'\MvcCore\Ext\View\Helpers\'*/);
/**
* Rendered content
* @var \MvcCore\Controller|mixed
*/
public $Controller;
/**
* Rendered content
* @var string
*/
private $_content = '';
/**
* Currently rendered php/html file path
* @var array
*/
private $_renderedFullPaths = array();
/**
* Originaly declared dynamic properties to protect from __set() magic method
* @var string
*/
protected static $originalyDeclaredProperties = array(
'Controller' => 1,
'_content' => 1,
'_renderedFullPaths'=> 1,
);
/**
* Helpers instances storrage
* @var array
*/
private static $_helpers = array();
/**
* Static initialization - complete static::$HelpersClassBases by app configuration.
* @return void
*/
public static function StaticInit () {
$app = \MvcCore::GetInstance();
static::$HelpersClassBases = array(
'\MvcCore\Ext\View\Helpers\\',
// '\App\Views\Helpers\'
'\\' . implode('\\', array(
$app->GetAppDir(),
$app->GetViewsDir(),
static::$HelpersDir
)) . '\\',
);
}
/**
* Add view helpers class base name(s),
* example: \MvcCore\View::AddHelpersClassBases('\Any\Other\ViewHelpers\Place\', '...');
* @param string $helper,... View helper class base name(s)
* @return void
*/
public static function AddHelpersClassBases (/*...$helper*/) {
$args = func_get_args();
foreach ($args as $arg) {
static::$HelpersClassBases[] = '\\' . trim($arg, '\\') . '\\';
}
}
/**
* Get view script full path
* @param string $typePath
* @param string $corectedRelativePath
* @return string
*/
public static function GetViewScriptFullPath ($typePath = '', $corectedRelativePath = '') {
$app = \MvcCore::GetInstance();
return implode('/', array(
$app->GetRequest()->AppRoot,
$app->GetAppDir(),
$app->GetViewsDir(),
$typePath,
$corectedRelativePath . \MvcCore\View::EXTENSION
));
}
/**
* Create new view instance.
* @param \MvcCore\Controller $controller
*/
public function __construct (\MvcCore\Controller & $controller) {
$this->Controller = $controller;
}
/**
* Set up all keys/fields/properties in given array/stdclass/instance into current view context.
* @param mixed $paramsInstance
*/
public function SetUp (& $paramsInstance) {
$params = get_object_vars($paramsInstance);
foreach ($params as $key => $value) {
if (isset(static::$originalyDeclaredProperties[$key])) continue;
$this->$key = $value;
}
}
/**
* Return rendered controller/action template content.
* @return string
*/
public function GetContent () {
return $this->_content;
}
/**
* Return controller instance.
* @return \MvcCore\Controller
*/
public function GetController () {
return $this->Controller;
}
/**
* Render controller/action template script and return it's result.
* @param string $relativePath
* @return string
*/
public function RenderScript ($relativePath = '') {
return $this->Render(static::$ScriptsDir, $relativePath);
}
/**
* Render layout template script and return it's result.
* @param string $relativePath
* @return string
*/
public function RenderLayout ($relativePath = '') {
return $this->Render(static::$LayoutsDir, $relativePath);
}
/**
* Render layout template script and return it's result with inner rendered content.
* @param string $relativePatht.
* @param string $content
* @return string
*/
public function RenderLayoutAndContent ($relativePath = '', $content = '') {
$this->_content = $content;
return $this->Render(static::$LayoutsDir, $relativePath);
}
/**
* Render controller template and all necessary layout templates and return rendered result.
* @param string $typePath
* @param string $relativePath
* @throws \Exception
* @return string
*/
public function Render ($typePath = '', $relativePath = '') {
if (!$typePath) $typePath = self::$ScriptsDir;
$result = '';
$relativePath = $this->_correctRelativePath($this->Controller->GetRequest()->AppRoot, $typePath, $relativePath);
$viewScriptFullPath = static::GetViewScriptFullPath($typePath, $relativePath);
if (!file_exists($viewScriptFullPath)) {
throw new \Exception('['.__CLASS__."] Template not found in path: '$viewScriptFullPath'.");
}
$this->_renderedFullPaths[] = $viewScriptFullPath;
ob_start();
include($viewScriptFullPath);
$result = ob_get_clean();
array_pop($this->_renderedFullPaths); // unset last
return $result;
}
/**
* Evaluate given code as PHP in current view context,
* any $this keyword will be used as view context.
* @param string $content
* @return string
*/
public function Evaluate ($content = '') {
ob_start();
try {
eval(' ?'.'>'.$content.'<'.'?php ');
} catch (\Exception $e) {
throw $e;
}
return ob_get_clean();
}
/**
* Generates url by:
* - 'Controller:Action' name and params array
* (for routes configuration when routes array has keys with 'Controller:Action' strings
* and routes has not controller name and action name defined inside)
* - route name and params array
* (route name is key in routes configuration array, should be any string
* but routes must have information about controller name and action name inside)
* Result address should have two forms:
* - nice rewrited url by routes configuration
* (for apps with .htaccess supporting url_rewrite and when first param is key in routes configuration array)
* - for all other cases is url form: index.php?controller=ctrlName&action=actionName
* (when first param is not founded in routes configuration array)
* @param string $controllerActionOrRouteName Should be 'Controller:Action' combination or just any route name as custom specific string
* @param array $params optional
* @return string
*/
public function Url ($controllerActionOrRouteName = 'Index:Index', $params = array()) {
return \MvcCore\Router::GetInstance()->Url($controllerActionOrRouteName, $params);
}
/**
* Get asset url - proxy method into \MvcCore\Controller::AssetUrl();
* @param string $path
* @return string
*/
public function AssetUrl ($path = '') {
return $this->Controller->AssetUrl($path);
}
/**
* Set any value into view context except system keys declared in static::$originalyDeclaredProperties.
* @param string $name
* @param mixed $value
* @throws \Exception
*/
public function __set ($name, $value) {
if (isset(static::$originalyDeclaredProperties[$name])) {
throw new \Exception(
'['.__CLASS__."] It's not possible to change property: '$name' originaly declared in class ".__CLASS__.'.'
);
}
$this->$name = $value;
}
/**
* Try to call view helper.
* If view helper doesn't exist in global helpers store - create new helper instance.
* If helper already exists in global helpers store - do not create it again - use instance from store.
* Then call it's public method named in the same way as helper and return result
* as it is without any conversion! So then there could be called any other helper method if helper instance was returned.
* @param string $method
* @param mixed $arguments
* @return string|mixed
*/
public function __call ($method, $arguments) {
$result = '';
foreach (static::$HelpersClassBases as $helperClassBase) {
$className = $helperClassBase . ucfirst($method);
if (class_exists($className)) {
if (isset(self::$_helpers[$method]) && get_class(self::$_helpers[$method]) == $className) {
$instance = self::$_helpers[$method];
$result = call_user_func_array(array($instance, $method), $arguments);
} else {
$instance = new $className($this);
$result = call_user_func_array(array($instance, $method), $arguments);
}
break;
}
}
return $result;
}
/**
* If relative path declared in view starts with "./anything-else.phtml",
* then change relative path to correct "./" context and return full path.
* @param string $appRoot
* @param string $typePath
* @param string $relativePath
* @return string full path
*/
private function _correctRelativePath ($appRoot, $typePath, $relativePath) {
$result = str_replace('\\', '/', $relativePath);
if (substr($relativePath, 0, 2) == './') {
$app = \MvcCore::GetInstance();
$typedViewDirFullPath = implode('/', array(
$appRoot, $app->GetAppDir(), $app->GetViewsDir(), $typePath
));
$lastRenderedFullPath = $this->_renderedFullPaths[count($this->_renderedFullPaths) - 1];
$renderedRelPath = substr($lastRenderedFullPath, strlen($typedViewDirFullPath));
$renderedRelPathLastSlashPos = strrpos($renderedRelPath, '/');
if ($renderedRelPathLastSlashPos !== FALSE) {
$result = substr($renderedRelPath, 0, $renderedRelPathLastSlashPos + 1).substr($relativePath, 2);
$result = ltrim($result, '/');
}
}
return $result;
}
}
View::StaticInit();