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:
<?php
namespace MvcCore\Controller;
trait Dispatching {
public static function CreateInstance () {
$instance = new static();
self::$allControllers[spl_object_hash($instance)] = $instance;
return $instance;
}
public static function GetCallerControllerInstance () {
$result = NULL;
$backtraceItems = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
if (count($backtraceItems) < 3) return $result;
$calledClass = get_called_class();
foreach ($backtraceItems as $backtraceItem) {
if (!isset($backtraceItem['object']) || !$backtraceItem['object']) continue;
$object = $backtraceItem['object'];
$class = $backtraceItem['class'];
if (
$object instanceof \MvcCore\IController &&
$class !== $calledClass
) {
$result = $object;
break;
}
}
return $result;
}
public function Dispatch ($actionName = "IndexAction") {
$actionNameStart = $this->actionName;
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
$this->Init();
if ($this->dispatchState == \MvcCore\IController::DISPATCH_STATE_TERMINATED)
return;
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_INITIALIZED;
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED)
$this->PreDispatch();
if ($this->dispatchState == \MvcCore\IController::DISPATCH_STATE_TERMINATED)
return;
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED)
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED;
if ($this->actionName !== $actionNameStart) {
$toolClass = $this->application->GetToolClass();
$actionName = $toolClass::GetPascalCaseFromDashed($this->actionName) . 'Action';
}
if (
$this->dispatchState < \MvcCore\IController::DISPATCH_STATE_ACTION_EXECUTED &&
method_exists($this, $actionName)
)
$this->{$actionName}();
if ($this->dispatchState == \MvcCore\IController::DISPATCH_STATE_TERMINATED)
return;
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_ACTION_EXECUTED)
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_ACTION_EXECUTED;
if ($this->viewEnabled && $this->dispatchState < \MvcCore\IController::DISPATCH_STATE_RENDERED)
$this->Render(
$this->controllerName,
$this->actionName
);
}
public function Init () {
if ($this->dispatchState > \MvcCore\IController::DISPATCH_STATE_CREATED)
return;
self::$allControllers[spl_object_hash($this)] = $this;
if ($this->parentController === NULL && !$this->request->IsCli()) {
if ($this->autoStartSession)
$this->application->SessionStart();
if ($this->ajax || (
$this->controllerName == 'controller' &&
$this->actionName == 'asset'
)) $this->viewEnabled = FALSE;
$responseContentType = $this->ajax ? 'text/javascript' : 'text/html';
$this->response->SetHeader('Content-Type', $responseContentType);
}
if ($this->autoInitProperties)
$this->autoInitializeProperties();
foreach ($this->childControllers as $controller) {
$controller->Init();
if ($controller->dispatchState == \MvcCore\IController::DISPATCH_STATE_TERMINATED)
break;
}
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_INITIALIZED;
}
protected function autoInitializeProperties () {
$ctrl = new \ReflectionClass($this);
$props = $ctrl->getProperties(
\ReflectionProperty::IS_PUBLIC |
\ReflectionProperty::IS_PROTECTED |
\ReflectionProperty::IS_PRIVATE
);
$toolsClass = $this->application->GetToolClass();
$attrsAnotations = $toolsClass::GetAttributesAnotations();
$attrClassName = '\\MvcCore\\Controller\\AutoInit';
$attrClassNameWithoutSlash = mb_substr($attrClassName, 1);
$phpDocsTagName = $attrClassName::PHP_DOCS_TAG_NAME;
foreach ($props as $prop) {
if ($attrsAnotations) {
$attrArgs = $toolsClass::GetAttrCtorArgs($prop, $attrClassNameWithoutSlash);
} else {
$attrArgs = $toolsClass::GetPhpDocsTagArgs($prop, $phpDocsTagName);
}
if ($attrArgs === NULL) continue;
$factoryMethodName = isset($attrArgs[0]) && is_string($attrArgs[0])
? $attrArgs[0]
: 'create' . ucfirst($prop->name);
$hasMethod = $ctrl->hasMethod($factoryMethodName);
if (!$hasMethod) {
$factoryMethodName = '_'.$factoryMethodName;
if (!$ctrl->hasMethod($factoryMethodName))
$factoryMethodName = NULL;
}
$this->autoInitializeProperty($ctrl, $prop, $factoryMethodName);
}
}
protected function autoInitializeProperty (\ReflectionClass $ctrl, \ReflectionProperty $prop, $factoryMethodName) {
$phpWithTypes = PHP_VERSION_ID >= 70400;
if ($factoryMethodName !== NULL) {
$method = $ctrl->getMethod($factoryMethodName);
if (!$method->isPublic()) $method->setAccessible(TRUE);
$instance = $method->invoke($this, []);
} else {
$className = NULL;
if ($phpWithTypes && $prop->hasType()) {
$refType = $prop->getType();
if ($refType !== NULL)
$className = $refType->getName();
}
if ($className === NULL) {
$toolsClass = $this->application->GetToolClass();
$attrArgs = $toolsClass::GetPhpDocsTagArgs($prop, '@var');
if (isset($attrArgs[0]) && is_string($attrArgs[0]))
$className = $attrArgs[0];
}
if ($className === NULL)
return FALSE;
if (!@class_exists($className)) {
$className = $prop->getDeclaringClass()->getNamespaceName() . '\\' . $className;
if (!@class_exists($className)) return FALSE;
}
if (is_callable("{$className}::CreateInstance")) {
$instance = $className::CreateInstance();
} else {
$instance = new $className();
}
}
if ($instance instanceof \MvcCore\IController)
$this->AddChildController($instance, $prop->name);
if (!$prop->isPublic()) $prop->setAccessible(TRUE);
$prop->setValue($this, $instance);
return TRUE;
}
public function PreDispatch () {
if ($this->dispatchState > \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
return;
if ($this->dispatchState == \MvcCore\IController::DISPATCH_STATE_CREATED)
$this->Init();
if ($this->viewEnabled && $this->view === NULL) {
$viewClass = $this->application->GetViewClass();
$this->view = $viewClass::CreateInstance()
->SetController($this);
}
foreach ($this->childControllers as $controller) {
$controller->PreDispatch();
if ($controller->dispatchState == \MvcCore\IController::DISPATCH_STATE_TERMINATED)
break;
}
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED)
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED;
}
public function AddChildController (\MvcCore\IController $controller, $index = NULL) {
self::$allControllers[spl_object_hash($controller)] = $controller;
if (!in_array($controller, $this->childControllers, TRUE)) {
if ($index === NULL) {
$this->childControllers[] = $controller;
} else {
$this->childControllers[$index] = $controller;
}
}
$controller
->SetParentController($this)
->SetApplication($this->application)
->SetEnvironment($this->environment)
->SetRequest($this->request)
->SetResponse($this->response)
->SetRouter($this->router)
->SetRenderMode($this->renderMode)
->SetLayout($this->layout)
->SetViewEnabled($this->viewEnabled)
->SetUser($this->user);
return $this;
}
public function GetSessionNamespace ($name = \MvcCore\ISession::DEFAULT_NAMESPACE_NAME) {
$sessionClass = $this->application->GetSessionClass();
return $sessionClass::GetNamespace($name);
}
public static function Redirect ($location = '', $code = \MvcCore\IResponse::SEE_OTHER, $reason = NULL) {
$app = \MvcCore\Application::GetInstance();
$response = $app->GetResponse();
$response
->SetCode($code)
->SetHeader('Location', $location);
if ($reason !== NULL)
$response->SetHeader('X-Reason', $reason);
foreach (self::$allControllers as & $controller)
$controller->dispatchState = \MvcCore\IController::DISPATCH_STATE_TERMINATED;
$app->Terminate();
}
public function Terminate () {
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_TERMINATED;
self::$allControllers = [];
$this->application->Terminate();
}
public function AssetAction () {
$ext = '';
$path = $this->GetParam('path', 'a-zA-Z0-9_\-\/\.');
$path = '/' . ltrim(str_replace('..', '', $path), '/');
if (
strpos($path, static::$staticPath) !== 0 &&
strpos($path, static::$tmpPath) !== 0
)
throw new \ErrorException("[".get_class($this)."] File path: '$path' is not allowed.", 500);
$path = $this->request->GetAppRoot() . $path;
if (!file_exists($path))
throw new \ErrorException("[".get_class($this)."] File not found: '$path'.", 404);
$lastDotPos = strrpos($path, '.');
if ($lastDotPos !== FALSE)
$ext = substr($path, $lastDotPos + 1);
if (isset(self::$_assetsMimeTypes[$ext]))
header('Content-Type: ' . self::$_assetsMimeTypes[$ext]);
header_remove('X-Powered-By');
header('Vary: Accept-Encoding');
$assetMTime = @filemtime($path);
if ($assetMTime)
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', $assetMTime));
readfile($path);
exit;
}
}