/** * Run applications and display output * @throws \DebugBar\DebugBarException */ public function run() { $html = null; // lets try to get html full content to page render try { /** @var \Ffcms\Core\Arch\Controller $callClass */ $callClass = null; $callMethod = 'action' . self::$Request->getAction(); // founded callback injection alias if (self::$Request->getCallbackAlias() !== false) { $cName = self::$Request->getCallbackAlias(); if (class_exists($cName)) { $callClass = new $cName(); } else { throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded'); } } else { // typical parsing of native apps $cName = '\\Apps\\Controller\\' . env_name . '\\' . self::$Request->getController(); // try to initialize class object if (class_exists($cName)) { $callClass = new $cName(); } else { throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName)); } } // try to call method of founded callback class if (method_exists($callClass, $callMethod)) { $actionQuery = []; // prepare action params for callback if (!Str::likeEmpty(self::$Request->getID())) { $actionQuery[] = self::$Request->getID(); if (!Str::likeEmpty(self::$Request->getAdd())) { $actionQuery[] = self::$Request->getAdd(); } } // get controller method arguments count $reflection = new \ReflectionMethod($callClass, $callMethod); $argumentCount = 0; foreach ($reflection->getParameters() as $arg) { if (!$arg->isOptional()) { $argumentCount++; } } // check method arguments count and current request count to prevent warnings if (count($actionQuery) < $argumentCount) { throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', ['method' => $callMethod, 'required' => $argumentCount, 'current' => count($actionQuery)])); } // make callback call to action in controller and get response $actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery); if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) { // set response to controller property object $callClass->setOutput($actionResponse); } // build full compiled output html data $html = $callClass->buildOutput(); } else { throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"'); } } catch (NotFoundException $e) { // catch exceptions and set output $html = $e->display(); } catch (ForbiddenException $e) { $html = $e->display(); } catch (SyntaxException $e) { $html = $e->display(); } catch (JsonException $e) { $html = $e->display(); } catch (NativeException $e) { $html = $e->display(); } catch (\Exception $e) { // catch all other exceptions $html = (new NativeException($e->getMessage()))->display(); } // set full rendered content to response builder self::$Response->setContent($html); // echo full response to user via http foundation self::$Response->send(); }