[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Scanner/ -> DirectoryScanner.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Code\Scanner;
  11  
  12  use RecursiveDirectoryIterator;
  13  use RecursiveIteratorIterator;
  14  use Zend\Code\Exception;
  15  
  16  class DirectoryScanner implements ScannerInterface
  17  {
  18      /**
  19       * @var bool
  20       */
  21      protected $isScanned = false;
  22  
  23      /**
  24       * @var string[]|DirectoryScanner[]
  25       */
  26      protected $directories = array();
  27  
  28      /**
  29       * @var FileScanner[]
  30       */
  31      protected $fileScanners = array();
  32  
  33      /**
  34       * @var array
  35       */
  36      protected $classToFileScanner = null;
  37  
  38      /**
  39       * @param null|string|array $directory
  40       */
  41      public function __construct($directory = null)
  42      {
  43          if ($directory) {
  44              if (is_string($directory)) {
  45                  $this->addDirectory($directory);
  46              } elseif (is_array($directory)) {
  47                  foreach ($directory as $d) {
  48                      $this->addDirectory($d);
  49                  }
  50              }
  51          }
  52      }
  53  
  54      /**
  55       * @param  DirectoryScanner|string $directory
  56       * @return void
  57       * @throws Exception\InvalidArgumentException
  58       */
  59      public function addDirectory($directory)
  60      {
  61          if ($directory instanceof DirectoryScanner) {
  62              $this->directories[] = $directory;
  63          } elseif (is_string($directory)) {
  64              $realDir = realpath($directory);
  65              if (!$realDir || !is_dir($realDir)) {
  66                  throw new Exception\InvalidArgumentException(sprintf(
  67                      'Directory "%s" does not exist',
  68                      $realDir
  69                  ));
  70              }
  71              $this->directories[] = $realDir;
  72          } else {
  73              throw new Exception\InvalidArgumentException(
  74                  'The argument provided was neither a DirectoryScanner or directory path'
  75              );
  76          }
  77      }
  78  
  79      /**
  80       * @param  DirectoryScanner $directoryScanner
  81       * @return void
  82       */
  83      public function addDirectoryScanner(DirectoryScanner $directoryScanner)
  84      {
  85          $this->addDirectory($directoryScanner);
  86      }
  87  
  88      /**
  89       * @param  FileScanner $fileScanner
  90       * @return void
  91       */
  92      public function addFileScanner(FileScanner $fileScanner)
  93      {
  94          $this->fileScanners[] = $fileScanner;
  95      }
  96  
  97      /**
  98       * @return void
  99       */
 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  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1