Ejemplo n.º 1
0
 /**
  * the main workhorse of the maverick class - this performs tasks like fetching views from cache to bypass the rest of the framework,
  * handles route pre-parsing if that's set up, sets up any initial database connections if requested in the config and calls the 
  * first controller matched by the requested route
  */
 public function build()
 {
     // load from the cache if that is on and this is a GET request
     if ($this->get_config('cache.on') !== false && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') {
         $hash = $this->get_request_route_hash();
         $headers = \maverick\cache::fetch("{$hash}_headers");
         $view = \maverick\cache::fetch($hash);
         if ($headers) {
             $headers = json_decode($headers);
             foreach ($headers as $header) {
                 header($header);
             }
         }
         if ($view) {
             die($view);
         }
     }
     /*if(strlen($this->get_config('config.route_preparser') ) )
     		$this->route_preparser();*/
     $this->get_request_uri();
     $this->db = new \stdClass();
     if ($this->get_config('lang.active') === true) {
         $this->set_lang_culture();
     }
     // look at routes to find out which route the requested path best matches
     require_once MAVERICK_BASEDIR . 'routes.php';
     // initialise base db object if the config exists, the engine is not an empty string, and the required parameters exist
     if (strlen($this->get_config('db.engine')) && $this->check_required_fields($this->get_config('db'), array('engine', 'host', 'database', 'username', 'password'))) {
         $dbname = $this->get_config('db.database');
         $dbhost = $this->get_config('db.host');
         $dbuser = $this->get_config('db.username');
         $dbpass = $this->get_config('db.password');
         $this->db->pdo = $pdo = new \PDO("mysql:dbname={$dbname};host={$dbhost}", $dbuser, $dbpass, array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
     }
     // locate and initiate call to controller responsible for the requested route
     if (!empty($this->controller)) {
         if ($this->check_class_method_exists($this->controller['controller_name'], $this->controller['method'], $this->controller['controller'])) {
             $this->controller['controller']->{$this->controller['method']}($this->controller['args']);
         } else {
             \error::show("controller '{$this->controller['controller_name']}' or method '{$this->controller['method']}' not found");
         }
     } else {
         // throw a 404 - either look to see if an explicit route accounts for this, or throw some kind of default one
         $error_controller = $this->get_error_route(404);
         if ($this->check_class_method_exists($error_controller['controller_name'], $error_controller['method'], $this->controller['controller'])) {
             // add the error controller details in as the main controller details for consistency later on
             list($this->controller['controller_name'], $this->controller['method'], $this->controller['protocol'], $this->controller['args']) = array($error_controller['controller_name'], $error_controller['method'], 'any', null);
             $this->controller['controller']->{$this->controller['method']}();
         } else {
             \error::show("404", 404);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * use object buffering to build up the views into a single string and either return or echo it
  * optionally output any headers that have been added to the view instance
  * @param bool $echo whether to echo the view or return it as a string
  * @param bool $with_headers if set to true and $echo is also set to true, then send headers to the browser, otherwise do nothing
  * @return string
  */
 public static function render($echo = true, $with_headers = false)
 {
     $v = view::getInstance();
     $app = \maverick\maverick::getInstance();
     $view_file_path = MAVERICK_VIEWSDIR . "/{$v->view}.php";
     if (!file_exists($view_file_path)) {
         error::show("View '{$v->view}' does not exist");
     } else {
         ob_start();
         include $view_file_path;
         $view = ob_get_contents();
         ob_end_clean();
         if ($app->get_config('config.view_parsing') !== false) {
             $view = $v->parse_view($view);
         }
         // this just stores the view if the config value is set to cache it
         if ($app->get_config('cache.on') !== false) {
             $hash = $app->get_request_route_hash();
             \maverick\cache::store($hash, $view);
             if ($with_headers) {
                 \maverick\cache::store("{$hash}_headers", json_encode($v->headers));
             }
         }
         if ($with_headers) {
             foreach ($v->headers as $header) {
                 header($header);
             }
             // teapot
             if (isset($v->original_headers['status']) && $v->original_headers['status'] == 418 && $app->get_config('config.teapot') !== false) {
                 $teapot = \helpers\html\html::load_snippet(\MAVERICK_BASEDIR . 'vendor/maverick/teapot.html', array());
                 $view = preg_replace('/<body/', "<body>{$teapot}", $view, 1);
             }
         }
         if ($echo) {
             echo $view;
         } else {
             return $view;
         }
     }
 }