Нет описания

WebProcessor.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Processor;
  11. /**
  12. * Injects url/method and remote IP of the current web request in all records
  13. *
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class WebProcessor
  17. {
  18. /**
  19. * @var array|\ArrayAccess
  20. */
  21. protected $serverData;
  22. /**
  23. * Default fields
  24. *
  25. * Array is structured as [key in record.extra => key in $serverData]
  26. *
  27. * @var array
  28. */
  29. protected $extraFields = array(
  30. 'url' => 'REQUEST_URI',
  31. 'ip' => 'REMOTE_ADDR',
  32. 'http_method' => 'REQUEST_METHOD',
  33. 'server' => 'SERVER_NAME',
  34. 'referrer' => 'HTTP_REFERER',
  35. );
  36. /**
  37. * @param array|\ArrayAccess $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
  38. * @param array|null $extraFields Field names and the related key inside $serverData to be added. If not provided it defaults to: url, ip, http_method, server, referrer
  39. */
  40. public function __construct($serverData = null, array $extraFields = null)
  41. {
  42. if (null === $serverData) {
  43. $this->serverData = &$_SERVER;
  44. } elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) {
  45. $this->serverData = $serverData;
  46. } else {
  47. throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
  48. }
  49. if (null !== $extraFields) {
  50. if (isset($extraFields[0])) {
  51. foreach (array_keys($this->extraFields) as $fieldName) {
  52. if (!in_array($fieldName, $extraFields)) {
  53. unset($this->extraFields[$fieldName]);
  54. }
  55. }
  56. } else {
  57. $this->extraFields = $extraFields;
  58. }
  59. }
  60. }
  61. /**
  62. * @param array $record
  63. * @return array
  64. */
  65. public function __invoke(array $record)
  66. {
  67. // skip processing if for some reason request data
  68. // is not present (CLI or wonky SAPIs)
  69. if (!isset($this->serverData['REQUEST_URI'])) {
  70. return $record;
  71. }
  72. $record['extra'] = $this->appendExtraFields($record['extra']);
  73. return $record;
  74. }
  75. /**
  76. * @param string $extraName
  77. * @param string $serverName
  78. * @return $this
  79. */
  80. public function addExtraField($extraName, $serverName)
  81. {
  82. $this->extraFields[$extraName] = $serverName;
  83. return $this;
  84. }
  85. /**
  86. * @param array $extra
  87. * @return array
  88. */
  89. private function appendExtraFields(array $extra)
  90. {
  91. foreach ($this->extraFields as $extraName => $serverName) {
  92. $extra[$extraName] = isset($this->serverData[$serverName]) ? $this->serverData[$serverName] : null;
  93. }
  94. if (isset($this->serverData['UNIQUE_ID'])) {
  95. $extra['unique_id'] = $this->serverData['UNIQUE_ID'];
  96. }
  97. return $extra;
  98. }
  99. }