EnvParametersResource.php 2.3 KB

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