public static function serve($routes) { ToroHook::fire('before_request'); $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (isset($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : $path_info); $discovered_handler = null; $regex_matches = array(); if (isset($routes[$path_info])) { $discovered_handler = $routes[$path_info]; } else { if ($routes) { $tokens = array(':string' => '([a-zA-Z]+)', ':number' => '([0-9]+)', ':alpha' => '([a-zA-Z0-9-_]+)'); foreach ($routes as $pattern => $handler_name) { $pattern = strtr($pattern, $tokens); if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) { $discovered_handler = $handler_name; $regex_matches = $matches; break; } } } } if ($discovered_handler && class_exists($discovered_handler)) { unset($regex_matches[0]); $handler_instance = new $discovered_handler(); if (self::is_xhr_request() && method_exists($discovered_handler, $request_method . '_xhr')) { header('Content-type: application/json'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); $request_method .= '_xhr'; } if (method_exists($handler_instance, $request_method)) { ToroHook::fire('before_handler'); call_user_func_array(array($handler_instance, $request_method), $regex_matches); ToroHook::fire('after_handler'); } else { ToroHook::fire('404'); } } else { ToroHook::fire('404'); } ToroHook::fire('after_request'); }
/** * Autoaction * * @param string $var * @param string $httpMethod */ protected function _autoaction($var, $httpMethod = 'get') { $method = $this->urlToActionName($var, $httpMethod); if (method_exists($this, $method)) { return $this->{$method}(); } else { \ToroHook::fire('404', array("error" => "Controller " . get_class($this) . " does not contain {$method} action", "path_info" => $this->_pathInfo)); } }
/** * Oauth post handler. */ public function post() { ToroHook::fire('405', "GET"); // method not allowed }
public static function serve($routes) { ToroHook::fire('before_request', compact('routes')); $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; if (!empty($_SERVER['PATH_INFO'])) { $path_info = $_SERVER['PATH_INFO']; } elseif (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') { $path_info = $_SERVER['ORIG_PATH_INFO']; } else { if (!empty($_SERVER['REQUEST_URI'])) { $path_info = strpos($_SERVER['REQUEST_URI'], '?') > 0 ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']; } } $regex = '/\\.[^.\\s]{3,4}$/'; $extension = array(); preg_match($regex, $path_info, $extension); // Access using: // /route/path/id // /route/path/id.json if (empty($extension) || 1 == count($extension) && '.json' == strtolower($extension[0])) { header('Content-Type: application/json'); } else { die('Unsupported file extension'); } // Remove extension if set if (1 == count($extension)) { $path_info = preg_replace($regex, '', $path_info); } $discovered_handler = null; $regex_matches = array(); if (isset($routes[$path_info])) { $discovered_handler = $routes[$path_info]; } elseif ($routes) { $tokens = array(':string' => '([a-zA-Z]+)', ':number' => '([0-9]+)', ':alpha' => '([a-zA-Z0-9-_]+)'); foreach ($routes as $pattern => $handler_name) { $pattern = strtr($pattern, $tokens); if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) { $discovered_handler = $handler_name; $regex_matches = $matches; break; } } } $result = null; $handler_instance = null; if ($discovered_handler) { if (is_string($discovered_handler)) { $handler_instance = new $discovered_handler(); } elseif (is_callable($discovered_handler)) { $handler_instance = $discovered_handler(); } } if ($handler_instance) { unset($regex_matches[0]); if (self::is_xhr_request() && method_exists($handler_instance, $request_method . '_xhr')) { header('Content-type: application/json'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); $request_method .= '_xhr'; } if (method_exists($handler_instance, $request_method)) { ToroHook::fire('before_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); $result = call_user_func_array(array($handler_instance, $request_method), $regex_matches); ToroHook::fire('after_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); } else { ToroHook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } } else { ToroHook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } ToroHook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); }
public function serve() { ToroHook::fire('before_request'); $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $path_info; $path_info = isset($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : $path_info; $discovered_handler = NULL; $regex_matches = array(); $method_arguments = NULL; foreach ($this->_handler_route_pairs as $handler) { list($pattern, $handler_name) = $handler; if ($path_info == $pattern) { $discovered_handler = $handler_name; $regex_matches = array($path_info, preg_replace('/^\\//', '', $path_info)); $method_arguments = $this->get_argument_overrides($handler); break; } else { $pattern = str_replace('/', '\\/', $pattern); if (preg_match('/^\\/' . $pattern . '\\/?$/', $path_info, $matches)) { $discovered_handler = $handler_name; $regex_matches = $matches; $method_arguments = $this->get_argument_overrides($handler); break; } } } if ($discovered_handler && class_exists($discovered_handler)) { unset($regex_matches[0]); $handler_instance = new $discovered_handler(); if (!$method_arguments) { $method_arguments = $regex_matches; } // XHR (must come first), iPad, mobile catch all if ($this->xhr_request() && method_exists($discovered_handler, $request_method . '_xhr')) { header('Content-type: application/json'); header('Pragma: no-cache'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); $request_method .= '_xhr'; } else { if ($this->ipad_request() && method_exists($discovered_handler, $request_method . '_ipad')) { $request_method .= '_ipad'; } else { if ($this->mobile_request() && method_exists($discovered_handler, $request_method . '_mobile')) { $request_method .= '_mobile'; } } } ToroHook::fire('before_handler'); call_user_func_array(array($handler_instance, $request_method), $method_arguments); ToroHook::fire('after_handler'); } else { header('HTTP/1.0 404 Not Found'); echo '404 Not Found'; exit; } ToroHook::fire('after_request'); }
public static function serve($routes) { ToroHook::fire('before_request', compact('routes')); // Determine action (and set default) if (empty($_SERVER['REQUEST_METHOD'])) { $_SERVER['REQUEST_METHOD'] = 'GET'; } $action = strtolower($_SERVER['REQUEST_METHOD']); // e.g. get, put, post, delete $path_info = '/'; if (!empty($_SERVER['PATH_INFO'])) { $path_info = $_SERVER['PATH_INFO']; } else { if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') { $path_info = $_SERVER['ORIG_PATH_INFO']; } else { if (!empty($_SERVER['REQUEST_URI'])) { $path_info = strpos($_SERVER['REQUEST_URI'], '?') > 0 ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']; } } } $discovered_handler = null; $regex_matches = array(); $arguments = array(); if (isset($routes[$path_info])) { $discovered_handler = $routes[$path_info]; } else { if ($routes) { $tokens = array(':string' => '([a-zA-Z]+)', ':number' => '([0-9]+)', ':alpha' => '([a-zA-Z0-9-_]+)', ':action' => '([a-zA-Z0-9-_/]+)'); // Search through routes and find first match foreach ($routes as $pattern => $handler_name) { $pattern = strtr($pattern, $tokens); if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) { $discovered_handler = $handler_name; $regex_matches = $matches; $params = isset($regex_matches[1]) ? explode("/", $regex_matches[1]) : array(); // Determine action and arguments if (count($params) > 1) { // @todo add different parsers here...possibly pass route function in routes.json $action .= ucfirst($params[0]); unset($params[0]); // $int = 1; foreach ($params as $param) { $arguments[] = $param; /* // if even param if($int % 2 == 0) $action .= ucfirst($param); else $arguments[] = $param; $int++; */ } } else { unset($regex_matches[0]); $arguments = $regex_matches; // Toro compatible } // error_log("regex_matches: ".print_r($regex_matches, true)); // error_log("action: $action"); // error_log("arguments: ".print_r($arguments, true)); break; } } } } $result = null; $handler_instance = null; if ($discovered_handler) { if (is_string($discovered_handler)) { $handler_instance = new $discovered_handler(); } elseif (is_callable($discovered_handler)) { $handler_instance = $discovered_handler(); } } if ($handler_instance) { if (self::is_xhr_request() && method_exists($handler_instance, $action . '_xhr')) { header('Content-type: application/json'); // @todo support xml header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header("Access-Control-Allow-Origin: *"); // @todo make this a parameter? header('Pragma: no-cache'); $action .= '_xhr'; $handler_instance->setIsXhrRequest(1); } if (method_exists($handler_instance, $action)) { try { $handler_instance->setPathInfo($path_info); ToroHook::fire('before_handler', compact('routes', 'discovered_handler', 'action', 'arguments')); $handler_instance->_before(); // Action call call_user_func_array(array($handler_instance, $action), $arguments); $handler_instance->_after(); $handler_instance->send(); // render the response and return the data ToroHook::fire('after_handler', compact('routes', 'discovered_handler', 'action', 'arguments', 'result')); } catch (\Exception $e) { ToroHook::fire('500', array('error' => $e->getMessage(), 'path_info' => $path_info)); } } else { ToroHook::fire('404', compact('discovered_handler', 'action', 'arguments', 'path_info')); } } else { ToroHook::fire('404', compact('discovered_handler', 'action', 'arguments', 'path_info')); } ToroHook::fire('after_request', compact('routes', 'discovered_handler', 'action', 'arguments', 'result')); }