Skip to content

Commit

Permalink
Mimic_Request_Store now implements basic scoring of request matches
Browse files Browse the repository at this point in the history
Unlikely to be the final algorithm, but should be good enough for
testing in the wild. Request method is given a weighting of 1000,
header and query keys are scored evenly. Exact matches get more
points than wildcards.
  • Loading branch information
acoulton committed Nov 5, 2011
1 parent 566ae6c commit a7f58d7
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions classes/mimic/request/store.php
Expand Up @@ -182,6 +182,14 @@ protected function _get_formatter($content_type)
return new $class; return new $class;
} }


/**
* Searches within a request index file to identify the most specific match
* for the passed in request. Returns an array of request/response data if
* found, or NULL if not.
*
* @param Request $request
* @return array
*/
protected function _search_index($request) protected function _search_index($request)
{ {
// Check the index file exists and load it into memory // Check the index file exists and load it into memory
Expand Down Expand Up @@ -219,15 +227,19 @@ protected function _search_index($request)
continue; continue;
} }


$matching_scores[$index_key] = $scores; // Combine the scores to give an overall value
// Request method always trumps everything else, header and query are equal
$score = (1000 * $scores['method']) + $scores['header'] + $scores['query'];
$matching_scores[$index_key] = $score;


} }


// Temporarily take the first match // Take the highest scored item
if ( ! $matching_scores) if ( ! $matching_scores)
{ {
return FALSE; return FALSE;
} }
arsort($matching_scores);
reset($matching_scores); reset($matching_scores);
$matched_request = $request_index[key($matching_scores)]; $matched_request = $request_index[key($matching_scores)];


Expand All @@ -239,6 +251,14 @@ protected function _search_index($request)
return $matched_request; return $matched_request;
} }


/**
* Allocates a match score for the request method
*
* @param string $request_method The method from the request
* @param array $scores The current array of scores for this criteria
* @param string $criteria_method The method from the index entry
* @return boolean
*/
protected function _score_match_method($request_method, & $scores, $criteria_method) protected function _score_match_method($request_method, & $scores, $criteria_method)
{ {
if ($request_method == $criteria_method) if ($request_method == $criteria_method)
Expand All @@ -261,6 +281,16 @@ protected function _score_match_method($request_method, & $scores, $criteria_met
return TRUE; return TRUE;
} }


/**
* Allocates a match score for the request header/query array, including
* allowing for Mimic_Request_Wildcard_Require values.
*
* @param array $request_array The header/query array from the request
* @param array $scores The current array of scores for this criteria
* @param array $criteria_array The header/query array from the index entry
* @param string $type query|header
* @return boolean
*/
protected function _score_match_array($request_array, & $scores, $criteria_array, $type) protected function _score_match_array($request_array, & $scores, $criteria_array, $type)
{ {
$score = 0; $score = 0;
Expand Down

0 comments on commit a7f58d7

Please sign in to comment.