summaryrefslogtreecommitdiff
path: root/poc/poc02-compiling-cake/src/php-weave/main.php
blob: 2a4df55c17b7bfa8bcaa37805a763d5214f97c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
require '../vendor/nikic/php-parser/lib/bootstrap.php';
require './visitors.inc.php';

ini_set('xdebug.max_nesting_level', 2000);

function dbg($indent, $text) {
	for($i=0;$i<$indent;$i++) echo ".";
	echo "$text\n";
}

function recursive_parse($src_filepath, $stmts1_filepath, $stmts2_filepath, $level=0) {
	global $parser,$nodeDumper;

	try {
		dbg($level,"Parsing '$src_filepath'");
		$stmts = $parser->parse(file_get_contents($src_filepath));

		if (strlen($stmts1_filepath) > 0 ) {
			dbg($level,"Dumping1 '$src_filepath' AST to '$stmts1_filepath'");
			file_put_contents($stmts1_filepath, $nodeDumper->dump($stmts));
		}

		dbg($level,"Transforming '$src_filepath'");
		$traverser = new PHPParser_NodeTraverser;
/*
		$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver);
		$traverser->addVisitor(new NodeVisitor_NamespaceConverter);
*/
		// FIXME : getcwd is quick and dirty here
		$traverser->addVisitor(new NodeVisitor_PreprocessInclude($level, getcwd(), $src_filepath));
		$stmts = $traverser->traverse($stmts);
		$traverser=null; //Destroy

		if (strlen($stmts2_filepath) > 0) {	
			dbg($level,"Dumping2 '$src_filepath' AST to '$stmts2_filepath'");
			file_put_contents($stmts2_filepath, $nodeDumper->dump($stmts));
		}

	} catch (PHPParser_Error $e) {
	    echo 'Parse Error: ', $e->getMessage();
	}
	return $stmts;
}


// Main
$src_filepath = "./main.php";
#$src_filepath = "./to_parse.php";
$stmts1_filepath = "out/stmts1.ast";
$stmts2_filepath = "out/stmts2.ast";
$dest_filepath = "out/result.php";

$parser = new PHPParser_Parser(new PHPParser_Lexer);
$nodeDumper = new PHPParser_NodeDumper;

$level=0;
$stmts=recursive_parse($src_filepath, $stmts1_filepath, $stmts2_filepath);

dbg(0,"Outputing '$dest_filepath'");
$prettyPrinter = new PHPParser_PrettyPrinter_Zend;
file_put_contents($dest_filepath, "<?php\n" . $prettyPrinter->prettyPrint($stmts) . "\n?>");

?>