[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Reflection/ -> FileReflection.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-2016 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\Reflection;
  11  
  12  use Zend\Code\Scanner\CachingFileScanner;
  13  
  14  use function basename;
  15  use function count;
  16  use function current;
  17  use function file_get_contents;
  18  use function get_included_files;
  19  use function in_array;
  20  use function realpath;
  21  use function reset;
  22  use function sprintf;
  23  use function stream_resolve_include_path;
  24  use function substr_count;
  25  
  26  class FileReflection implements ReflectionInterface
  27  {
  28      /**
  29       * @var string
  30       */
  31      protected $filePath;
  32  
  33      /**
  34       * @var string
  35       */
  36      protected $docComment;
  37  
  38      /**
  39       * @var int
  40       */
  41      protected $startLine = 1;
  42  
  43      /**
  44       * @var int
  45       */
  46      protected $endLine;
  47  
  48      /**
  49       * @var string[]
  50       */
  51      protected $namespaces = [];
  52  
  53      /**
  54       * @var string[]
  55       */
  56      protected $uses = [];
  57  
  58      /**
  59       * @var string[]
  60       */
  61      protected $requiredFiles = [];
  62  
  63      /**
  64       * @var ClassReflection[]
  65       */
  66      protected $classes = [];
  67  
  68      /**
  69       * @var FunctionReflection[]
  70       */
  71      protected $functions = [];
  72  
  73      /**
  74       * @var string
  75       */
  76      protected $contents;
  77  
  78      /**
  79       * @param  string $filename
  80       * @param  bool $includeIfNotAlreadyIncluded
  81       * @throws Exception\InvalidArgumentException If file does not exists
  82       * @throws Exception\RuntimeException If file exists but is not included or required
  83       */
  84      public function __construct($filename, $includeIfNotAlreadyIncluded = false)
  85      {
  86          if (($fileRealPath = realpath($filename)) === false) {
  87              $fileRealPath = stream_resolve_include_path($filename);
  88          }
  89  
  90          if (! $fileRealPath) {
  91              throw new Exception\InvalidArgumentException(sprintf(
  92                  'No file for %s was found.',
  93                  $filename
  94              ));
  95          }
  96  
  97          if (! in_array($fileRealPath, get_included_files())) {
  98              if (! $includeIfNotAlreadyIncluded) {
  99                  throw new Exception\RuntimeException(sprintf(
 100                      'File %s must be required before it can be reflected',
 101                      $filename
 102                  ));
 103              }
 104  
 105              include $fileRealPath;
 106          }
 107  
 108          $this->filePath = $fileRealPath;
 109          $this->reflect();
 110      }
 111  
 112      /**
 113       * Required by the Reflector interface.
 114       *
 115       * @todo   What should this do?
 116       * @return void
 117       */
 118      public static function export()
 119      {
 120      }
 121  
 122      /**
 123       * Return the file name of the reflected file
 124       *
 125       * @return string
 126       */
 127      public function getFileName()
 128      {
 129          return basename($this->filePath);
 130      }
 131  
 132      /**
 133       * Get the start line - Always 1, staying consistent with the Reflection API
 134       *
 135       * @return int
 136       */
 137      public function getStartLine()
 138      {
 139          return $this->startLine;
 140      }
 141  
 142      /**
 143       * Get the end line / number of lines
 144       *
 145       * @return int
 146       */
 147      public function getEndLine()
 148      {
 149          return $this->endLine;
 150      }
 151  
 152      /**
 153       * @return string
 154       */
 155      public function getDocComment()
 156      {
 157          return $this->docComment;
 158      }
 159  
 160      /**
 161       * @return DocBlockReflection|false
 162       */
 163      public function getDocBlock()
 164      {
 165          if (! ($docComment = $this->getDocComment())) {
 166              return false;
 167          }
 168  
 169          $instance = new DocBlockReflection($docComment);
 170  
 171          return $instance;
 172      }
 173  
 174      /**
 175       * @return string[]
 176       */
 177      public function getNamespaces()
 178      {
 179          return $this->namespaces;
 180      }
 181  
 182      /**
 183       * @return null|string
 184       */
 185      public function getNamespace()
 186      {
 187          if (count($this->namespaces) == 0) {
 188              return;
 189          }
 190  
 191          return $this->namespaces[0];
 192      }
 193  
 194      /**
 195       * @return array
 196       */
 197      public function getUses()
 198      {
 199          return $this->uses;
 200      }
 201  
 202      /**
 203       * Return the reflection classes of the classes found inside this file
 204       *
 205       * @return ClassReflection[]
 206       */
 207      public function getClasses()
 208      {
 209          $classes = [];
 210          foreach ($this->classes as $class) {
 211              $classes[] = new ClassReflection($class);
 212          }
 213  
 214          return $classes;
 215      }
 216  
 217      /**
 218       * Return the reflection functions of the functions found inside this file
 219       *
 220       * @return FunctionReflection[]
 221       */
 222      public function getFunctions()
 223      {
 224          $functions = [];
 225          foreach ($this->functions as $function) {
 226              $functions[] = new FunctionReflection($function);
 227          }
 228  
 229          return $functions;
 230      }
 231  
 232      /**
 233       * Retrieve the reflection class of a given class found in this file
 234       *
 235       * @param  null|string $name
 236       * @return ClassReflection
 237       * @throws Exception\InvalidArgumentException for invalid class name or invalid reflection class
 238       */
 239      public function getClass($name = null)
 240      {
 241          if (null === $name) {
 242              reset($this->classes);
 243              $selected = current($this->classes);
 244  
 245              return new ClassReflection($selected);
 246          }
 247  
 248          if (in_array($name, $this->classes)) {
 249              return new ClassReflection($name);
 250          }
 251  
 252          throw new Exception\InvalidArgumentException(sprintf(
 253              'Class by name %s not found.',
 254              $name
 255          ));
 256      }
 257  
 258      /**
 259       * Return the full contents of file
 260       *
 261       * @return string
 262       */
 263      public function getContents()
 264      {
 265          return file_get_contents($this->filePath);
 266      }
 267  
 268      public function toString()
 269      {
 270          return ''; // @todo
 271      }
 272  
 273      /**
 274       * Serialize to string
 275       *
 276       * Required by the Reflector interface
 277       *
 278       * @todo   What should this serialization look like?
 279       * @return string
 280       */
 281      public function __toString()
 282      {
 283          return '';
 284      }
 285  
 286      /**
 287       * This method does the work of "reflecting" the file
 288       *
 289       * Uses Zend\Code\Scanner\FileScanner to gather file information
 290       *
 291       * @return void
 292       */
 293      protected function reflect()
 294      {
 295          $scanner             = new CachingFileScanner($this->filePath);
 296          $this->docComment    = $scanner->getDocComment();
 297          $this->requiredFiles = $scanner->getIncludes();
 298          $this->classes       = $scanner->getClassNames();
 299          $this->namespaces    = $scanner->getNamespaces();
 300          $this->uses          = $scanner->getUses();
 301      }
 302  
 303      /**
 304       * Validate / check a file level DocBlock
 305       *
 306       * @param  array $tokens Array of tokenizer tokens
 307       * @return void
 308       */
 309      protected function checkFileDocBlock($tokens)
 310      {
 311          foreach ($tokens as $token) {
 312              $type    = $token[0];
 313              $value   = $token[1];
 314              $lineNum = $token[2];
 315              if (($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) {
 316                  continue;
 317              } elseif ($type == T_DOC_COMMENT) {
 318                  $this->docComment = $value;
 319                  $this->startLine  = $lineNum + substr_count($value, "\n") + 1;
 320  
 321                  return;
 322              } else {
 323                  // Only whitespace is allowed before file DocBlocks
 324                  return;
 325              }
 326          }
 327      }
 328  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1