1 | <?php |
||
21 | class SnappyRouter |
||
22 | { |
||
23 | /** the DI key for the main configuration */ |
||
24 | const KEY_CONFIG = 'config'; |
||
25 | |||
26 | private $config; // the configuration |
||
27 | private $handlers; // array of registered handlers |
||
28 | |||
29 | /** |
||
30 | * The constructor for the service router. |
||
31 | * @param array $config The configuration array. |
||
32 | */ |
||
33 | 8 | public function __construct(ConfigInterface $config) |
|
38 | |||
39 | /** |
||
40 | * Returns the array of registered handlers. |
||
41 | * @return The array of registered handlers. |
||
42 | */ |
||
43 | 7 | public function getHandlers() |
|
47 | |||
48 | /** |
||
49 | * Handles the standard route. Determines the execution environment |
||
50 | * and makes the appropriate call. |
||
51 | * @param string $environment (optional) An optional environment variable, if not |
||
52 | * specified, the method will fallback to php_sapi_name(). |
||
53 | * @return string Returns the encoded response string. |
||
54 | */ |
||
55 | 4 | public function handleRoute($environment = null) |
|
76 | |||
77 | /** |
||
78 | * Handles routing an HTTP request directly. |
||
79 | * @param string $path The URL path from the client. |
||
80 | * @param array $query The query parameters as an array. |
||
81 | * @param array $post The post data as an array. |
||
82 | * @param string $verb The HTTP verb used in the request. |
||
83 | * @return string Returns an encoded string to pass back to the client. |
||
84 | */ |
||
85 | 4 | public function handleHttpRoute($path, $query, $post, $verb) |
|
89 | |||
90 | /** |
||
91 | * Handles routing a CLI request directly. |
||
92 | * @param array $pathComponents The array of path components to the CLI script. |
||
93 | * @return string Returns an encoded string to be output to the CLI. |
||
94 | */ |
||
95 | 3 | public function handleCliRoute($pathComponents) |
|
99 | |||
100 | /** |
||
101 | * Determines which handler is appropriate for this request. |
||
102 | * |
||
103 | * @param bool $isCli True for CLI handlers, false otherwise. |
||
104 | * @param array $handlerParams An array parameters required by the handler. |
||
105 | * @return Returns the handler to be used for the route. |
||
106 | */ |
||
107 | 7 | private function invokeHandler($isCli, $handlerParams) |
|
147 | |||
148 | /** |
||
149 | * Determines which handler is appropriate for this request. |
||
150 | * |
||
151 | * @param bool $isCli True for CLI handlers, false otherwise. |
||
152 | * @param array $checkParams An array parameters for the handler isAppropriate method. |
||
153 | * @return Returns the handler to be used for the route. |
||
154 | */ |
||
155 | 7 | private function determineHandler($isCli, $checkParams) |
|
176 | |||
177 | /** |
||
178 | * Parses the config, sets up the default DI and registers the config |
||
179 | * in the DI. |
||
180 | */ |
||
181 | 8 | private function parseConfig() |
|
196 | |||
197 | // helper to setup the array of handlers |
||
198 | 8 | private function setupHandlers($handlers) |
|
219 | } |
||
220 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: