Exemplo n.º 1
0
 /**
  * Get controller based on HTTP request
  * @param array $args Controller arguments
  * @return object Controller
  * @throws Exception\UndefinedControllerException
  * @throws Exception\InvalidControllerException
  * @throws Exception\NoIndexRouteException
  */
 public static function getController(array $args = array())
 {
     $config = ConfigFileHandler::getInstance();
     $mvc = $config->getParam("framework", "mvc");
     $requestArray = Request::getDirArray();
     $routePath = $mvc["route"];
     $controllerClassName = null;
     if (!isset($requestArray[intval($mvc["start_index"])])) {
         $controllerClassName = $routePath["."];
     } else {
         for ($i = intval($mvc["start_index"]); $i < count($requestArray) + 1; $i++) {
             if (!($routePath = self::getRouteChild($requestArray[$i], $routePath))) {
                 throw new UndefinedControllerException("Controller for this request is not defined");
             }
             if (is_string($routePath)) {
                 $controllerClassName = $routePath;
                 break;
             }
             if (!isset($requestArray[$i + 1])) {
                 if (isset($routePath["."])) {
                     $controllerClassName = $routePath["."];
                     break;
                 } else {
                     throw new NoIndexRouteException("Route has no index controller defined");
                 }
             }
         }
     }
     $controllerReflection = new \ReflectionClass(__NAMESPACE__ . "\\Controller\\" . $controllerClassName);
     if ($controllerReflection->implementsInterface(__NAMESPACE__ . "\\Controller\\Init\\IController")) {
         return $controllerReflection->newInstance($args);
     } else {
         throw new InvalidControllerException("Controller is not implementing interface IController");
     }
 }
Exemplo n.º 2
0
 public function main(array $argv = array())
 {
     // populate array
     $this->errorCode = $argv["code"];
     $this->errorData["time"] = time();
     $this->errorData["data"] = $argv["data"];
     $this->errorData["error"]["code"] = $this->errorCode;
     $this->errorData["error"]["message"] = Request::getErrorMessage($this->errorCode);
     $this->errorData["vars"]["server"] = $_SERVER;
     $this->errorData["vars"]["post"] = $_POST;
     $this->errorData["vars"]["get"] = $_GET;
     $this->errorData["vars"]["session"] = $_SESSION;
     $this->view->setParam("error_code", $this->errorCode);
     $this->view->setParam("error_message", Request::getErrorMessage($this->errorCode));
     $this->view->setParam("monkey_script", self::monkeyHash($this->errorData));
     $this->view->render();
 }
Exemplo n.º 3
0
use FreshPHP\Config\ClassAutoloader;
use FreshPHP\Config\ConfigFileHandler;
use FreshPHP\Config\LocaleTransfer;
use FreshPHP\HTTP\Request;
use FreshPHP\MVC\MVCRouter;
use FreshPHP\MVC\Controller\ErrorController;
session_start();
ClassAutoloader::register() || die("Cannot register autoloader");
error_reporting(Request::getVariable("debug") ? E_ALL : 0);
$lDir = (int) ConfigFileHandler::getInstance()->getParam("framework", "mvc", "locale_index");
if ($lDir >= 0 && Request::getDir($lDir) == "") {
    Request::redirect("/" . !Request::getSessionVar("locale", "%s", false) ? ConfigFileHandler::getInstance()->getParam("framework", "mvc", "default_locale") : Request::getSessionVar("locale"));
}
try {
    LocaleTransfer::setLocale(Request::getDir((int) ConfigFileHandler::getInstance()->getParam("framework", "mvc", "locale_index")));
    Request::setSessionVar("locale", LocaleTransfer::getLocale());
    MVCRouter::getController()->main();
} catch (Exception $e) {
    $errorController = new ErrorController();
    $errorData = array();
    $exception = explode("\\", get_class($e));
    switch ($exception[count($exception) - 1]) {
        case "NoIndexRouteException":
        case "UndefinedControllerException":
            $errorData["code"] = 404;
            break;
        case "InvalidControllerException":
            $errorData["code"] = 500;
            break;
        case "Exception":
        default: