Basecoat\Routing::run PHP Method

run() public method

public run ( $route )
    public function run($route)
    {
        static $route_loop_cntr = 0;
        $route_loop_cntr++;
        // Set current route
        $this->running_route = $route;
        // Set convenience variable to reference Basecoat instance
        $basecoat = $this->basecoat;
        // check if valid route is specified
        if (!isset($this->routes[$route])) {
            // No route by that name, sanitize route name
            $file_name = trim(trim(str_replace('/', '', $route)), '/');
            // Check if there is a static template file matching request
            if (file_exists($basecoat->view->templates_path . 'static/' . $file_name . '.html')) {
                $this->routes[$route] = $this->routes[$this->default_routes['static']];
                $this->routes[$route]['template'] = 'static/' . $file_name . '.html';
            } elseif (file_exists($basecoat->view->templates_path . 'static/' . $file_name)) {
                // Create route using static route
                $this->routes[$route] = $this->routes[$this->default_routes['static']];
                $this->routes[$route]['template'] = 'static/' . $file_name;
            } else {
                $route = $this->default_routes['undefined'];
            }
        }
        // Assign route information to "current"
        $this->current = $this->routes[$route];
        // Check if the route specified a layout
        if (isset($this->current['layout'])) {
            $this->basecoat->view->setLayout($this->current['layout']);
        }
        if (isset($this->current['cacheable'])) {
            $cache =& $this->current['cacheable'];
            // Check for expires
            if (isset($cache['expires'])) {
                $this->basecoat->setCacheHeaders($cache['expires']);
            }
        }
        $this->processHooks($this->hooks['before']);
        // Make sure it's a valid route
        if (array_key_exists($route, $this->routes)) {
            // Check if http(s) is required
            if (isset($this->current['require_secure']) && $this->current['require_secure'] != 2) {
                // Determine current scheme
                $scheme = parse_url($this->requested_url, PHP_URL_SCHEME);
                if ($scheme == 'http' && $this->current['require_secure'] == 1) {
                    // Redirect to https
                    $new_url = 'https' . substr($this->requested_url, 4);
                    header('Location: ' . $new_url);
                    exit;
                } elseif ($scheme == 'https' && $this->current['require_secure'] == 0) {
                    $new_url = 'http' . substr($this->requested_url, 5);
                    header('Location: ' . $new_url);
                    exit;
                }
            }
            // Check for function call and/or file include
            if (isset($this->current['function']) && is_callable($this->current['function'])) {
                if (is_callable($this->current['function'])) {
                    $call_f = $this->current['function'];
                    $call_f();
                } elseif (is_array($this->current['function'])) {
                    foreach ($this->current['function'] as $call_f) {
                        if (is_callable($this->current['function'])) {
                            $call_f();
                        } else {
                            throw new \Exception('Route function not callable for "' . $this->running_route . '"');
                        }
                    }
                } else {
                    throw new \Exception('Route function not callable for "' . $this->running_route . '"');
                }
            }
            // Check if a route has a file registered to run
            if (isset($this->current['file'])) {
                // Run file mapped to route
                if (file_exists($this->current['file'])) {
                    include $this->current['file'];
                } else {
                    throw new \Exception('File not found for "' . $this->running_route . '" route: ' . $this->current['file']);
                }
            }
        } else {
            throw new \Exception('Undeclared route: ' . $this->running_route);
        }
        $this->routeClose();
    }