public static function init($filepath, $global = true) { if (!is_file($filepath)) { if (!file_put_contents($filepath, "<?php\n", LOCK_EX)) { throw new \RuntimeException("Cannot write file {$filepath}"); } } if (isset(self::$path2ns[$filepath])) { return self::$path2ns[$filepath]; } $ns = $global ? 'global' : uniqid(true); $data = (array) (require $filepath); $useful = !empty($data['vesion']) && $data['version'] == self::CACHE_VERSION; self::$data[$ns] = $useful ? $data['data'] : array(); self::$path[$ns] = $filepath; self::$isDirty[$ns] = false; self::$path2ns[$filepath] = $ns; if (!self::$registredShutdown) { register_shutdown_function(function () { Cache::save(); }); self::$registredShutdown = true; } return $ns; }
/** @depends testCacheInit */ function testCacheContent() { $obj = new ReflectionClass(__CLASS__); $arr = $obj->getAnnotations(); $this->assertEquals(1, \Notoj\Cache::save()); $this->assertEquals(0, \Notoj\Cache::save()); $content = file_get_contents(CACHE); $this->assertTrue(strpos($content, sha1($obj->getDocComment())) !== FALSE); }
public static function parseDocComment($content, &$isCached = NULL, $localCache = NULL) { if (is_object($content) && is_callable(array($content, 'getDocComment'))) { $content = $content->getDocComment(); } $id = sha1($content); if (isset(self::$internal_cache[$id])) { $isCached = true; return self::$internal_cache[$id]; } $isCached = false; $cached = Cache::Get($id, $found, $localCache); if ($found) { $isCached = true; self::$internal_cache[$id] = new Annotations($cached); return self::$internal_cache[$id]; } $pzToken = new Tokenizer($content); $Parser = new \Notoj_Parser(); $buffer = array(); $isNew = true; do { try { $token = $pzToken->getToken($isNew); if (!$token) { break; } $isNew = false; $Parser->doParse($token[0], $token[1]); } catch (\Exception $e) { $buffer = array_merge($buffer, $Parser->body); $Parser = new \Notoj_Parser(); $isNew = true; } } while (true); try { $Parser->doParse(0, 0); } catch (\Exception $e) { // ignore error } $struct = new Annotations(array_merge($buffer, $Parser->body)); Cache::Set($id, $struct, $localCache); self::$internal_cache[$id] = $struct; return self::$internal_cache[$id]; }
public function readDirectory($path) { $filter = $this->filter; $modtime = filemtime($path); $cached = Cache::get('dir://' . $path, $has, $this->localCache); if ($has && $cached['modtime'] >= $modtime) { if ($this->cacheTs < $modtime) { $this->cacheTs = $modtime; } foreach ($cached['cache'] as $file => $cache) { $this->files[] = $file; $this->addFile(File::fromCache($file, $cache, $this->localCache)); } return; } $this->cached = false; $iter = new RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS); $cache = array(); foreach (new RecursiveIteratorIterator($iter) as $file) { if (!$file->isfile() || $filter && !$filter($file)) { continue; } $rpath = realpath($file->getPathname()); $this->files[] = $rpath; $file = $this->addFile(new File($file->getPathname(), $this->localCache)); $cache[$rpath] = $file->ToCache(); } Cache::set('dir://' . $path, compact('modtime', 'cache'), $this->localCache); return $this->annotations; }
protected function doParse($path) { $modtime = filemtime($path); $cached = Cache::get('file://' . $path, $found, $this->localCache); if ($found && $cached['modtime'] >= $modtime) { $this->cached = true; foreach (unserialize($cached['cache']) as $object) { $this->addObject($object); } return; } $this->cached = false; try { $parser = new ClassInfo($path); } catch (\Exception $e) { // Internal error, probably parsing buggy/invalid php code return; } foreach ($parser->getPHPDocs() as $object) { $this->addObject($object); } $cache = $this->toCache(); $cached = Cache::set('file://' . $path, compact('modtime', 'cache'), $this->localCache); return; }