vendor/symfony/config/Resource/ReflectionClassResource.php line 101

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