Go\Instrument\ClassLoading\CachePathManager::flushCacheState PHP Method

flushCacheState() public method

Flushes the cache state into the file
public flushCacheState ( )
    public function flushCacheState()
    {
        if (!empty($this->newCacheState) && is_writable($this->cacheDir)) {
            $fullCacheMap = $this->newCacheState + $this->cacheState;
            $cachePath = substr(var_export($this->cacheDir, true), 1, -1);
            $rootPath = substr(var_export($this->appDir, true), 1, -1);
            $cacheData = '<?php return ' . var_export($fullCacheMap, true) . ';';
            $cacheData = strtr($cacheData, array('\'' . $cachePath => 'AOP_CACHE_DIR . \'', '\'' . $rootPath => 'AOP_ROOT_DIR . \''));
            $fullCacheFileName = $this->cacheDir . self::CACHE_FILE_NAME;
            file_put_contents($fullCacheFileName, $cacheData, LOCK_EX);
            // For cache files we don't want executable bits by default
            chmod($fullCacheFileName, $this->fileMode & ~0111);
            if (function_exists('opcache_invalidate')) {
                opcache_invalidate($fullCacheFileName, true);
            }
            $this->cacheState = $this->newCacheState + $this->cacheState;
            $this->newCacheState = [];
        }
    }

Usage Example

 /**
  * Warms up the cache.
  *
  * @param string $cacheDir The cache directory
  */
 public function warmUp($cacheDir)
 {
     $options = $this->aspectKernel->getOptions();
     $oldCacheDir = $this->cachePathManager->getCacheDir();
     $this->cachePathManager->setCacheDir($cacheDir . '/aspect');
     $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
     $iterator = $enumerator->enumerate();
     set_error_handler(function ($errno, $errstr, $errfile, $errline) {
         throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
     });
     $errors = array();
     foreach ($iterator as $file) {
         $realPath = $file->getRealPath();
         try {
             // This will trigger creation of cache
             file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ . SourceTransformingLoader::FILTER_IDENTIFIER . "/resource=" . $realPath);
         } catch (\Exception $e) {
             $errors[$realPath] = $e;
         }
     }
     restore_error_handler();
     $this->cachePathManager->flushCacheState();
     $this->cachePathManager->setCacheDir($oldCacheDir);
 }