Go\Instrument\FileSystem\Enumerator::getFilter PHP Method

getFilter() public method

Returns a filter callback for enumerating files
public getFilter ( ) : Closure
return Closure
    public function getFilter()
    {
        $rootDirectory = $this->rootDirectory;
        $includePaths = $this->includePaths;
        $excludePaths = $this->excludePaths;
        return function (\SplFileInfo $file) use($rootDirectory, $includePaths, $excludePaths) {
            if ($file->getExtension() !== 'php') {
                return false;
            }
            $fullPath = $this->getFileFullPath($file);
            $dir = dirname($fullPath);
            // Do not touch files that not under rootDirectory
            if (strpos($fullPath, $rootDirectory) !== 0) {
                return false;
            }
            if (!empty($includePaths)) {
                $found = false;
                foreach ($includePaths as $includePattern) {
                    if (fnmatch($includePattern, $dir)) {
                        $found = true;
                        break;
                    }
                }
                if (!$found) {
                    return false;
                }
            }
            foreach ($excludePaths as $excludePattern) {
                if (fnmatch($excludePattern, $dir)) {
                    return false;
                }
            }
            return true;
        };
    }

Usage Example

 /**
  * {@inheritDoc}
  */
 public function findFile($class)
 {
     static $isAllowedFilter = null, $isProduction = false;
     if (!$isAllowedFilter) {
         $isAllowedFilter = $this->fileEnumerator->getFilter();
         $isProduction = !$this->options['debug'];
     }
     $file = $this->original->findFile($class);
     if ($file) {
         $cacheState = isset($this->cacheState[$file]) ? $this->cacheState[$file] : null;
         if ($cacheState && $isProduction) {
             $file = $cacheState['cacheUri'] ?: $file;
         } elseif ($isAllowedFilter(new \SplFileInfo($file))) {
             // can be optimized here with $cacheState even for debug mode, but no needed right now
             $file = FilterInjectorTransformer::rewrite($file);
         }
     }
     return $file;
 }