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