EnvParametersResource.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class EnvParametersResource implements SelfCheckingResourceInterface, \Serializable
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $prefix;
  23. /**
  24. * @var string
  25. */
  26. private $variables;
  27. /**
  28. * Constructor.
  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 array('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. public function serialize()
  59. {
  60. return serialize(array('prefix' => $this->prefix, 'variables' => $this->variables));
  61. }
  62. public function unserialize($serialized)
  63. {
  64. if (\PHP_VERSION_ID >= 70000) {
  65. $unserialized = unserialize($serialized, array('allowed_classes' => false));
  66. } else {
  67. $unserialized = unserialize($serialized);
  68. }
  69. $this->prefix = $unserialized['prefix'];
  70. $this->variables = $unserialized['variables'];
  71. }
  72. private function findVariables()
  73. {
  74. $variables = array();
  75. foreach ($_SERVER as $key => $value) {
  76. if (0 === strpos($key, $this->prefix)) {
  77. $variables[$key] = $value;
  78. }
  79. }
  80. ksort($variables);
  81. return $variables;
  82. }
  83. }