/www/slight

To get this branch, use:
bzr branch http://bzr.ed.am/www/slight
1 by Tim Marston
initial commit
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
 * @method  static void map($name, $callback) Creates a custom framework method.
13
 * @method  static void register($name, $class, array $params = array(), $callback = null) Registers a class to a framework method.
14
 * @method  static void before($name, $callback) Adds a filter before a framework method.
15
 * @method  static void after($name, $callback) Adds a filter after a framework method.
16
 * @method  static void path($path) Adds a path for autoloading classes.
17
 * @method  static mixed get($key) Gets a variable.
18
 * @method  static void set($key, $value) Sets a variable.
19
 * @method  static bool has($key) Checks if a variable is set.
20
 * @method  static void clear($key = null) Clears a variable.
21
 * @method  static void start() Starts the framework.
22
 * @method  static void stop() Stops the framework and sends a response.
23
 * @method  static void halt($code = 200, $message = '') Stop the framework with an optional status code and message.
24
 * @method  static void route($pattern, $callback) Maps a URL pattern to a callback.
25
 * @method  static void redirect($url, $code = 303) Redirects to another URL.
26
 * @method  static void render($file, array $data = null, $key = null) Renders a template file.
27
 * @method  static void error($exception) Sends an HTTP 500 response.
28
 * @method  static void notFound() Sends an HTTP 404 response.
29
 * @method  static void etag($id, $type = 'strong') Performs ETag HTTP caching.
30
 * @method  static void lastModified($time) Performs last modified HTTP caching.
31
 * @method  static void json($data, $code = 200, $encode = true) Sends a JSON response.
32
 * @method  static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true) Sends a JSONP response.
33
 */
34
class Flight {
35
    /**
36
     * Framework engine.
37
     *
38
     * @var object
39
     */
40
    private static $engine;
41
42
    // Don't allow object instantiation
43
    private function __construct() {}
44
    private function __destruct() {}
45
    private function __clone() {}
46
47
    /**
48
     * Handles calls to static methods.
49
     *
50
     * @param string $name Method name
51
     * @param array $params Method parameters
52
     * @return mixed Callback results
53
     */
54
    public static function __callStatic($name, $params) {
55
        static $initialized = false;
56
57
        if (!$initialized) {
58
            require_once __DIR__.'/autoload.php';
59
60
            self::$engine = new \flight\Engine();
61
62
            $initialized = true;
63
        }
64
65
        return \flight\core\Dispatcher::invokeMethod(array(self::$engine, $name), $params);
66
    }
67
68
    /**
69
     * @return object Application instance
70
     */
71
    public static function app() {
72
        return self::$engine;
73
    }
74
}