ComposerResource.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /**
  12. * ComposerResource tracks the PHP version and Composer dependencies.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @final
  17. */
  18. class ComposerResource implements SelfCheckingResourceInterface
  19. {
  20. private $vendors;
  21. private static $runtimeVendors;
  22. public function __construct()
  23. {
  24. self::refresh();
  25. $this->vendors = self::$runtimeVendors;
  26. }
  27. public function getVendors(): array
  28. {
  29. return array_keys($this->vendors);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __toString(): string
  35. {
  36. return __CLASS__;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function isFresh(int $timestamp): bool
  42. {
  43. self::refresh();
  44. return array_values(self::$runtimeVendors) === array_values($this->vendors);
  45. }
  46. private static function refresh()
  47. {
  48. self::$runtimeVendors = [];
  49. foreach (get_declared_classes() as $class) {
  50. if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
  51. $r = new \ReflectionClass($class);
  52. $v = \dirname($r->getFileName(), 2);
  53. if (is_file($v.'/composer/installed.json')) {
  54. self::$runtimeVendors[$v] = @filemtime($v.'/composer/installed.json');
  55. }
  56. }
  57. }
  58. }
  59. }