1 | <?php |
||
7 | class Application { |
||
8 | /// Properties /// |
||
9 | protected static $instances; |
||
10 | |||
11 | /** |
||
12 | * @var Request The current request. |
||
13 | */ |
||
14 | public $request; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | * @var Response The current response. |
||
19 | */ |
||
20 | public $response; |
||
21 | |||
22 | /** |
||
23 | * @var array An array of route objects. |
||
24 | */ |
||
25 | protected $routes; |
||
26 | |||
27 | /// Methods /// |
||
28 | |||
29 | 44 | public function __construct($name = 'default') { |
|
34 | |||
35 | public static function instance($name = 'default') { |
||
36 | if (!isset(self::$instances[$name])) { |
||
37 | self::$instances[$name] = new Application($name); |
||
38 | } |
||
39 | return self::$instances[$name]; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get all of the matched routes for a request. |
||
44 | * |
||
45 | * @param Request $request The {@link Request} to match against. |
||
46 | * @return array An array of arrays corresponding to matching routes and their args. |
||
47 | */ |
||
48 | 44 | public function matchRoutes(Request $request) { |
|
49 | 44 | $result = array(); |
|
50 | |||
51 | 44 | foreach ($this->routes as $route) { |
|
52 | 44 | $matches = $route->matches($request, $this); |
|
53 | 44 | if ($matches) { |
|
54 | 44 | $result[] = array($route, $matches); |
|
55 | } |
||
56 | } |
||
57 | 44 | return $result; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Add a new route. |
||
62 | * |
||
63 | * @param string|Route $pathOrRoute The path to the route or the {@link Route} object itself. |
||
64 | * @param callable|string|null $callback Either a callback to map the route to or a string representing |
||
65 | * a format for {@link sprintf()}. |
||
66 | * @return Route Returns the route that was added. |
||
67 | * @throws \InvalidArgumentException Throws an exceptio if {@link $path} isn't a string or {@link Route}. |
||
68 | */ |
||
69 | 44 | public function route($pathOrRoute, $callback = null) { |
|
70 | 44 | if (is_object($pathOrRoute) && $pathOrRoute instanceof Route) { |
|
71 | $route = $pathOrRoute; |
||
72 | 44 | } elseif (is_string($pathOrRoute) && $callback !== null) { |
|
73 | 44 | $route = Route::create($pathOrRoute, $callback); |
|
74 | } else { |
||
75 | throw new \InvalidArgumentException("Argument #1 must be either a Garden\\Route or a string.", 500); |
||
76 | } |
||
77 | 44 | $this->routes[] = $route; |
|
78 | 44 | return $route; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Route to a GET request. |
||
83 | * |
||
84 | * @param string $pattern The url pattern to match. |
||
85 | * @param callable $callback The callback to execute on the route. |
||
86 | * @return CallbackRoute Returns the new route. |
||
87 | */ |
||
88 | 1 | public function get($pattern, callable $callback) { |
|
91 | |||
92 | /** |
||
93 | * Route to a POST request. |
||
94 | * |
||
95 | * @param string $pattern The url pattern to match. |
||
96 | * @param callable $callback The callback to execute on the route. |
||
97 | * @return CallbackRoute Returns the new route. |
||
98 | */ |
||
99 | public function post($pattern, callable $callback) { |
||
102 | |||
103 | /** |
||
104 | * Route to a PUT request. |
||
105 | * |
||
106 | * @param string $pattern The url pattern to match. |
||
107 | * @param callable $callback The callback to execute on the route. |
||
108 | * @return CallbackRoute Returns the new route. |
||
109 | */ |
||
110 | public function put($pattern, callable $callback) { |
||
113 | |||
114 | /** |
||
115 | * Route to a PATCH request. |
||
116 | * |
||
117 | * @param string $pattern The url pattern to match. |
||
118 | * @param callable $callback The callback to execute on the route. |
||
119 | * @return CallbackRoute Returns the new route. |
||
120 | */ |
||
121 | public function patch($pattern, callable $callback) { |
||
124 | |||
125 | /** |
||
126 | * Route to a DELETE request. |
||
127 | * |
||
128 | * @param string $pattern The url pattern to match. |
||
129 | * @param callable $callback The callback to execute on the route. |
||
130 | * @return CallbackRoute Returns the new route. |
||
131 | */ |
||
132 | public function delete($pattern, callable $callback) { |
||
135 | |||
136 | /** |
||
137 | * Run the application against a {@link Request}. |
||
138 | * |
||
139 | * @param Request|null $request A {@link Request} to run the application against or null to run against a request |
||
140 | * on the current environment. |
||
141 | * @return mixed Returns a response appropriate to the request's ACCEPT header. |
||
142 | */ |
||
143 | 44 | public function run(Request $request = null) { |
|
197 | |||
198 | /** |
||
199 | * Finalize the result from a dispatch. |
||
200 | * |
||
201 | * @param mixed $result The result of the dispatch. |
||
202 | * @return mixed Returns relevant debug data or processes the response. |
||
203 | * @throws \Exception Throws an exception when finalizing internal content types and the result is an exception. |
||
204 | */ |
||
205 | 44 | protected function finalize($result) { |
|
249 | } |
||
250 |