From f1481d95065efa14abbde945fa372ae907e9b062 Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Tue, 11 Aug 2009 22:11:33 +0000 Subject: [PATCH] [2.0] Added dump function for AST nodes to help debugging complex queries --- lib/Doctrine/ORM/Query/AST/Node.php | 50 +++++++++++++++++++++++++++++ lib/Doctrine/ORM/Query/Parser.php | 2 ++ 2 files changed, 52 insertions(+) diff --git a/lib/Doctrine/ORM/Query/AST/Node.php b/lib/Doctrine/ORM/Query/AST/Node.php index dec961977..442a7e73a 100644 --- a/lib/Doctrine/ORM/Query/AST/Node.php +++ b/lib/Doctrine/ORM/Query/AST/Node.php @@ -35,4 +35,54 @@ namespace Doctrine\ORM\Query\AST; abstract class Node { abstract public function dispatch($sqlWalker); + + /** + * Dumps the AST Node into a string representation for information purpose only + * + * @return string + */ + public function __toString() + { + return $this->dump($this); + } + + public function dump($obj) + { + static $ident = 0; + + $str = ''; + + if ($obj instanceof Node) { + $str .= get_class($obj) . '(' . PHP_EOL; + $props = get_object_vars($obj); + + foreach ($props as $name => $prop) { + $ident += 4; + $str .= str_repeat(' ', $ident) . '"' . $name . '": ' + . $this->dump($prop) . ',' . PHP_EOL; + $ident -= 4; + } + + $str .= str_repeat(' ', $ident) . ')'; + } else if (is_array($obj)) { + $ident += 4; + $str .= 'array('; + $some = false; + + foreach ($obj as $k => $v) { + $str .= PHP_EOL . str_repeat(' ', $ident) . '"' + . $k . '" => ' . $this->dump($v) . ','; + $some = true; + } + + $ident -= 4; + $str .= ($some ? PHP_EOL . str_repeat(' ', $ident) : '') . ')'; + } else if (is_object($obj)) { + $str .= 'instanceof(' . get_class($obj) . ')'; + } else { + $str .= var_export($obj, true); + } + + return $str; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 4928748e3..8e689ffc4 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -257,6 +257,8 @@ class Parser { // Parse & build AST $AST = $this->QueryLanguage(); + + echo PHP_EOL . ((string) $AST) . PHP_EOL; // Check for end of string if ($this->_lexer->lookahead !== null) {