예제 #1
0
/**
 * Runs Frank
 */
function run()
{
    if (Frank::was_run() !== true) {
        $output = Frank::call();
        foreach (Frank::middleware() as $middleware) {
            if (gettype($middleware) == 'string') {
                $middleware = new $middleware();
            }
            $output = $middleware->call($output);
        }
        Frank::output($output);
    }
}
예제 #2
0
 private static function getInstance()
 {
     if (self::$instance) {
         return self::$instance;
     }
     return self::$instance = new self();
 }
예제 #3
0
/**
 * Adds a middleware to use
 *
 * @param string or object $middleware
 */
function middleware($middleware)
{
    Frank::add_middleware($middleware);
}
예제 #4
0
 /**
  * Finds a function to call from a request path
  *
  * @param string $method	Request method
  * @param string $request	Request path
  * @return array			Function to call and parameters to call it with
  */
 private static function route($method, $request)
 {
     $params = array('splat' => array(), 'captures' => array());
     if (isset(self::$routes[$method][$request])) {
         $function = self::$routes[$method][$request];
     } elseif (($route = reverse_preg_match_array($request, array_keys(self::$routes[$method]), array('#\\*(/|$)#', '/:[A-Za-z0-9]+/'))) && $route !== false) {
         $route = end($route);
         // The only different things between the request url and the
         // route should be the regex's, so we get them.
         $changes = url_diff($request, $route);
         $function = self::$routes[$method][$route];
         foreach ($changes as $index => $value) {
             if (preg_match('/^:/', $index)) {
                 //Strip leading :
                 $index = preg_replace('/^:/', '', $index);
                 $params[$index] = $value;
             } elseif ($index == '*') {
                 $params['splat'][] = $value;
             } else {
                 $params['captures'][] = $value;
             }
         }
     }
     if (!isset($function)) {
         self::$status = 404;
         //We don't want to display headers for command line
         if (!defined('STDIN')) {
             header("HTTP/1.1 404 Not Found");
         }
         if (isset(self::$errors['404'])) {
             $function = self::$errors['404'];
         } else {
             $function = create_function('', 'echo "We couldn\'t find that page.";');
         }
     }
     return array($function, $params);
 }
예제 #5
0
 /**
  * Gets the output from a url
  *
  * @param 	string	$url	 path (in Frank) to get the data for
  * @param	array	$options set of options to pass to Frank::exec()
  * @param	string	$method  type of request to the server (i.e. get, post, put, delete)
  * @return 	string			 data the function outputs
  */
 private function get_data($url, $options = array(), $method = 'get')
 {
     Frank::set_request($url);
     Frank::set_method($method);
     Frank::set_run(true);
     Frank::set_status(array(200, array(), false));
     $output = Frank::call($options);
     foreach (Frank::middleware() as $middleware) {
         if (gettype($middleware) == 'string') {
             $middleware = new $middleware();
         }
         $output = $middleware->call($output);
     }
     return $output[2];
 }