1 | <?php |
||
23 | class SnappyRouter |
||
24 | { |
||
25 | /** the DI key for the main configuration */ |
||
26 | const KEY_CONFIG = 'config'; |
||
27 | |||
28 | private $config; // the configuration |
||
29 | private $handlers; // array of registered handlers |
||
30 | |||
31 | /** |
||
32 | * @var LoggerInterface |
||
33 | */ |
||
34 | private $logger; |
||
35 | |||
36 | /** |
||
37 | * The constructor for the service router. |
||
38 | * @param array $config The configuration array. |
||
39 | */ |
||
40 | 8 | public function __construct(ConfigInterface $config) |
|
46 | |||
47 | /** |
||
48 | * Configure SnappyRouter to log its actions. |
||
49 | * |
||
50 | * @param LoggerInterface $logger |
||
51 | */ |
||
52 | 1 | public function setLogger(LoggerInterface $logger) |
|
56 | |||
57 | /** |
||
58 | * Returns the array of registered handlers. |
||
59 | * @return The array of registered handlers. |
||
60 | */ |
||
61 | 7 | public function getHandlers() |
|
65 | |||
66 | /** |
||
67 | * Handles the standard route. Determines the execution environment |
||
68 | * and makes the appropriate call. |
||
69 | * @param string $environment (optional) An optional environment variable, if not |
||
70 | * specified, the method will fallback to php_sapi_name(). |
||
71 | * @return string Returns the encoded response string. |
||
72 | */ |
||
73 | 4 | public function handleRoute($environment = null) |
|
74 | { |
||
75 | 4 | if (null === $environment) { |
|
76 | 3 | $environment = PHP_SAPI; |
|
77 | } |
||
78 | |||
79 | switch ($environment) { |
||
80 | 4 | case 'cli': |
|
81 | 1 | case 'phpdbg': |
|
82 | 3 | $components = empty($_SERVER['argv']) ? array() : $_SERVER['argv']; |
|
83 | 3 | return $this->handleCliRoute($components).PHP_EOL; |
|
84 | default: |
||
85 | 1 | $queryPos = strpos($_SERVER['REQUEST_URI'], '?'); |
|
86 | 1 | $path = (false === $queryPos) ? $_SERVER['REQUEST_URI'] : substr($_SERVER['REQUEST_URI'], 0, $queryPos); |
|
87 | 1 | return $this->handleHttpRoute( |
|
88 | 1 | $path, |
|
89 | 1 | $_GET, |
|
90 | 1 | $_POST, |
|
91 | 1 | isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET' |
|
92 | ); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Handles routing an HTTP request directly. |
||
98 | * @param string $path The URL path from the client. |
||
99 | * @param array $query The query parameters as an array. |
||
100 | * @param array $post The post data as an array. |
||
101 | * @param string $verb The HTTP verb used in the request. |
||
102 | * @return string Returns an encoded string to pass back to the client. |
||
103 | */ |
||
104 | 4 | public function handleHttpRoute($path, $query, $post, $verb) |
|
109 | |||
110 | /** |
||
111 | * Handles routing a CLI request directly. |
||
112 | * @param array $pathComponents The array of path components to the CLI script. |
||
113 | * @return string Returns an encoded string to be output to the CLI. |
||
114 | */ |
||
115 | 3 | public function handleCliRoute($pathComponents) |
|
120 | |||
121 | /** |
||
122 | * Determines which handler is appropriate for this request. |
||
123 | * |
||
124 | * @param bool $isCli True for CLI handlers, false otherwise. |
||
125 | * @param array $handlerParams An array parameters required by the handler. |
||
126 | * @return Returns the handler to be used for the route. |
||
127 | */ |
||
128 | 7 | private function invokeHandler($isCli, $handlerParams) |
|
151 | |||
152 | /** |
||
153 | * Attempts to mop up after an exception during handler invocation. |
||
154 | * |
||
155 | * @param \Exception $exception The exception that occurred during invocation. |
||
156 | * @param HandlerInterface $activeHandler The active handler, or null. |
||
157 | * @param bool $isCli True for CLI handlers, false otherwise. |
||
158 | * @return mixed Returns a handler-dependent response type, usually a string. |
||
159 | */ |
||
160 | 4 | private function handleInvocationException($exception, $activeHandler, $isCli) |
|
189 | |||
190 | /** |
||
191 | * Determines which handler is appropriate for this request. |
||
192 | * |
||
193 | * @param bool $isCli True for CLI handlers, false otherwise. |
||
194 | * @param array $checkParams An array parameters for the handler isAppropriate method. |
||
195 | * @return Returns the handler to be used for the route. |
||
196 | */ |
||
197 | 7 | private function determineHandler($isCli, $checkParams) |
|
218 | |||
219 | /** |
||
220 | * Parses the config, sets up the default DI and registers the config |
||
221 | * in the DI. |
||
222 | */ |
||
223 | 8 | private function parseConfig() |
|
238 | |||
239 | // helper to setup the array of handlers |
||
240 | 8 | private function setupHandlers($handlers) |
|
261 | } |
||
262 |