Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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