コード例 #1
0
 private function processUri($uri)
 {
     $hasBodyParams = false;
     $queryString = null;
     $queryStringHasParams = false;
     // get rid of any leading /
     if ($uri[0] == '/') {
         $uri = substr($uri, 1);
     }
     // do we have any query string params?
     $hasQueryString = strripos($uri, '?');
     if ($hasQueryString !== false) {
         $queryString = substr($uri, $hasQueryString + 1);
         $queryStringHasParams = strpos($queryString, '{') === false ? false : true;
         $queryString = explode('&', $queryString);
         $uri = substr($uri, 0, $hasQueryString);
         $this->key = '?';
     }
     // cleanup and prepare:
     // do we end with a /
     $uri = strripos($uri, '/') == strlen($uri) - 1 ? substr($uri, 0, -1) : $uri;
     $hasBodyParams = strpos($uri, '{') === false ? false : true;
     $segments = explode('/', $uri);
     // parse the main URI body
     if ($hasBodyParams) {
         $this->processArray($segments);
     } else {
         $this->parts = $segments;
         $this->key .= implode("", $segments);
     }
     if (is_array($queryString)) {
         $this->processArray($queryString, true);
     }
     $this->hash = AMServiceManager::generateKey($this->key, $this->parameter_count);
 }
コード例 #2
0
 public function start()
 {
     $endpoints = null;
     $method = strtoupper($_SERVER['REQUEST_METHOD']);
     $termination = strripos($_SERVER['REQUEST_URI'], '?');
     $path = substr($_SERVER['REQUEST_URI'], 0, $termination === false ? strlen($_SERVER['REQUEST_URI']) : $termination);
     if ($path[0] == '/') {
         $path = substr($path, 1);
     }
     if ($path[strlen($path) - 1] == '/') {
         $path = substr($path, 0, strlen($path) - 1);
     }
     $segments = explode("/", $path);
     if ($_SERVER['QUERY_STRING']) {
         $queryString = explode('&', $_SERVER['QUERY_STRING']);
         foreach ($queryString as $part) {
             list($argName, $argValue) = explode('=', $part);
             $segments[] = $argName;
             $segments[] = $argValue;
         }
     }
     $segmentsLength = count($segments);
     $branch = $this->contract->endpoints[$method];
     $endpoints = array();
     foreach ($branch as $service) {
         $match = array_intersect($segments, $service->parts);
         $params = $segmentsLength - count($match);
         if ($params == $service->parameter_count && count(array_diff($service->parts, $match)) == 0) {
             $input = strlen($_SERVER['QUERY_STRING']) ? '?' . implode("", $match) : implode("", $match);
             $key = AMServiceManager::generateKey($input, $params);
             if (array_key_exists($key, $branch)) {
                 $args = array_diff($segments, $service->parts);
                 // we apply some weighting.  matching path arguments hold more importance that parameter arguments
                 // so if we have an endnpoint defined as /{arg} vs /users
                 // /users will be given a higher priority than /{arg}
                 $score = $params + count($match) * 2;
                 $endpoints[] = array('score' => $score, 'arguments' => $args, 'service' => $service);
             }
         }
     }
     $choices = count($endpoints);
     if ($choices) {
         if ($choices > 1) {
             $this->execute($endpoints[0]['service'], $endpoints[0]['arguments']);
         } else {
             usort($endpoints, array($this, "sort_endpoints_score"));
             $selection = array_pop($endpoints);
             $this->execute($selection['service'], $selection['arguments']);
         }
     } else {
         AMServiceManager::not_found();
     }
 }