菜谱项目

EnvParametersResource.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @return array An array with two keys: 'prefix' for the prefix used and 'variables' containing all the variables watched by this resource
  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. if (\PHP_VERSION_ID >= 70000) {
  63. $unserialized = unserialize($serialized, array('allowed_classes' => false));
  64. } else {
  65. $unserialized = unserialize($serialized);
  66. }
  67. $this->prefix = $unserialized['prefix'];
  68. $this->variables = $unserialized['variables'];
  69. }
  70. private function findVariables()
  71. {
  72. $variables = array();
  73. foreach ($_SERVER as $key => $value) {
  74. if (0 === strpos($key, $this->prefix)) {
  75. $variables[$key] = $value;
  76. }
  77. }
  78. ksort($variables);
  79. return $variables;
  80. }
  81. }