← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1810: [urldispatcher] Support callback functions passed as string

 

------------------------------------------------------------
revno: 1810
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt
timestamp: Tue 2010-11-09 21:50:31 +0100
message:
  [urldispatcher] Support callback functions passed as string
  
  Command callbacks passed to the add_route_*() methods now
  also accept strings like 'Foo::bar'. This means external
  commands no longer need to be passed as callback arrays.
modified:
  urldispatcher/urldispatcher.lib.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription
=== modified file 'urldispatcher/urldispatcher.lib.php'
--- urldispatcher/urldispatcher.lib.php	2010-03-26 21:38:30 +0000
+++ urldispatcher/urldispatcher.lib.php	2010-11-09 20:50:31 +0000
@@ -105,6 +105,8 @@
  * call those methods. It is recommended to use static methods, but it is also
  * possible to use an object instance whose methods will be called. See the PHP
  * manual on the callable type conventions and <code>call_user_func()</code>.
+ * Additionally, AnewtURLDispatcher also handles strings like
+ * <code>'ArticleCommand::view'</code> as expected.
  *
  * If you use external commands, the classes that implement these commands can
  * be loaded lazily, i.e. only when they are needed. For example, this would
@@ -377,7 +379,20 @@
 	 private function validate_command($command)
 	 {
 		if (is_string($command))
-			$command = array($this, sprintf('command_%s', $command));
+		{
+			if (strpos($command, '::', 1) !== false)
+			{
+				/* Static method (provided as string): Foo::bar() */
+				$command = explode('::', $command, 2);
+			}
+			else
+			{
+				/* Simple string. Prefix with "command_" and point it to $this,
+				 * i.e. this is a command method implemented in the dispatcher
+				 * class itself. */
+				$command = array($this, sprintf('command_%s', $command));
+			}
+		}
 
 		list ($is_valid, $name_for_error_message) = $this->is_valid_command($command);
 
@@ -394,7 +409,7 @@
 	  * Check whether a command is valid.
 	  *
 	  * \param $command
-	  *   The command to validate
+	  *   The command to validate (as a 2-tuple callback)
 	  *
 	  * \return
 	  *   A 2-tuple with a 'valid' flag and an error message.
@@ -404,9 +419,6 @@
 		$is_valid = false;
 		$name_for_error_message = $command;
 
-		if (is_string($command))
-			$command = array($this, sprintf('command_%s', $command));
-
 		if (is_numeric_array($command) && count($command) == 2 && is_string($command[1]))
 		{
 			if (is_string($command[0]))