Browse Source

Merge pull request #786 from jasonhinkle/master

Phreeze fix for query test warnings and fortunes test fail
mcocciaTE 11 years ago
parent
commit
591e7ce314

+ 1 - 1
phreeze/benchmark_config

@@ -5,7 +5,7 @@
       "setup_file": "setup",
       "json_url": "/index.php?json",
       "db_url": "/index.php?db",
-      "query_url": "/index.php?db&queries=",
+      "query_url": "/index.php?query&queries=",
       "fortune_url": "/index.php?fortunes",
       "update_url": "/index.php?updates&queries=",
       "plaintext_url": "/index.php?plaintext",

+ 3 - 0
phreeze/index.php

@@ -23,6 +23,7 @@ $route_map = array(
 		'GET:' => array('route' => 'Test.JSON'),
 		'GET:json' => array('route' => 'Test.JSON'),
 		'GET:db' => array('route' => 'Test.DB'),
+		'GET:query' => array('route' => 'Test.Query'),
 		'GET:fortunes' => array('route' => 'Test.Fortunes'),
 		'GET:updates' => array('route' => 'Test.Updates'),
 		'GET:plaintext' => array('route' => 'Test.PlainText')
@@ -30,6 +31,8 @@ $route_map = array(
 
 $router = new SimpleRouter('/','Test.JSON',$route_map);
 
+Dispatcher::$FAST_LOOKUP = true;
+
 Dispatcher::Dispatch(
 	$phreezer,
 	null,

+ 34 - 14
phreeze/libs/Controller/TestController.php

@@ -40,24 +40,44 @@ class TestController extends Controller
 	public function DB()
 	{
 		require_once("Model/World.php");
-		
-		// Read number of queries to run from URL parameter
+
+		$id = mt_rand(1, 10000);
+		$world = $this->Phreezer->Get("World",$id);
+		$this->RenderJSON($world,'',true);
+	}
+	
+	
+	/**
+	 * Test route that connects to the database and outputs
+	 * the number of rows specified in the querystring argument "queries"
+	 */
+	public function Query()
+	{
+		require_once("Model/World.php");
+	
+		// Read number of queries to run from URL parameter
 		$query_count = RequestUtil::Get('queries',1);
-
-		$arr = array();
-		
-		for ($i = 0; $i < $query_count; $i++) {
 		
-			$id = mt_rand(1, 10000);
-			
-			$world = $this->Phreezer->Get("World",$id);
-			
-			// convert the Phreezable object into a simple structure for output
-			$arr[] = array('id'=>$world->Id,'randomNumber'=>$world->Randomnumber);
+		// make sure the query count paramter is in range
+		if (!is_numeric($query_count)) {
+			$query_count = 1;
 		}
-		
-		$this->RenderJSON($arr);
+		else {
+			$query_count = max(1,min($query_count,500));
+		}
+		
+		$arr = array();
+			
+		for ($i = 0; $i < $query_count; $i++) {
+				
+			$id = mt_rand(1, 10000);
 
+			// convert the Phreezable object into a simple structure for output
+			$arr[] = $this->Phreezer->Get("World",$id)->ToObject();
+		}
+			
+		$this->RenderJSON($arr);
+	
 	}
 	
 	/**

+ 10 - 1
phreeze/libs/Model/World.php

@@ -39,11 +39,20 @@ class World extends WorldDAO
 		// the controller create/update methods validate before saving.  this will be a
 		// redundant validation check, however it will ensure data integrity at the model
 		// level based on validation rules.  comment this line out if this is not desired
-		if (!$this->Validate()) throw new Exception('Unable to Save World: ' .  implode(', ', $this->GetValidationErrors()));
+		// if (!$this->Validate()) throw new Exception('Unable to Save World: ' .  implode(', ', $this->GetValidationErrors()));
 
 		// OnSave must return true or eles Phreeze will cancel the save operation
 		return true;
 	}
+	
+	/**
+	 * @return multitype:NULL boolean
+	 */
+	public function ToObject($options = null)
+	{
+		$obj = array('id'=>$this->Id,'randomNumber'=>$this->Randomnumber);
+		return $obj;
+	}
 
 }
 

+ 25 - 5
phreeze/libs/verysimple/Phreeze/Dispatcher.php

@@ -12,7 +12,7 @@ require_once("verysimple/Util/ExceptionThrower.php");
  * @author     VerySimple Inc.
  * @copyright  1997-2007 VerySimple, Inc.
  * @license    http://www.gnu.org/licenses/lgpl.html  LGPL
- * @version    2.6
+ * @version    2.7
  */
 class Dispatcher
 {
@@ -21,6 +21,14 @@ class Dispatcher
 	 * @var boolean default = true
 	 */
 	static $IGNORE_DEPRECATED = true;
+	
+	/**
+	 * FAST_LOOKUP mode instructs the dispatcher to assume that the controller and method
+	 * supplied by the router are valid and not do any checking for the existance of
+	 * the controller file or the method before trying to call it
+	 * @var boolean use fast lookup mode if true 
+	 */
+	static $FAST_LOOKUP = false;
 
 	/**
 	 * This is a case-insensitive version of file_exists
@@ -59,16 +67,28 @@ class Dispatcher
 	 */
 	static function Dispatch($phreezer,$renderEngine,$action='',$context=null,$router=null)
 	{
-		if ($router == null)
-		{
+		if ($router == null) {
 			require_once('GenericRouter.php');
 			$router = new GenericRouter();
 		}
 
+		// get the route and normalize the controller name
 		list($controller_param,$method_param) = $router->GetRoute( $action );
-
-		// normalize the input
 		$controller_class = $controller_param."Controller";
+	
+		if (self::$FAST_LOOKUP) {
+
+			if (!class_exists($controller_class)) {
+				$controller_file = "Controller/$controller_class.php";
+				include_once $controller_file;
+			}
+			
+			$controller = new $controller_class($phreezer,$renderEngine,$context,$router);
+			$controller->$method_param();
+			
+			return true;
+		}
+		
 		
 		// if the controller was in a sub-directory, get rid of the directory path
 		$slashPos = strpos($controller_class,'/');

+ 3 - 8
phreeze/templates/TestFortunes.php

@@ -1,16 +1,11 @@
 <!DOCTYPE html>
 <html>
-<head>
-<title>Fortunes</title>
-</head>
+<head><title>Fortunes</title></head>
 <body>
 <table>
-<tr>
-<th>id</th>
-<th>message</th>
-</tr>
+<tr><th>id</th><th>message</th></tr>
 <?php foreach ($model['fortunes'] as $fortune) {
-	echo '<tr><td>' . $fortune->Id . '</td><td>' . htmlspecialchars($fortune->Message) . '</td></tr>' . "\n";
+	echo '<tr><td>' . $fortune->Id . '</td><td>' . htmlspecialchars($fortune->Message, ENT_QUOTES | ENT_HTML5) . '</td></tr>';
 } ?>
 </table>
 </body>