/** * Please bootstrap first, than run the application! * * Run a application, let application accept a request, route the request, * dispatch to controller/action, render response and return response to client finally. * * @param array $get Array of variables passed to the current script via the URL parameters. * @param array $post Array of variables passed to the current script via the HTTP POST method. * @param array $cookie Array of variables passed to the current script via HTTP Cookies. * * @throws \LogicException If application not bootstrapped. * @return void */ public static function run(array $get, array $post, array $cookie) { $cli = array(); if (Sapi::isCli()) { $cli = Cli::parse((array) Registry::get('env')->argv); if (count($cli) < 1 || isset($cli['list'])) { Cli::absorb(); exit(0); } } $conf = Registry::get('conf'); $prefix = Str::ensureTrailing('\\', $conf['app']['name']); $repository = BASE_PATH . 'app/' . $conf['app']['name'] . '/Controller'; if (isset($cli['controller']) && $cli['controller'] == 'core') { $prefix = 'Pimf\\'; $repository = BASE_PATH . 'pimf-framework/core/Pimf/Controller'; } $resolver = new Resolver(new Request($get, $post, $cookie, $cli), $repository, $prefix); $sessionized = Sapi::isWeb() && $conf['session']['storage'] !== ''; if ($sessionized) { Session::load(); } $pimf = $resolver->process(); if ($sessionized) { Session::save(); Cookie::send(); } $pimf->render(); }
/** * Method to show the content. * * @return mixed * @throws \Exception If not supported request method or bad controller */ public function render() { if (Sapi::isCli() && Config::get('environment') == 'production') { $suffix = 'CliAction'; $action = $this->request->fromCli()->get('action', 'index'); } else { $suffix = 'Action'; if ($this->request->getMethod() != 'GET' && $this->request->getMethod() != 'POST') { $redirectUrl = new Value($this->env->REDIRECT_URL); $redirectUrl = $redirectUrl->deleteLeading('/')->deleteTrailing('/')->explode('/'); $action = isset($redirectUrl[1]) ? $redirectUrl[1] : 'index'; } else { $bag = sprintf('from%s', ucfirst(strtolower($this->request->getMethod()))); $action = $this->request->{$bag}()->get('action', 'index'); } if (Config::get('app.routeable') === true && $this->router instanceof \Pimf\Router) { $target = $this->router->find(); if ($target instanceof \Pimf\Route\Target) { $action = $target->getAction(); Request::$getData = new Param(array_merge($target->getParams(), Request::$getData->getAll())); } } } $action = strtolower($action) . $suffix; if (method_exists($this, 'init')) { call_user_func(array($this, 'init')); } if (!method_exists($this, $action)) { throw new Bomb("no action '{$action}' defined at controller " . get_class($this)); } return call_user_func(array($this, $action)); }
/** * Please bootstrap first, than run the application! * Run a application, let application accept a request, route the request, * dispatch to controller/action, render response and return response to client finally. * * @param array $get Array of variables passed to the current script via the URL parameters. * @param array $post Array of variables passed to the current script via the HTTP POST method. * @param array $cookie Array of variables passed to the current script via HTTP Cookies. * @param array $files An associative array FILES of items uploaded to the current script via the HTTP POST method. * * @return void */ public static function run(array $get, array $post, array $cookie, array $files) { $cli = array(); if (Sapi::isCli()) { $cli = Cli::parse((array) self::$env->argv); if (count($cli) < 1 || isset($cli['list'])) { Cli::absorb(); exit(0); } } $prefix = Str::ensureTrailing('\\', Config::get('app.name')); $repository = BASE_PATH . 'app/' . Config::get('app.name') . '/Controller'; if (isset($cli['controller']) && $cli['controller'] == 'core') { $prefix = 'Pimf\\'; $repository = BASE_PATH . 'pimf-framework/core/Pimf/Controller'; } $request = new Request($get, $post, $cookie, $cli, $files, self::$env); $resolver = new Resolver($request, $repository, $prefix, self::$router); $sessionized = Sapi::isWeb() && Config::get('session.storage') !== ''; if ($sessionized) { Session::load(); } $pimf = $resolver->process(self::$env, self::$em, self::$logger); if ($sessionized) { Session::save(); Cookie::send(); } $pimf->render(); }
/** * @param Request $request * @param string $repositoryPath * @param string $prefix * * @throws Resolver\Exception */ public function __construct(Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\') { $conf = Registry::get('conf'); $controllerName = $request->fromGet()->get('controller'); if ($conf['app']['routeable'] === true) { $target = Registry::get('router')->find(); if ($target instanceof \Pimf\Route\Target) { $controllerName = $target->getController(); } } if (Sapi::isCli() && $conf['environment'] == 'production') { $controllerName = $request->fromCli()->get('controller'); } if (!$controllerName) { $controllerName = $conf['app']['default_controller']; } $this->repositoryPath = $repositoryPath; $this->request = $request; $this->controllerClass = $prefix . 'Controller\\'; $basepath = $this->repositoryPath . '/'; $controller = ucfirst($controllerName); if (Str::isEvilPath($basepath . $controller)) { throw new Bomb('directory traversal attack is not funny!'); } $this->controllerPath = $basepath . $controller . '.php'; if (!file_exists($this->controllerPath)) { throw new Bomb('no controller found at the repository path; ' . $this->controllerPath); } }
/** * Method to show the content. * * @return mixed * @throws \Exception If not supported request method or bad controller */ public function render() { $conf = Registry::get('conf'); if (Sapi::isCli() && $conf['environment'] == 'production') { $suffix = 'CliAction'; $action = $this->request->fromCli()->get('action', 'index'); } else { $suffix = 'Action'; $bag = 'from' . ucfirst(strtolower($this->response->getMethod())); $action = $this->request->{$bag}()->get('action', 'index'); if ($conf['app']['routeable'] === true) { $target = Registry::get('router')->find(); if ($target instanceof \Pimf\Route\Target) { $action = $target->getAction(); Request::$getData = new Param((array) Request::stripSlashesIfMagicQuotes(array_merge($target->getParams(), Request::$getData->getAll()))); } } } $action = strtolower($action) . $suffix; if (method_exists($this, 'init')) { call_user_func(array($this, 'init')); } if (!method_exists($this, $action)) { throw new Bomb("no action '{$action}' defined at controller " . get_class($this)); } return call_user_func(array($this, $action)); }
/** * If detailed errors are enabled, just format the exception into * a simple error message and display it. * * @param \Exception $exception * * @return string */ public static function format(\Exception $exception) { if (Sapi::isCli()) { return "+++ Untreated Exception +++" . PHP_EOL . "Message: " . $exception->getMessage() . PHP_EOL . "Location: " . $exception->getFile() . " on line " . $exception->getLine() . PHP_EOL . "Stack Trace: " . PHP_EOL . $exception->getTraceAsString() . PHP_EOL; } return "<html><h2>Untreated Exception</h2>\n <h3>Message:</h3>\n <pre>" . $exception->getMessage() . "</pre>\n <h3>Location:</h3>\n <pre>" . $exception->getFile() . " on line " . $exception->getLine() . "</pre>\n <h3>Stack Trace:</h3>\n <pre>" . $exception->getTraceAsString() . "</pre></html>"; }
/** * Handle an exception and display the exception report. * * @param \Exception $exception * @param Logger $logger * @param boolean $exit */ public static function exception(\Exception $exception, Logger $logger, $exit = true) { static::log($exception, $logger); ob_get_length() > 0 && ob_get_level() && ob_end_clean(); if (Config::get('error.debug_info') === true) { echo static::format($exception, Sapi::isCli()); if ($exit) { exit; } } if ($exception instanceof \Pimf\Controller\Exception || $exception instanceof \Pimf\Resolver\Exception) { Event::first('404', array($exception)); Header::sendNotFound(null, $exit); } else { Event::first('500', array($exception)); Header::sendInternalServerError(null, $exit); } }
/** * @param int $code * @param string $status * @param boolean $exit */ protected static function view($code, $status, $exit = true) { if (Sapi::isCli()) { echo $status . PHP_EOL; if ($exit) { exit; } } self::send($code, $status); $appTpl = str_replace('/', DS, BASE_PATH . 'app/' . Config::get('app.name') . '/_error/' . $code . '.php'); $coreTpl = str_replace('/', DS, BASE_PATH . 'pimf-framework/core/Pimf/_error/' . $code . '.php'); $coreTpl = str_replace(DS . 'pimf-framework' . DS . 'pimf-framework' . DS, DS . 'pimf-framework' . DS, $coreTpl); if (file_exists($appTpl) && is_readable($appTpl)) { include $appTpl; if ($exit) { exit(1); } } include $coreTpl; if ($exit) { exit(1); } }