Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

DirectoryScanner.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 7.00 KiB


001  <?php
002  /**
003   * Zend Framework (http://framework.zend.com/)
004   *
005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
006   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
007   * @license   http://framework.zend.com/license/new-bsd New BSD License
008   */
009   
010  namespace Zend\Code\Scanner;
011   
012  use RecursiveDirectoryIterator;
013  use RecursiveIteratorIterator;
014  use Zend\Code\Exception;
015   
016  use function array_keys;
017  use function array_merge;
018  use function is_array;
019  use function is_dir;
020  use function is_string;
021  use function pathinfo;
022  use function realpath;
023  use function sprintf;
024   
025  class DirectoryScanner implements ScannerInterface
026  {
027      /**
028       * @var bool
029       */
030      protected $isScanned = false;
031   
032      /**
033       * @var string[]|DirectoryScanner[]
034       */
035      protected $directories = [];
036   
037      /**
038       * @var FileScanner[]
039       */
040      protected $fileScanners = [];
041   
042      /**
043       * @var array
044       */
045      protected $classToFileScanner;
046   
047      /**
048       * @param null|string|array $directory
049       */
050      public function __construct($directory = null)
051      {
052          if ($directory) {
053              if (is_string($directory)) {
054                  $this->addDirectory($directory);
055              } elseif (is_array($directory)) {
056                  foreach ($directory as $d) {
057                      $this->addDirectory($d);
058                  }
059              }
060          }
061      }
062   
063      /**
064       * @param  DirectoryScanner|string $directory
065       * @return void
066       * @throws Exception\InvalidArgumentException
067       */
068      public function addDirectory($directory)
069      {
070          if ($directory instanceof DirectoryScanner) {
071              $this->directories[] = $directory;
072          } elseif (is_string($directory)) {
073              $realDir = realpath($directory);
074              if (! $realDir || ! is_dir($realDir)) {
075                  throw new Exception\InvalidArgumentException(sprintf(
076                      'Directory "%s" does not exist',
077                      $realDir
078                  ));
079              }
080              $this->directories[] = $realDir;
081          } else {
082              throw new Exception\InvalidArgumentException(
083                  'The argument provided was neither a DirectoryScanner or directory path'
084              );
085          }
086      }
087   
088      /**
089       * @param  DirectoryScanner $directoryScanner
090       * @return void
091       */
092      public function addDirectoryScanner(DirectoryScanner $directoryScanner)
093      {
094          $this->addDirectory($directoryScanner);
095      }
096   
097      /**
098       * @param  FileScanner $fileScanner
099       * @return void
100       */
101      public function addFileScanner(FileScanner $fileScanner)
102      {
103          $this->fileScanners[] = $fileScanner;
104      }
105   
106      /**
107       * @return void
108       */
109      protected function scan()
110      {
111          if ($this->isScanned) {
112              return;
113          }
114   
115          // iterate directories creating file scanners
116          foreach ($this->directories as $directory) {
117              if ($directory instanceof DirectoryScanner) {
118                  $directory->scan();
119                  if ($directory->fileScanners) {
120                      $this->fileScanners = array_merge($this->fileScanners, $directory->fileScanners);
121                  }
122              } else {
123                  $rdi = new RecursiveDirectoryIterator($directory);
124                  foreach (new RecursiveIteratorIterator($rdi) as $item) {
125                      if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') {
126                          $this->fileScanners[] = new FileScanner($item->getRealPath());
127                      }
128                  }
129              }
130          }
131   
132          $this->isScanned = true;
133      }
134   
135      /**
136       * @todo implement method
137       */
138      public function getNamespaces()
139      {
140          // @todo
141      }
142   
143      /**
144       * @param  bool $returnFileScanners
145       * @return array
146       */
147      public function getFiles($returnFileScanners = false)
148      {
149          $this->scan();
150   
151          $return = [];
152          foreach ($this->fileScanners as $fileScanner) {
153              $return[] = $returnFileScanners ? $fileScanner : $fileScanner->getFile();
154          }
155   
156          return $return;
157      }
158   
159      /**
160       * @return array
161       */
162      public function getClassNames()
163      {
164          $this->scan();
165   
166          if ($this->classToFileScanner === null) {
167              $this->createClassToFileScannerCache();
168          }
169   
170          return array_keys($this->classToFileScanner);
171      }
172   
173      /**
174       * @param  bool  $returnDerivedScannerClass
175       * @return array
176       */
177      public function getClasses($returnDerivedScannerClass = false)
178      {
179          $this->scan();
180   
181          if ($this->classToFileScanner === null) {
182              $this->createClassToFileScannerCache();
183          }
184   
185          $returnClasses = [];
186          foreach ($this->classToFileScanner as $className => $fsIndex) {
187              $classScanner = $this->fileScanners[$fsIndex]->getClass($className);
188              if ($returnDerivedScannerClass) {
189                  $classScanner = new DerivedClassScanner($classScanner, $this);
190              }
191              $returnClasses[] = $classScanner;
192          }
193   
194          return $returnClasses;
195      }
196   
197      /**
198       * @param  string $class
199       * @return bool
200       */
201      public function hasClass($class)
202      {
203          $this->scan();
204   
205          if ($this->classToFileScanner === null) {
206              $this->createClassToFileScannerCache();
207          }
208   
209          return isset($this->classToFileScanner[$class]);
210      }
211   
212      /**
213       * @param  string $class
214       * @param  bool $returnDerivedScannerClass
215       * @return ClassScanner|DerivedClassScanner
216       * @throws Exception\InvalidArgumentException
217       */
218      public function getClass($class, $returnDerivedScannerClass = false)
219      {
220          $this->scan();
221   
222          if ($this->classToFileScanner === null) {
223              $this->createClassToFileScannerCache();
224          }
225   
226          if (! isset($this->classToFileScanner[$class])) {
227              throw new Exception\InvalidArgumentException('Class not found.');
228          }
229   
230          /** @var FileScanner $fs */
231          $fs          = $this->fileScanners[$this->classToFileScanner[$class]];
232          $returnClass = $fs->getClass($class);
233   
234          if ($returnClass instanceof ClassScanner && $returnDerivedScannerClass) {
235              return new DerivedClassScanner($returnClass, $this);
236          }
237   
238          return $returnClass;
239      }
240   
241      /**
242       * Create class to file scanner cache
243       *
244       * @return void
245       */
246      protected function createClassToFileScannerCache()
247      {
248          if ($this->classToFileScanner !== null) {
249              return;
250          }
251   
252          $this->classToFileScanner = [];
253   
254          /** @var FileScanner $fileScanner */
255          foreach ($this->fileScanners as $fsIndex => $fileScanner) {
256              $fsClasses = $fileScanner->getClassNames();
257              foreach ($fsClasses as $fsClassName) {
258                  $this->classToFileScanner[$fsClassName] = $fsIndex;
259              }
260          }
261      }
262   
263      /**
264       * Export
265       *
266       * @todo implement method
267       */
268      public static function export()
269      {
270          // @todo
271      }
272   
273      /**
274       * __ToString
275       *
276       * @todo implement method
277       */
278      public function __toString()
279      {
280          // @todo
281      }
282  }
283