EnvParametersResource.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * @param string $prefix
  29. */
  30. public function __construct($prefix)
  31. {
  32. $this->prefix = $prefix;
  33. $this->variables = $this->findVariables();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function __toString()
  39. {
  40. return serialize($this->getResource());
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getResource()
  46. {
  47. return array('prefix' => $this->prefix, 'variables' => $this->variables);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function isFresh($timestamp)
  53. {
  54. return $this->findVariables() === $this->variables;
  55. }
  56. public function serialize()
  57. {
  58. return serialize(array('prefix' => $this->prefix, 'variables' => $this->variables));
  59. }
  60. public function unserialize($serialized)
  61. {
  62. $unserialized = unserialize($serialized);
  63. $this->prefix = $unserialized['prefix'];
  64. $this->variables = $unserialized['variables'];
  65. }
  66. private function findVariables()
  67. {
  68. $variables = array();
  69. foreach ($_SERVER as $key => $value) {
  70. if (0 === strpos($key, $this->prefix)) {
  71. $variables[$key] = $value;
  72. }
  73. }
  74. ksort($variables);
  75. return $variables;
  76. }
  77. }