[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/config/Resource/ -> ReflectionClassResource.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Config\Resource;
  13  
  14  use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
  15  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16  
  17  /**
  18   * @author Nicolas Grekas <p@tchwork.com>
  19   */
  20  class ReflectionClassResource implements SelfCheckingResourceInterface, \Serializable
  21  {
  22      private $files = [];
  23      private $className;
  24      private $classReflector;
  25      private $excludedVendors = [];
  26      private $hash;
  27  
  28      public function __construct(\ReflectionClass $classReflector, $excludedVendors = [])
  29      {
  30          $this->className = $classReflector->name;
  31          $this->classReflector = $classReflector;
  32          $this->excludedVendors = $excludedVendors;
  33      }
  34  
  35      public function isFresh($timestamp)
  36      {
  37          if (null === $this->hash) {
  38              $this->hash = $this->computeHash();
  39              $this->loadFiles($this->classReflector);
  40          }
  41  
  42          foreach ($this->files as $file => $v) {
  43              if (false === $filemtime = @filemtime($file)) {
  44                  return false;
  45              }
  46  
  47              if ($filemtime > $timestamp) {
  48                  return $this->hash === $this->computeHash();
  49              }
  50          }
  51  
  52          return true;
  53      }
  54  
  55      public function __toString()
  56      {
  57          return 'reflection.'.$this->className;
  58      }
  59  
  60      /**
  61       * @internal
  62       */
  63      public function serialize()
  64      {
  65          if (null === $this->hash) {
  66              $this->hash = $this->computeHash();
  67              $this->loadFiles($this->classReflector);
  68          }
  69  
  70          return serialize([$this->files, $this->className, $this->hash]);
  71      }
  72  
  73      /**
  74       * @internal
  75       */
  76      public function unserialize($serialized)
  77      {
  78          list($this->files, $this->className, $this->hash) = unserialize($serialized);
  79      }
  80  
  81      private function loadFiles(\ReflectionClass $class)
  82      {
  83          foreach ($class->getInterfaces() as $v) {
  84              $this->loadFiles($v);
  85          }
  86          do {
  87              $file = $class->getFileName();
  88              if (false !== $file && file_exists($file)) {
  89                  foreach ($this->excludedVendors as $vendor) {
  90                      if (0 === strpos($file, $vendor) && false !== strpbrk(substr($file, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
  91                          $file = false;
  92                          break;
  93                      }
  94                  }
  95                  if ($file) {
  96                      $this->files[$file] = null;
  97                  }
  98              }
  99              foreach ($class->getTraits() as $v) {
 100                  $this->loadFiles($v);
 101              }
 102          } while ($class = $class->getParentClass());
 103      }
 104  
 105      private function computeHash()
 106      {
 107          if (null === $this->classReflector) {
 108              try {
 109                  $this->classReflector = new \ReflectionClass($this->className);
 110              } catch (\ReflectionException $e) {
 111                  // the class does not exist anymore
 112                  return false;
 113              }
 114          }
 115          $hash = hash_init('md5');
 116  
 117          foreach ($this->generateSignature($this->classReflector) as $info) {
 118              hash_update($hash, $info);
 119          }
 120  
 121          return hash_final($hash);
 122      }
 123  
 124      private function generateSignature(\ReflectionClass $class)
 125      {
 126          yield $class->getDocComment();
 127          yield (int) $class->isFinal();
 128          yield (int) $class->isAbstract();
 129  
 130          if ($class->isTrait()) {
 131              yield print_r(class_uses($class->name), true);
 132          } else {
 133              yield print_r(class_parents($class->name), true);
 134              yield print_r(class_implements($class->name), true);
 135              yield print_r($class->getConstants(), true);
 136          }
 137  
 138          if (!$class->isInterface()) {
 139              $defaults = $class->getDefaultProperties();
 140  
 141              foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) {
 142                  yield $p->getDocComment();
 143                  yield $p->isDefault() ? '<default>' : '';
 144                  yield $p->isPublic() ? 'public' : 'protected';
 145                  yield $p->isStatic() ? 'static' : '';
 146                  yield '$'.$p->name;
 147                  yield print_r(isset($defaults[$p->name]) && !\is_object($defaults[$p->name]) ? $defaults[$p->name] : null, true);
 148              }
 149          }
 150  
 151          if (\defined('HHVM_VERSION')) {
 152              foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
 153                  // workaround HHVM bug with variadics, see https://github.com/facebook/hhvm/issues/5762
 154                  yield preg_replace('/^  @@.*/m', '', new ReflectionMethodHhvmWrapper($m->class, $m->name));
 155              }
 156          } else {
 157              foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $m) {
 158                  $defaults = [];
 159                  $parametersWithUndefinedConstants = [];
 160                  foreach ($m->getParameters() as $p) {
 161                      if (!$p->isDefaultValueAvailable()) {
 162                          $defaults[$p->name] = null;
 163  
 164                          continue;
 165                      }
 166  
 167                      if (!$p->isDefaultValueConstant() || \defined($p->getDefaultValueConstantName())) {
 168                          $defaults[$p->name] = $p->getDefaultValue();
 169  
 170                          continue;
 171                      }
 172  
 173                      $defaults[$p->name] = $p->getDefaultValueConstantName();
 174                      $parametersWithUndefinedConstants[$p->name] = true;
 175                  }
 176  
 177                  if (!$parametersWithUndefinedConstants) {
 178                      yield preg_replace('/^  @@.*/m', '', $m);
 179                  } else {
 180                      $t = \PHP_VERSION_ID >= 70000 ? $m->getReturnType() : '';
 181                      $stack = [
 182                          $m->getDocComment(),
 183                          $m->getName(),
 184                          $m->isAbstract(),
 185                          $m->isFinal(),
 186                          $m->isStatic(),
 187                          $m->isPublic(),
 188                          $m->isPrivate(),
 189                          $m->isProtected(),
 190                          $m->returnsReference(),
 191                          $t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t,
 192                      ];
 193  
 194                      foreach ($m->getParameters() as $p) {
 195                          if (!isset($parametersWithUndefinedConstants[$p->name])) {
 196                              $stack[] = (string) $p;
 197                          } else {
 198                              $t = \PHP_VERSION_ID >= 70000 ? $p->getType() : '';
 199                              $stack[] = $p->isOptional();
 200                              $stack[] = $t instanceof \ReflectionNamedType ? ((string) $t->allowsNull()).$t->getName() : (string) $t;
 201                              $stack[] = $p->isPassedByReference();
 202                              $stack[] = \PHP_VERSION_ID >= 50600 ? $p->isVariadic() : '';
 203                              $stack[] = $p->getName();
 204                          }
 205                      }
 206  
 207                      yield implode(',', $stack);
 208                  }
 209  
 210                  yield print_r($defaults, true);
 211              }
 212          }
 213  
 214          if ($class->isAbstract() || $class->isInterface() || $class->isTrait()) {
 215              return;
 216          }
 217  
 218          if (interface_exists(EventSubscriberInterface::class, false) && $class->isSubclassOf(EventSubscriberInterface::class)) {
 219              yield EventSubscriberInterface::class;
 220              yield print_r(\call_user_func([$class->name, 'getSubscribedEvents']), true);
 221          }
 222  
 223          if (interface_exists(ServiceSubscriberInterface::class, false) && $class->isSubclassOf(ServiceSubscriberInterface::class)) {
 224              yield ServiceSubscriberInterface::class;
 225              yield print_r(\call_user_func([$class->name, 'getSubscribedServices']), true);
 226          }
 227      }
 228  }
 229  
 230  /**
 231   * @internal
 232   */
 233  class ReflectionMethodHhvmWrapper extends \ReflectionMethod
 234  {
 235      public function getParameters()
 236      {
 237          $params = [];
 238  
 239          foreach (parent::getParameters() as $i => $p) {
 240              $params[] = new ReflectionParameterHhvmWrapper([$this->class, $this->name], $i);
 241          }
 242  
 243          return $params;
 244      }
 245  }
 246  
 247  /**
 248   * @internal
 249   */
 250  class ReflectionParameterHhvmWrapper extends \ReflectionParameter
 251  {
 252      public function getDefaultValue()
 253      {
 254          return [$this->isVariadic(), $this->isDefaultValueAvailable() ? parent::getDefaultValue() : null];
 255      }
 256  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1