1<?php 2/** 3 * Flight: An extensible micro-framework. 4 * 5 * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> 6 * @license MIT, http://flightphp.com/license 7 */ 8 9/** 10 * The Flight class is a static representation of the framework. 11 * 12 * Core. 13 * @method static void start() Starts the framework. 14 * @method static void path($path) Adds a path for autoloading classes. 15 * @method static void stop() Stops the framework and sends a response. 16 * @method static void halt($code = 200, $message = '') Stop the framework with an optional status code and message. 17 * 18 * Routing. 19 * @method static void route($pattern, $callback) Maps a URL pattern to a callback. 20 * @method static \flight\net\Router router() Returns Router instance. 21 * 22 * Extending & Overriding. 23 * @method static void map($name, $callback) Creates a custom framework method. 24 * @method static void register($name, $class, array $params = array(), $callback = null) Registers a class to a framework method. 25 * 26 * Filtering. 27 * @method static void before($name, $callback) Adds a filter before a framework method. 28 * @method static void after($name, $callback) Adds a filter after a framework method. 29 * 30 * Variables. 31 * @method static void set($key, $value) Sets a variable. 32 * @method static mixed get($key) Gets a variable. 33 * @method static bool has($key) Checks if a variable is set. 34 * @method static void clear($key = null) Clears a variable. 35 * 36 * Views. 37 * @method static void render($file, array $data = null, $key = null) Renders a template file. 38 * @method static \flight\template\View view() Returns View instance. 39 * 40 * Request & Response. 41 * @method static \flight\net\Request request() Returns Request instance. 42 * @method static \flight\net\Response response() Returns Response instance. 43 * @method static void redirect($url, $code = 303) Redirects to another URL. 44 * @method static void json($data, $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSON response. 45 * @method static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSONP response. 46 * @method static void error($exception) Sends an HTTP 500 response. 47 * @method static void notFound() Sends an HTTP 404 response. 48 * 49 * HTTP Caching. 50 * @method static void etag($id, $type = 'strong') Performs ETag HTTP caching. 51 * @method static void lastModified($time) Performs last modified HTTP caching. 52 */ 53class Flight { 54 /** 55 * Framework engine. 56 * 57 * @var \flight\Engine 58 */ 59 private static $engine; 60 61 // Don't allow object instantiation 62 private function __construct() {} 63 private function __destruct() {} 64 private function __clone() {} 65 66 /** 67 * Handles calls to static methods. 68 * 69 * @param string $name Method name 70 * @param array $params Method parameters 71 * @return mixed Callback results 72 * @throws \Exception 73 */ 74 public static function __callStatic($name, $params) { 75 $app = Flight::app(); 76 77 return \flight\core\Dispatcher::invokeMethod(array($app, $name), $params); 78 } 79 80 /** 81 * @return \flight\Engine Application instance 82 */ 83 public static function app() { 84 static $initialized = false; 85 86 if (!$initialized) { 87 require_once __DIR__.'/autoload.php'; 88 89 self::$engine = new \flight\Engine(); 90 91 $initialized = true; 92 } 93 94 return self::$engine; 95 } 96} 97