/** * Fire a filter * * This is used internally to dispatch various events throughout * the Kanso application. It should not really be used externally * unless you really know what you're doing. * * @param string $filterName The name of the filter being fired * @param array $args The arguements to be sent to filter */ public static function apply($filterName, $args) { # Is there a custom callback for the filter? if (isset(self::$filters[$filterName]) && !empty(self::$filters[$filterName])) { # Return the supplied data to filtered $result = $args; # Get all the filters callbacks for the filter $filters = array_keys(self::$filters[$filterName]); # Loop the filter callbacks foreach ($filters as $filter) { # Apply the callback $result = \Kanso\Utility\Callback::apply(self::$callbacks[$filter], $args); } return $result; } else { return $args; } }
/** * Fire an event * * This is used internally to dispatch various events throughout * the Kanso application. It should not really be used externally * unless you really know what you're doing. * * @param string $eventName The name of the event being fired * @param array $args The arguements to be sent to event */ public static function fire($eventName, $args = []) { if (isset(self::$events[$eventName]) && !empty(self::$events[$eventName])) { $events = array_keys(self::$events[$eventName]); foreach ($events as $i) { # Apply the callback \Kanso\Utility\Callback::apply(self::$callbacks[$i], $args); } } }
/** * Loop the routes/methods to match request */ public static function dispatch() { $uri = parse_url(trim($_SERVER['REQUEST_URI'], '/'), PHP_URL_PATH); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); $found_route = false; # check if route is defined without regex if (in_array($uri, self::$routes)) { $route_pos = array_keys(self::$routes, $uri); foreach ($route_pos as $route) { if (self::$methods[$route] == $method) { # Found route $found_route = true; # Apply the callback \Kanso\Utility\Callback::apply(self::$callbacks[$route], self::$callbackArgs[$route]); # Halt on match ? if (self::$halts) { return; } } } } else { # check if defined with regex $pos = 0; foreach (self::$routes as $route) { if (strpos($route, ':') !== false) { $route = str_replace($searches, $replaces, $route); } if (preg_match('#^' . $route . '$#', $uri, $matched)) { if (self::$methods[$pos] == $method) { # Found route $found_route = true; # remove $matched[0] as [1] is the first parameter. array_shift($matched); # Apply the callback \Kanso\Utility\Callback::apply(self::$callbacks[$pos], self::$callbackArgs[$pos]); # Halt on match ? if (self::$halts) { return; } } } $pos++; } } # run the error callback if the route was not found if ($found_route == false) { call_user_func(self::$error_callback[0], self::$error_callback[1]); } }