Permalink
Browse files

Moved stuff out of mojo.lib and into path.lib and added path_match().…

… Other mods related to function naming convention... don't have clarity on this yet :(
  • Loading branch information...
1 parent 1c6bf2b commit e8c736075ae279b6c834ca5cce46112b2cf21f68 @sandeepshetty sandeepshetty committed Jul 21, 2009
View
@@ -1,6 +1,6 @@
<?php
- requires ('helpers', 'webserver');
+ requires ('helpers', 'webserver', 'path');
function map_requests_to_handlers($custom_routes=array())
{
@@ -60,81 +60,23 @@ function matching_route_found($request, $routes)
{
foreach ($routes as $route)
{
- $path_pattern = path_pattern($route['path']);
+ //TODO handle missing keys in route
$request_query = !empty($request['query']);
$route_query = (isset($route['query']) and $route['query']);
- //TODO handle missing keys in route
+ $method_matches = (is_equal($request['method'], $route['method']) or is_equal('', $route['method']));
+ $defaults = isset($route['defaults']) ? $route['defaults'] : array();
+ $path_matches = path_match($route['path'], $request['path'], $defaults);
+ $query_matches = is_equal($request_query, $route_query);
- if ((is_equal($request['method'], $route['method']) or is_equal('', $route['method']))
- and is_equal(preg_match($path_pattern, $request['path'], $matches), 1)
- and is_equal($request_query, $route_query))
+ if ($method_matches and $path_matches and $query_matches)
{
- return isset($route['defaults'])
- ? array_merge($route['defaults'], $matches)
- : $matches;
+ return $path_matches;
}
}
return false;
}
- //TODO: convert all \{ and \} to \x00<curllystart>, \x00<curllyend>
- function path_pattern($pattern)
- {
- $pattern = convert_optional_parts_to_regex($pattern);
- $pattern = convert_named_parts_to_regex($pattern);
- $pattern = strtr($pattern, array('/' => '\/'));
- return "/^$pattern\$/";
- }
- function convert_optional_parts_to_regex($pattern)
- {
- $optional_parts_pattern = '/\[([^\]\[]*)\]/';
- $replacement = '(\1)?';
-
- while (true)
- {
- $regex_pattern = preg_replace($optional_parts_pattern, $replacement, $pattern);
- if (!is_equal($regex_pattern, $pattern))
- {
- $pattern = $regex_pattern;
- }
- else break;
- }
-
- return $pattern;
- }
-
- function convert_named_parts_to_regex($pattern)
- {
- $named_parts = '/{([^}]*)}/';
- $pattern = preg_replace_callback($named_parts, 'named_part_replacement_callback', $pattern);
- return $pattern;
- }
-
- function named_part_replacement_callback($matches)
- {
- return convert_named_part_filters_to_regex($matches, named_part_filters());
- }
-
- function convert_named_part_filters_to_regex($matches, $filters)
- {
- if (str_contains(':', $matches[1]))
- {
- list($subpattern_name, $pattern) = explode(':', $matches[1], 2);
- $pattern = isset($filters[$pattern]) ? $filters[$pattern] : $pattern;
- return "(?P<$subpattern_name>$pattern)";
- }
- else
- {
- return "(?P<{$matches[1]}>{$filters['segment']})";
- }
- }
-
- function named_part_filters()
- {
- require dirname(__FILE__).DIRECTORY_SEPARATOR.'filters.config.php';
- return isset($filters) ? $filters : array();
- }
function handler_func_exists($matches)
{
View
@@ -6,27 +6,27 @@
'path'=>'/[{handler}/].*',
'query'=>true,
'defaults'=>array('handler'=>'',
- 'func'=>'query_')),
+ 'func'=>'query')),
array('method'=>'GET',
'path'=>'/[{handler}/]',
'defaults'=>array('handler'=>'',
- 'func'=>'home_')),
+ 'func'=>'home')),
array('method'=>'GET',
'path'=>'/[{handler}/].*',
'defaults'=>array('handler'=>'',
- 'func'=>'show_')),
+ 'func'=>'show')),
//TODO should put & delete work on collections /, /users/ ??
array('method'=>'PUT',
'path'=>'/[{handler}/].*',
'defaults'=>array('handler'=>'',
- 'func'=>'save_')),
+ 'func'=>'save')),
array('method'=>'DELETE',
'path'=>'/[{handler}/].*',
'defaults'=>array('handler'=>'',
- 'func'=>'delete_')),
+ 'func'=>'delete')),
array('method'=>'POST',
'path'=>'/[{handler}/]{func}',
File renamed without changes.
View
@@ -0,0 +1,130 @@
+<?php
+
+/* path.lib.php
+ *
+ *
+ *
+ * function email($str){return preg_replace('/^(.*)\/(.*)/', '$2@$1', $str);}
+ * Authors: Sandeep Shetty email('gmail.com/sandeep.shetty')
+ *
+ * Copyright (C) 2005 - date('Y') Collaboration Science,
+ * http://collaborationscience.com/
+ *
+ * This file is part of Swx.
+ *
+ * Swx is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * Swx is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * To read the license please visit http://www.gnu.org/copyleft/gpl.html
+ *
+ *
+ *----------------------------------------------------------------------------
+ */
+
+
+ function segment_count($path)
+ {
+ return ($sub_segments = _sub_segments($path, 0)) ? count($sub_segments) : 0;
+ }
+
+
+ function sub_path($path, $start, $length=NULL)
+ {
+ $slash = (is_equal(substr($path, 0, 1), '/')) ? '/' : '';
+ return ($sub_segments = _sub_segments($path, $start, $length)) ? $slash.implode("/", $sub_segments) : false;
+ }
+
+
+ function sub_segment($path, $index=0)
+ {
+ if ($sub_segments = _sub_segments($path, $index, 1)) return $sub_segments[0];
+ return false;
+ }
+
+ function _sub_segments($path, $start, $length=NULL)
+ {
+ $segments = explode('/', $path);
+ if (is_equal(substr($path, 0, 1), '/')) array_shift($segments);
+ $sub_segments = (is_null($length)) ? array_slice($segments, $start) : array_slice($segments, $start, $length);
+ if (empty($sub_segments)) return false;
+ return $sub_segments;
+ }
+
+
+ function path_match($path_pattern, $path, $dafaults=array())
+ {
+ $pattern = path_pattern_to_pattern($path_pattern);
+ if (is_equal(preg_match($pattern, $path, $matches), 1))
+ {
+ return array_merge($dafaults, $matches);
+ }
+ else $dafaults;
+ }
+
+ //TODO: convert all \{ and \} to \x00<curllystart>, \x00<curllyend>
+ function path_pattern_to_pattern($pattern)
+ {
+ $pattern = convert_optional_parts_to_regex($pattern);
+ $pattern = convert_named_parts_to_regex($pattern);
+ $pattern = strtr($pattern, array('/' => '\/'));
+ return "/^$pattern\$/";
+ }
+
+ function convert_optional_parts_to_regex($pattern)
+ {
+ $optional_parts_pattern = '/\[([^\]\[]*)\]/';
+ $replacement = '(\1)?';
+
+ while (true)
+ {
+ $regex_pattern = preg_replace($optional_parts_pattern, $replacement, $pattern);
+ if (!is_equal($regex_pattern, $pattern))
+ {
+ $pattern = $regex_pattern;
+ }
+ else break;
+ }
+
+ return $pattern;
+ }
+
+ function convert_named_parts_to_regex($pattern)
+ {
+ $named_parts = '/{([^}]*)}/';
+ $pattern = preg_replace_callback($named_parts, 'named_part_replacement_callback', $pattern);
+ return $pattern;
+ }
+
+ function named_part_replacement_callback($matches)
+ {
+ return convert_named_part_filters_to_regex($matches, named_part_filters());
+ }
+
+ function convert_named_part_filters_to_regex($matches, $filters)
+ {
+ if (str_contains(':', $matches[1]))
+ {
+ list($subpattern_name, $pattern) = explode(':', $matches[1], 2);
+ $pattern = isset($filters[$pattern]) ? $filters[$pattern] : $pattern;
+ return "(?P<$subpattern_name>$pattern)";
+ }
+ else
+ {
+ return "(?P<{$matches[1]}>{$filters['segment']})";
+ }
+ }
+
+ function named_part_filters()
+ {
+ require dirname(__FILE__).DIRECTORY_SEPARATOR.'filters.config.php';
+ return isset($filters) ? $filters : array();
+ }
+
+?>
@@ -0,0 +1,66 @@
+<?php
+
+
+ function test__sub_segments()
+ {
+ should_return(array(''), when_passed('', 0));
+ should_return(array(''), when_passed('/', 0));
+ should_return(array('', ''), when_passed('//', 0));
+ should_return(false, when_passed('/foo', 2));
+ should_return(array('foo', 'bar', ''), when_passed('/foo/bar/', 0));
+ should_return(array('foo', 'bar'), when_passed('/foo/bar', 0));
+ should_return(array('bar'), when_passed('/foo/bar', -1));
+ should_return(array('foo'), when_passed('/foo/bar', 0, 1));
+ should_return(array('bar'), when_passed('/foo/bar/', 1, -1));
+ should_return(array('bar'), when_passed('/foo/bar/', -2, -1));
+ should_return(array('foo', 'bar'), when_passed('/foo/bar', -10));
+ should_return(array('foo', 'bar'), when_passed('/foo/bar', 0, 10));
+ should_return(array('foo'), when_passed('foo/bar', 0, 1));
+ }
+
+
+ function test_sub_segment()
+ {
+ should_return('', when_passed('', 0));
+ should_return('', when_passed('/', 0));
+ should_return('', when_passed('//', 0));
+ should_return(false, when_passed('/foo', 2));
+ should_return('bar', when_passed('/foo/bar', -1));
+ should_return('foo', when_passed('/foo/bar', -10));
+ should_return('foo', when_passed('foo/bar', 0));
+ should_return('foo', when_passed('/foo/bar'));
+ }
+
+
+ function test_segment_count()
+ {
+ should_return(1, when_passed(''));
+ should_return(1, when_passed('/'));
+ should_return(2, when_passed('//'));
+ should_return(1, when_passed('foo'));
+ should_return(1, when_passed('/foo'));
+ should_return(2, when_passed('/foo/'));
+ should_return(2, when_passed('/foo/bar'));
+ should_return(3, when_passed('/foo/bar/'));
+ should_return(3, when_passed('/foo/bar/baz'));
+ }
+
+
+ function test_sub_path()
+ {
+ should_return('', when_passed('', 0));
+ should_return('/', when_passed('/', 0));
+ should_return('//', when_passed('//', 0));
+ should_return(false, when_passed('/foo', 2));
+ should_return('/foo/bar/', when_passed('/foo/bar/', 0));
+ should_return('/foo/bar', when_passed('/foo/bar', 0));
+ should_return('/bar', when_passed('/foo/bar', -1));
+ should_return('/foo', when_passed('/foo/bar', 0, 1));
+ should_return('/bar', when_passed('/foo/bar/', 1, -1));
+ should_return('/bar', when_passed('/foo/bar/', -2, -1));
+ should_return('/foo/bar', when_passed('/foo/bar', -10));
+ should_return('/foo/bar', when_passed('/foo/bar', 0, 10));
+ should_return('foo', when_passed('foo/bar', 0, 1));
+ }
+
+?>
Oops, something went wrong.

0 comments on commit e8c7360

Please sign in to comment.