diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2012-08-03 11:40:43 +0000 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2012-08-03 11:40:43 +0000 |
commit | 4c1215ca04bc682a5165363724a69f9a557304de (patch) | |
tree | 5f0402f4f7b07e5a34ad692bcb47828b30c9fa11 /poc/poc02-compiling-cake | |
parent | 688d58f51120786b099509ea3785a8057ae36265 (diff) | |
download | 2012-php-weave-4c1215ca04bc682a5165363724a69f9a557304de.tar.gz 2012-php-weave-4c1215ca04bc682a5165363724a69f9a557304de.tar.bz2 2012-php-weave-4c1215ca04bc682a5165363724a69f9a557304de.zip |
Etape 2 sur 6743542 : parsing et tentative de mise en cache des AST des ficheir de tout le framework.
Avec dumpAST : 4m37 d'exec.
Avec serializeAST : 3m30 mais tous les fichiers sont vides car json_encode n'encode pas les objets !!!
git-svn-id: file:///var/svn/2012-php-weave/trunk@11 d972a294-176a-4cf9-8ea1-fcd5b0c30f5c
Diffstat (limited to 'poc/poc02-compiling-cake')
4 files changed, 87 insertions, 6 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); diff --git a/poc/poc02-compiling-cake/src/php-weave/cakephp_weaver.class.php b/poc/poc02-compiling-cake/src/php-weave/cakephp_weaver.class.php index 508e509..5e4f7de 100644 --- a/poc/poc02-compiling-cake/src/php-weave/cakephp_weaver.class.php +++ b/poc/poc02-compiling-cake/src/php-weave/cakephp_weaver.class.php @@ -14,20 +14,30 @@ class CakePHPWeaver extends AbstractWeaver { $fw_props=$this->staticEvalDefine($entrypoint_ast, $magics); if (!is_array($fw_props)) throw new Exception("No properties found in '$entrypoint_path'"); - print_r($fw_props); +// print_r($fw_props); $required=array('ROOT','APP_DIR','CAKE_CORE_INCLUDE_PATH','WEBROOT_DIR', 'WWW_ROOT'); $missing=array_diff($required, array_keys($fw_props)); - print_r($missing); +// print_r($missing); assert('count($missing)===0;'); return array_merge(array('entrypoint_path' => $entrypoint_path), $fw_props); } public function parseFrameworkConfig($fw_props) { - + return array(); } public function parseFrameworkCode($fw_props, $fw_conf, $cache_path) { - + $filelist=$this->findAllFiles($fw_props['CAKE_CORE_INCLUDE_PATH'], '/(\.php|\.ctp)$/', '/^\.svn$/'); + //print_r($filelist); + + $env=$fw_props; //TODO : More things here ? + foreach ($filelist as $f) { + //TODO : cache match + $cache_filepath=$cache_path . "/" . sha1($f) . "-" . substr(sha1(print_r($env,true)),-8,8) . ".ast"; + + $ast=$this->parseAndWalk($f, null, $env); + $this->serializeAST($ast, $f, $cache_filepath); + } } public function parseApplicationCode($fw_props, $fw_conf, $fw_extra, $cache_path) { diff --git a/poc/poc02-compiling-cake/src/php-weave/json_serialiser.class.php b/poc/poc02-compiling-cake/src/php-weave/json_serialiser.class.php new file mode 100644 index 0000000..40f55ce --- /dev/null +++ b/poc/poc02-compiling-cake/src/php-weave/json_serialiser.class.php @@ -0,0 +1,12 @@ +<?php +require_once '../vendor/nikic/php-parser/lib/bootstrap.php'; + +class PHPParser_JSONSerialiser implements PHPParser_Serializer,PHPParser_Unserializer { + public function serialize(array $nodes) { + //FIXME : gros problème, ça n'encode pas les objects !!! + return json_encode($nodes); + } + public function unserialize($string) { + return json_decode($string); + } +} diff --git a/poc/poc02-compiling-cake/src/php-weave/main2.php b/poc/poc02-compiling-cake/src/php-weave/main2.php new file mode 100644 index 0000000..faf3ac1 --- /dev/null +++ b/poc/poc02-compiling-cake/src/php-weave/main2.php @@ -0,0 +1,17 @@ +<?php +require_once './cakephp_weaver.class.php'; + +ini_set('xdebug.max_nesting_level', 2000); + +// Main +$weaver=new CakePHPWeaver; +$filelist = $weaver->findAllFiles("../workdir/cache", '/\.ast$/', '/^$/'); +$asts=array(); +foreach ($filelist as $f) { + echo "Unserializing '$f'\n"; + $asts[]=$weaver->unserializeAST($f); +} + +echo memory_get_usage(); + +?> |