Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassesScanner.php
Go to the documentation of this file.
1 <?php
8 
12 
14 {
18  protected $excludePatterns = [];
19 
23  private $fileResults = [];
24 
28  private $generationDirectory;
29 
34  public function __construct(array $excludePatterns = [], DirectoryList $directoryList = null)
35  {
36  $this->excludePatterns = $excludePatterns;
37  if ($directoryList === null) {
38  $directoryList = ObjectManager::getInstance()->get(DirectoryList::class);
39  }
40  $this->generationDirectory = $directoryList->getPath(DirectoryList::GENERATION);
41  }
42 
49  public function addExcludePatterns(array $excludePatterns)
50  {
51  $this->excludePatterns = array_merge($this->excludePatterns, $excludePatterns);
52  }
53 
62  public function getList($path)
63  {
64 
65  $realPath = realpath($path);
66  $isGeneration = strpos($realPath, $this->generationDirectory) === 0;
67 
68  // Generation folders should not have their results cached since they may actually change during compile
69  if (!$isGeneration && isset($this->fileResults[$realPath])) {
70  return $this->fileResults[$realPath];
71  }
72  if (!(bool)$realPath) {
73  throw new FileSystemException(
74  new \Magento\Framework\Phrase('The "%1" path is invalid. Verify the path and try again.', [$path])
75  );
76  }
77  $recursiveIterator = new \RecursiveIteratorIterator(
78  new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS),
79  \RecursiveIteratorIterator::SELF_FIRST
80  );
81 
82  $classes = $this->extract($recursiveIterator);
83  if (!$isGeneration) {
84  $this->fileResults[$realPath] = $classes;
85  }
86  return $classes;
87  }
88 
95  private function extract(\RecursiveIteratorIterator $recursiveIterator)
96  {
97  $classes = [];
98  foreach ($recursiveIterator as $fileItem) {
100  if ($fileItem->isDir() || $fileItem->getExtension() !== 'php' || $fileItem->getBasename()[0] == '.') {
101  continue;
102  }
103  $fileItemPath = $fileItem->getRealPath();
104  foreach ($this->excludePatterns as $excludePatterns) {
105  if ($this->isExclude($fileItemPath, $excludePatterns)) {
106  continue 2;
107  }
108  }
109  $fileScanner = new FileClassScanner($fileItemPath);
110  $classNames = $fileScanner->getClassNames();
111  $this->includeClasses($classNames, $fileItemPath);
112  $classes = array_merge($classes, $classNames);
113  }
114  return $classes;
115  }
116 
122  private function includeClasses(array $classNames, $fileItemPath)
123  {
124  foreach ($classNames as $className) {
125  if (!class_exists($className)) {
126  require_once $fileItemPath;
127  return true;
128  }
129  }
130  return false;
131  }
132 
140  private function isExclude($fileItemPath, $patterns)
141  {
142  if (!is_array($patterns)) {
143  $patterns = (array)$patterns;
144  }
145  foreach ($patterns as $pattern) {
146  if (preg_match($pattern, str_replace('\\', '/', $fileItemPath))) {
147  return true;
148  }
149  }
150  return false;
151  }
152 }
$pattern
Definition: website.php:22
__construct(array $excludePatterns=[], DirectoryList $directoryList=null)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31