From 63674c02bfc795a96aba583fe504bebbfbdc579f Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Thu, 7 Jan 2016 22:52:26 -0500 Subject: [PATCH] Optionally cache statments --- lib/FileChecker.php | 47 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/FileChecker.php b/lib/FileChecker.php index a32f1f21d..341fd1454 100644 --- a/lib/FileChecker.php +++ b/lib/FileChecker.php @@ -12,6 +12,7 @@ class FileChecker protected $_namespace; protected $_aliased_classes = []; + protected static $_cache_dir = null; protected static $_file_checkers = []; public function __construct($file_name) @@ -21,11 +22,7 @@ class FileChecker public function check($check_classes = true) { - $contents = file_get_contents($this->_file_name); - - $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - - $stmts = $parser->parse($contents); + $stmts = self::_getStatments($this->_file_name); foreach ($stmts as $stmt) { if ($stmt instanceof PhpParser\Node\Stmt\Class_) { @@ -44,6 +41,8 @@ class FileChecker } self::$_file_checkers[$this->_file_name] = $this; + + return $stmts; } public function _checkNamespace(PhpParser\Node\Stmt\Namespace_ $namespace, $check_classes) @@ -88,4 +87,42 @@ class FileChecker $file_checker->check(false); return $file_checker->getAbsoluteClass($class); } + + protected static function _getStatments($file_name) + { + $contents = file_get_contents($file_name); + + $stmts = []; + + if (self::$_cache_dir) { + $key = md5($contents); + + $cache_location = self::$_cache_dir . '/' . $key; + + if (is_readable($cache_location)) { + $stmts = unserialize(file_get_contents($cache_location)); + } + } + + if (!$stmts) { + $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + + $stmts = $parser->parse($contents); + } + + if (self::$_cache_dir) { + if (!file_exists(self::$_cache_dir)) { + mkdir(self::$_cache_dir); + } + + file_put_contents($cache_location, serialize($stmts)); + } + + return $stmts; + } + + public static function setCacheDir($cache_dir) + { + self::$_cache_dir = $cache_dir; + } }