Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed missing format() in render() and added %P formatting option

  • Loading branch information...
commit 33fa79afacce3a3f3b136b64ec9979a542e4d98c 1 parent 0dbbd60
@jeffturcotte authored
Showing with 70 additions and 41 deletions.
  1. +30 −37 Anchor.php
  2. +1 −0  README
  3. +39 −4 tests.php
View
67 Anchor.php
@@ -253,7 +253,7 @@ function notFound()
private static $param_types = array(
':' => '[^/]+',
'!' => '[A-Za-z][A-Za-z0-9_]+',
- '%' => '[0-9]+',
+ '^' => '[0-9]+',
'@' => '[A-Za-z]+'
);
@@ -483,13 +483,14 @@ public static function authorize($controller)
}
/**
- * Destroys all routes. This is most useful for running tests
+ * Destroys all hooks and routes. This is most useful for running tests
*
* @return void
*/
public static function clear()
{
self::$routes = array();
+ self::$hooks = array();
}
/**
@@ -585,16 +586,17 @@ public static function &call($callable, $data=NULL)
/**
* Formats a string, replacing symbols with active callback values
*
- * %n => active namespace
- * %N => active parent (outermost) namespace
- * %c => active class
+ * %n => active namespace
+ * %N => active parent/outermost namespace
+ * %c => active class
* %C => active short class
- * %m => active method
+ * %m => active method
* %M => active short method
- * %p => active path
+ * %p => active full path
+ * %P => active class path
*
* @param string $format The string to format
- * @param boolean $underscorize If the active callback values should be underscorized, does not affect active path
+ * @param boolean $underscorize If the active callback values should be underscorized, does not affect active path(s)
* @return string The formatted string
*/
public static function format($format, $underscorize=FALSE, $default=NULL)
@@ -614,14 +616,19 @@ public static function format($format, $underscorize=FALSE, $default=NULL)
// parse the callback
$callback = self::parseCallback($callback);
- // make the path
- $path = self::underscorize($callback->short_class) . '/';
- $path .= self::underscorize($callback->short_method);
-
+ // make the path and class path
+ $short_class = self::underscorize($callback->short_class);
+ $short_method = self::underscorize($callback->short_method);
+
+ $path = "{$short_class}/{$short_method}";
+ $class_path = $short_class;
+
if ($callback->namespace) {
- $path = self::underscorize($callback->namespace) . '/' . $path;
+ $namespace = self::underscorize($callback->namespace);
+ $path = "{$namespace}/{$path}";
+ $class_path = "{$namespace}/{$class_path}";
}
-
+
$formatter_pattern = "/[^%]%.{1}/i";
// set the replacements
@@ -632,7 +639,8 @@ public static function format($format, $underscorize=FALSE, $default=NULL)
'%C' => ($underscorize) ? self::underscorize($callback->short_class) : $callback->short_class,
'%m' => ($underscorize) ? self::underscorize($callback->method) : $callback->method,
'%M' => ($underscorize) ? self::underscorize($callback->short_method) : $callback->short_method,
- '%p' => ($path == '/') ? '' : $path
+ '%p' => ($path == '/') ? '' : $path,
+ '%P' => ($class_path == '/') ? '' : $class_path
);
$formatted = str_replace(
@@ -649,24 +657,6 @@ public static function format($format, $underscorize=FALSE, $default=NULL)
}
/**
- * Easy way to make an basic anchor tag
- *
- * @return string
- */
- public static function make()
- {
- $args = func_get_args();
- $text = array_shift($args);
- $link = call_user_func_array(__CLASS__.'::find', $args);
-
- if ($text === NULL) {
- $text = $link;
- }
-
- return sprintf('<a href="%s">%s</a>', $link, $text);
- }
-
- /**
* Generates a link from a callback/params string
*
* Example usage, without or with : before param name:
@@ -692,6 +682,8 @@ public static function render($callback_key)
if (isset(self::$closure_aliases[$callback])) {
$callback = self::$closure_aliases[$callback];
}
+
+ $callback = self::format($callback);
if (isset($param_values[0])) {
$data =& $param_values[0];
@@ -938,9 +930,9 @@ public static function setRequestPath($request_path) {
*
* @return void
*/
- public static function setHashBangRouting() {
+ public static function setFragmentRouting() {
if (isset($_GET) && isset($_GET['_escaped_fragment_'])) {
- self::configureRequestPath($_GET['_escaped_fragment_']);
+ self::setRequestPath($_GET['_escaped_fragment_']);
}
}
@@ -1007,6 +999,7 @@ private static function buildCallback($route, &$params)
);
unset($params[$short_class_param]);
}
+
if ($short_method == '*' && isset($params[$short_method_param])) {
$short_method = call_user_func_array(
self::$callback_param_formatters['short_method'],
@@ -1243,8 +1236,6 @@ private static function validateCallback($callback)
$callback = self::parseCallback($callback);
-
-
$reflected_method = NULL;
$reflected_call = NULL;
@@ -1563,6 +1554,8 @@ private static function parseCallback($callback)
$wild_pattern = ($callback->short_class == '*') ? '.+' : '.*';
$pattern = '(?P<' . self::$callback_param_names['short_class'] . '>' . str_replace('*', $wild_pattern, $callback->short_class) . ')::' . $pattern;
$derivative_pattern = '(?P<' . self::$callback_param_names['short_class'] . '>.+)::' . $derivative_pattern;
+
+
}
$separator = str_replace('\\', '\\\\', self::$namespace_separator);
View
1  README
@@ -1,3 +1,4 @@
=========================================
= Anchor // a routing library for PHP 5 =
=========================================
+
View
43 tests.php
@@ -11,12 +11,12 @@ public static function setUpBeforeClass() {
Anchor::hook('init', '*::*', 'TestController::init');
Anchor::hook('before', '*::*', 'TestController::before');
Anchor::hook('after', '*::*', 'TestController::after');
+ Anchor::hook('catch Exception', '*::*', 'TestController::catchException');
Anchor::hook('finish', '*::*', 'TestController::finish');
Anchor::add('/', 'TestController::home');
Anchor::add('/:class/:id/:method', '*::*');
Anchor::add('/:class/%id-:slug/:method', '*::*');
- Anchor::add('/:class/:method#:id-:slug', '*::*');
}
public function testAuthorization() {
@@ -48,7 +48,7 @@ public function testCall() {
$this->assertFalse(Anchor::call('TestController::__notAllowedMagic'));
}
- public function testFind() {
+ public function testRender() {
$this->assertEquals(
'/',
Anchor::render('TestController::home')
@@ -67,6 +67,17 @@ public function testHooks() {
$data = Anchor::call('AuthorizedController::index');
$this->assertEquals('ibaf', $data->test);
}
+
+ public function testFormat() {
+ $data = Anchor::call('AuthorizedController::index');
+ $this->assertEquals('AuthorizedController', $data->active_class);
+ $this->assertEquals('AuthorizedController', $data->active_short_class);
+ $this->assertEquals('AuthorizedController::index', $data->active_method);
+ $this->assertEquals('index', $data->active_short_method);
+ $this->assertEquals('authorized_controller/index', $data->active_full_path);
+ $this->assertEquals('authorized_controller', $data->active_class_path);
+
+ }
}
class UnauthorizedController {
@@ -74,17 +85,39 @@ public function index() {}
}
class AuthorizedController {
- public function index() {}
+ public function index($data) {
+ $data->active_class = Anchor::format('%c');
+ $data->active_short_class = Anchor::format('%C');
+ $data->active_method = Anchor::format('%m');
+ $data->active_short_method = Anchor::format('%M');
+ $data->active_full_path = Anchor::format('%p');
+ $data->active_class_path = Anchor::format('%P');
+ }
+
+ public function throwException() {
+ throw new Exception();
+ }
+
+ public function throwExtendingException() {
+ throw new ExtendingException();
+ }
+
+ public static function catchException() {
+ $data->test = 'e';
+ }
public static function init($data) {
$data->test = 'i';
}
+
public static function before($data) {
$data->test .= 'b';
}
+
public static function after($data) {
$data->test .= 'a';
}
+
public static function finish($data) {
$data->test .= 'f';
}
@@ -98,4 +131,6 @@ public function __notAllowedMagic() {}
public function home() {}
public function dynamic() {}
-}
+}
+
+class ExtendingException extends Exception {}
Please sign in to comment.
Something went wrong with that request. Please try again.