/** * Processes the request uri. * * TODO: Make this more efficient. * The following code works but only if the URI does not contain any wildcard variables ex: (any) * $routesArrayKey = array_search($this->requestUri, array_column($this->routes, 'uri')); */ public function dispatch() { /** * @var $route Route */ $routeMatches = false; $methodMatches = false; /** * Cycle through all of the routes in the routes array to locate a match. * Replace / in the route with \/ for regex matching. * Replace (any) in the route with (\w+) for regex matching. * Replace (int) in the route with (\d+) for regex matching. * Replace (abc) in the route with ([A-Za-z]+) for regex matching. */ foreach ($this->manager->getRoutes() as $route) { $regexPattern = str_replace('/', '\\/', $route->getPath()); $regexPattern = str_replace('(any)', '(\\w+)', $regexPattern); $regexPattern = str_replace('(int)', '(\\d+)', $regexPattern); $regexPattern = str_replace('(abc)', '([A-Za-z]+)', $regexPattern); $regexPattern = "/^" . $regexPattern . '$/i'; if (preg_match($regexPattern, $this->requestUri)) { $routeMatches = true; if ($route->getHttpMethod() == $this->requestMethod) { $methodMatches = true; break; } } } if (!$routeMatches) { throw new Exceptions\RouteNotFoundException(); } if (!$methodMatches) { throw new Exceptions\MethodNotAllowedException(); } /** * Strip the wildcard variables from the URI for use by using (any),(int),(abc) in the route. * * TODO: Fix Bug * Bug Description: Returns an incorrect variables array when a wildcard is not used at the end of a string. * Example Fails: '/profile/(any)/show/(int)' Returns: show,(WILDCARD VALUE) * * Does work if all wildcards are at the end of the route. * Example: '/profile/show/(any)/(int)/(abc)' */ $variables = array(); $routePathArray = explode('/', ltrim($route->getPath(), '/')); $requestUriArray = explode('/', ltrim($this->requestUri, '/')); foreach ($routePathArray as $key => $item) { if ($item === '(abc)' || $item === '(int)' || $item === '(any)') { $variables[] = $requestUriArray[$key]; } } /** * Separate the controller to call and the method to call. */ $action = explode('@', $route->getAction()); $this->matchedRoute = array('controller' => $action[0], 'method' => $action[1], 'variables' => $variables); }
/** * Initialize Page Manager * * ## Overview * * @uses SatanBarbaraApp * @uses SessionManager * @uses ViewManager * @uses DebugManager * @uses RouteManager * @uses PageView * * @see RouteManager * * @param array An array of creds for SendGrid API. * @return true Always unless fatal error or exception is thrown. * * @version 2015-07-05.1 * @since 0.5.1b * @author TronNet DevOps [Sean Murray] <*****@*****.**> */ public static function Init($params) { DebugManager::Log("Initializing Page Manager", '@'); DebugManager::Log($params); $appConfig = SatanBarbaraApp::GetConfig(); /** * @todo have config in it's own 'config' position instead of array_merge */ $data = array('app' => array_merge($appConfig[SATANBARBARA_CURRENT_ENVIRONMENT], array()), 'page' => $params); DebugManager::Log("checking if logged in...", null, 3); if (SessionManager::IsLoggedIn()) { $data['session'] = array('is_auth' => true, 'account' => SessionManager::GetAccount()); DebugManager::Log("Got an account, checking for a saved program...", null, 3); } $Page = ucfirst($params['page']) . 'View'; DebugManager::Log("Searching for view with class name: " . $Page); if ($Page::HasAccess(SessionManager::GetAccessLevel())) { $Page::Init($data); ViewManager::Render($Page); } else { DebugManager::Log("looks like this page requires auth but user isn't authenticated!"); RouteManager::GoToPageURI('login'); } return true; }
public static function Init($requestParams) { $appConfig = SatanBarbaraApp::GetConfig(); $data = $requestParams['params']; DebugManager::Log("Got a " . $requestParams['request']['action'] . " request!", '@'); DebugManager::Log("Got some data"); DebugManager::Log($data); $requestName = ucfirst($requestParams['request']['action']) . 'Request'; DebugManager::Log("Searching for view with class name: " . $requestName); if ($requestName::HasAccess(SessionManager::GetAccessLevel())) { $response = $requestName::Init($data); } else { DebugManager::Log("looks like this page requires auth but user isn't authenticated!"); RouteManager::GoToPageURI('login', array("message" => "That page requires authenticated access!")); } if (isset($data['_format']) && $data['_format'] == 'json') { self::AJAXResponse($response); } else { $format = isset($data['_format']) ? $data['_format'] : 'html'; if (isset($data['_redirect'])) { $redirect = $data['_redirect']; } else { if (isset($requestName::$defaultRedirect)) { $redirect = $requestName::$defaultRedirect; } else { $redirect = 'home'; // $requestParams['uri']; <-- can't do this, it will cause circular loop because it's getting current URI and not last URI. } } $redirectURI = $redirect; $HTTPVars = array(); if ($response->message != 'Success!') { if ($response->code) { $HTTPVars['error'] = $response->message; } else { $HTTPVars['success'] = $response->message; } } RouteManager::GoToPageURI($redirectURI, $HTTPVars); } }
/** * Initialize Metalsite Application * * ## Overview * This will load in the autoloader for third party embedded services * that are managed by `Composer`. After, it will either load in or use * provided application configurations to setup various managers. * * The `SessionManager` will then be initialized to establish and parse any * session data and then `RouteManager` will be invoked to begin loading * in HTTP variables and render a view corresponding to the data given. * * @uses NotificationManager * @uses SessionManager * @uses RouteManager * * @param array|null An array of app settings, or else use null to load. * @return true Always unless fatal error or exception is thrown. * * @version 2015-07-05.1 * @since 0.5.1b * @author TronNet DevOps [Sean Murray] <*****@*****.**> */ public static function Init($appConfig = null) { DebugManager::Log("Initializing Application", '@'); DebugManager::Log("Including Composer Autoloader"); /** * Auto generated autoloader created for and by Composer installed * libraries. */ require_once SATANBARBARA_APP_PATH . '/includes/vendor/autoload.php'; DebugManager::Log("Checking if an application config was provided."); if (!isset($appConfig)) { DebugManager::Log("No config provided, loading one from: " . SATANBARBARA_APP_CONFIG_PATH); $appConfig = json_decode(file_get_contents(SATANBARBARA_APP_CONFIG_PATH), true); } self::SetConfig($appConfig); DebugManager::Log("Setting DebugManager file to: " . $appConfig['logs']['app']['path']); DebugManager::Init(); SessionManager::Init(); RouteManager::Init(); return true; }
public static function getPublic() { return static::$instance->getPublic(); }