Nav apraksta

EnvParametersResource.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $unserialized = unserialize($serialized);
  65. $this->prefix = $unserialized['prefix'];
  66. $this->variables = $unserialized['variables'];
  67. }
  68. private function findVariables()
  69. {
  70. $variables = array();
  71. foreach ($_SERVER as $key => $value) {
  72. if (0 === strpos($key, $this->prefix)) {
  73. $variables[$key] = $value;
  74. }
  75. }
  76. ksort($variables);
  77. return $variables;
  78. }
  79. }