diff options
Diffstat (limited to 'poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php')
-rw-r--r-- | poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php b/poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php index cebe62a..f9eb513 100644 --- a/poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php +++ b/poc/poc02-compiling-cake/src/php-weave/abstract_weaver.class.php @@ -1,5 +1,6 @@ <?php -require '../vendor/nikic/php-parser/lib/bootstrap.php'; +require_once '../vendor/nikic/php-parser/lib/bootstrap.php'; +require_once 'json_serialiser.class.php'; function dbg($indent, $text) { for($i=0;$i<$indent;$i++) echo "."; @@ -10,6 +11,8 @@ abstract class AbstractWeaver { protected $parser; protected $nodeDumper; protected $prettyPrinter; + protected $serializer; + protected $unserializer; protected $ast_dump_before_walk; protected $ast_dump_after_walk; @@ -17,6 +20,8 @@ abstract class AbstractWeaver { $this->parser = new PHPParser_Parser(new PHPParser_Lexer); $this->nodeDumper = new PHPParser_NodeDumper; $this->prettyPrinter = new PHPParser_PrettyPrinter_Zend; + $this->serializer = new PHPParser_JSONSerialiser; + $this->unserializer = &$this->serializer; $this->ast_dump_before_walk=""; $this->ast_dump_after_walk=""; } @@ -79,6 +84,7 @@ abstract class AbstractWeaver { dbg($level,"Parsing '$src_filepath'"); $stmts = $this->parser->parse(file_get_contents($src_filepath)); + //TODO : using an attribute here is not very userfriendly $this->dumpAST($stmts, $src_filepath, $this->ast_dump_before_walk); if ($traverser) { @@ -200,11 +206,47 @@ abstract class AbstractWeaver { public function dumpAST($ast, $ast_title, $dest_filepath) { if (is_array($ast) && strlen($dest_filepath) > 0 ) { - dbg($level,"Dumping '$ast_title,' AST to '$dest_filepath'"); + dbg(0,"Dumping '$ast_title,' AST to '$dest_filepath'"); file_put_contents($dest_filepath, $this->nodeDumper->dump($ast)); } } + public function serializeAST($ast, $ast_title, $dest_filepath) { +// if (is_array($ast) && strlen($dest_filepath) > 0 ) { + file_put_contents($dest_filepath, $this->serializer->serialize($ast)); +// } + } + + public function unserializeAST($src_filepath) { + return $this->serializer->unserialize($src_filepath); + } + + public function findAllFiles($basepath, $regexMatch, $regexPrune, $already_found=array(), $level=0) { + //dbg($level,"findAllFiles('$basepath', '$regex')"); + $found=$already_found; + + if ( $files=scandir($basepath) ) { + foreach ($files as $f) { + if (preg_match($regexPrune, $f)===1) continue; + + $f_path=$basepath . DIRECTORY_SEPARATOR . $f; + if (is_dir($f_path)) { + if ( !( $f=="." || $f==".." ) ) { + $found += $this->findAllFiles($f_path, $regexMatch, $regexPrune, $found, $level+1); + } + } elseif ( is_readable($f_path) ) { + //dbg($level, "preg_match('$regexMatch', '$f')" ); + if (preg_match($regexMatch, $f)===1) { + // A matching file has found in the list + $found[] = $f_path; + } + } + } + } + + return $found; + } + // Framework specific code abstract public function detectFramework($sourcetree_rootpath); abstract public function parseFrameworkConfig($fw_props); |