summaryrefslogtreecommitdiff
path: root/tests/test01-parsing/src/php-weave/visitors.inc.php
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2012-08-01 08:29:21 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2012-08-01 08:29:21 +0000
commite73d3ed6011892b053b75419a43317c6a07bc763 (patch)
tree68f07771560b856e10bd464163c46cbe566df2a1 /tests/test01-parsing/src/php-weave/visitors.inc.php
parentcfdc1bc2f770341db269279b160d9210cd4b923c (diff)
download2012-php-weave-e73d3ed6011892b053b75419a43317c6a07bc763.tar.gz
2012-php-weave-e73d3ed6011892b053b75419a43317c6a07bc763.tar.bz2
2012-php-weave-e73d3ed6011892b053b75419a43317c6a07bc763.zip
Premier commit.
nikic-PHP-Parser est un outil très prometteur. Le test test01-parsing inclu dans l'AST principal tous les include(<String>) ou string est un chemin relatif facile à résoudre. git-svn-id: file:///var/svn/2012-php-weave/trunk@2 d972a294-176a-4cf9-8ea1-fcd5b0c30f5c
Diffstat (limited to 'tests/test01-parsing/src/php-weave/visitors.inc.php')
-rw-r--r--tests/test01-parsing/src/php-weave/visitors.inc.php91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test01-parsing/src/php-weave/visitors.inc.php b/tests/test01-parsing/src/php-weave/visitors.inc.php
new file mode 100644
index 0000000..3f997ad
--- /dev/null
+++ b/tests/test01-parsing/src/php-weave/visitors.inc.php
@@ -0,0 +1,91 @@
+<?php
+/*
+class NodeVisitor_NamespaceConverter extends PHPParser_NodeVisitorAbstract
+{
+ public function leaveNode(PHPParser_Node $node) {
+ if ($node instanceof PHPParser_Node_Name) {
+ return new PHPParser_Node_Name($node->toString('_'));
+ } elseif ($node instanceof PHPParser_Node_Stmt_Class
+ || $node instanceof PHPParser_Node_Stmt_Interface
+ || $node instanceof PHPParser_Node_Stmt_Function) {
+ $node->name = $node->namespacedName->toString('_');
+ } elseif ($node instanceof PHPParser_Node_Stmt_Const) {
+ foreach ($node->consts as $const) {
+ $const->name = $const->namespacedName->toString('_');
+ }
+ } elseif ($node instanceof PHPParser_Node_Stmt_Namespace) {
+ // returning an array merges is into the parent array
+ return $node->stmts;
+ } elseif ($node instanceof PHPParser_Node_Stmt_Use) {
+ // returning false removed the node altogether
+ return false;
+ }
+ }
+}
+*/
+
+class NodeVisitor_PreprocessInclude extends PHPParser_NodeVisitorAbstract {
+ protected $level;
+ protected $cwd;
+ protected $filepath;
+ protected $prettyPrinter;
+
+ public function __construct($level, $cwd, $filepath) {
+ $this->level=$level;
+ $this->cwd=$cwd;
+ $this->filepath=$filepath;
+ $this->prettyPrinter = new PHPParser_PrettyPrinter_Zend;
+ }
+
+ protected function resolveIncludePath($node) {
+ //FIXME : Quick and dirty
+ // no classpath checks...
+ // no check if compound value like "dirname(__FILE__) . '/PHPParser/Autoloader.php'"
+ return $node->expr->value;
+ }
+
+ public function enterNode(PHPParser_Node $node) {
+ if ($node instanceof PHPParser_Node_Expr_Include) {
+ // Already processed, go out (occurs if an unresolved include has left as is in the previous recursion level)
+ if (is_array($node->attributes) && array_key_exists('NodeVisitor_PreprocessInclude', $node->attributes)) return $node;
+
+ switch($node->type) {
+ case PHPParser_Node_Expr_Include::TYPE_INCLUDE:
+ case PHPParser_Node_Expr_Include::TYPE_REQUIRE:
+ $once=0;
+ break;
+ case PHPParser_Node_Expr_Include::TYPE_INCLUDE_ONCE:
+ case PHPParser_Node_Expr_Include::TYPE_REQUIRE_ONCE:
+ $once=1;
+ echo "TODO : include_once or require_once\n";
+ break;
+ default:
+ echo "FIXME : BUG NodeVisitor_PreprocessInclude::enterNode !!\n";
+ return $node;
+ }
+
+ $path=$this->resolveIncludePath($node);
+
+ //FIXME : No infinite recursion check like test.php contains include('./test.php');
+ // Preprocess include if readable
+ $comment_suffix=" */";
+ if ( ($this->level < 3) && is_readable($path)) {
+ $comment_prefix="/* ";
+ //FIXME : use a closure or sort of here (dependancy injection)
+ //$stmts=recursive_parse($path, "out/rec.out1", "out/rec.out2", $this->level+1);
+ $stmts=recursive_parse($path, "", "", $this->level+1);
+ } else {
+ $comment_prefix="/* UNRESOLVED : ";
+ $node->attributes['NodeVisitor_PreprocessInclude']='unresolved';
+ $stmts=array($node); // Caution : can cause infinite loop (will be tried at the next step)
+ }
+
+ $comment= new PHPParser_Comment($comment_prefix . $this->prettyPrinter->prettyPrint(array($node)) . $comment_suffix);
+
+ // FIXME : Get out this if() that is a trick for not returning here an array instead of a node
+ return new PHPParser_Node_Stmt_If(new PHPParser_Node_Scalar_LNumber(1),array('stmts'=>$stmts),array('comments'=>array($comment)));
+ }
+
+ }
+}
+