bad199be94be0bc91f42e8c6a3e2077868bdb893
[yaffs-website] / vendor / symfony / http-kernel / Config / EnvParametersResource.php
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\HttpKernel\Config;
13
14 use 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 class EnvParametersResource implements SelfCheckingResourceInterface, \Serializable
22 {
23     /**
24      * @var string
25      */
26     private $prefix;
27
28     /**
29      * @var string
30      */
31     private $variables;
32
33     /**
34      * Constructor.
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 array('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     public function serialize()
69     {
70         return serialize(array('prefix' => $this->prefix, 'variables' => $this->variables));
71     }
72
73     public function unserialize($serialized)
74     {
75         $unserialized = unserialize($serialized);
76
77         $this->prefix = $unserialized['prefix'];
78         $this->variables = $unserialized['variables'];
79     }
80
81     private function findVariables()
82     {
83         $variables = array();
84
85         foreach ($_SERVER as $key => $value) {
86             if (0 === strpos($key, $this->prefix)) {
87                 $variables[$key] = $value;
88             }
89         }
90
91         ksort($variables);
92
93         return $variables;
94     }
95 }