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\HttpKernel\Config;
13
14use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
15
16/**
17 * EnvParametersResource represents resources stored in prefixed environment variables.
18 *
19 * @author Chris Wilkinson <chriswilkinson84@gmail.com>
20 *
21 * @deprecated since version 3.4, to be removed in 4.0
22 */
23class EnvParametersResource implements SelfCheckingResourceInterface, \Serializable
24{
25    /**
26     * @var string
27     */
28    private $prefix;
29
30    /**
31     * @var string
32     */
33    private $variables;
34
35    /**
36     * @param string $prefix
37     */
38    public function __construct($prefix)
39    {
40        $this->prefix = $prefix;
41        $this->variables = $this->findVariables();
42    }
43
44    /**
45     * {@inheritdoc}
46     */
47    public function __toString()
48    {
49        return serialize($this->getResource());
50    }
51
52    /**
53     * @return array An array with two keys: 'prefix' for the prefix used and 'variables' containing all the variables watched by this resource
54     */
55    public function getResource()
56    {
57        return ['prefix' => $this->prefix, 'variables' => $this->variables];
58    }
59
60    /**
61     * {@inheritdoc}
62     */
63    public function isFresh($timestamp)
64    {
65        return $this->findVariables() === $this->variables;
66    }
67
68    /**
69     * @internal
70     */
71    public function serialize()
72    {
73        return serialize(['prefix' => $this->prefix, 'variables' => $this->variables]);
74    }
75
76    /**
77     * @internal
78     */
79    public function unserialize($serialized)
80    {
81        if (\PHP_VERSION_ID >= 70000) {
82            $unserialized = unserialize($serialized, ['allowed_classes' => false]);
83        } else {
84            $unserialized = unserialize($serialized);
85        }
86
87        $this->prefix = $unserialized['prefix'];
88        $this->variables = $unserialized['variables'];
89    }
90
91    private function findVariables()
92    {
93        $variables = [];
94
95        foreach ($_SERVER as $key => $value) {
96            if (0 === strpos($key, $this->prefix)) {
97                $variables[$key] = $value;
98            }
99        }
100
101        ksort($variables);
102
103        return $variables;
104    }
105}
106