WebProcessor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php declare(strict_types=1);
  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 implements ProcessorInterface
  17. {
  18. /**
  19. * @var array<string, mixed>|\ArrayAccess<string, mixed>
  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<string, string>
  28. */
  29. protected $extraFields = [
  30. 'url' => 'REQUEST_URI',
  31. 'ip' => 'REMOTE_ADDR',
  32. 'http_method' => 'REQUEST_METHOD',
  33. 'server' => 'SERVER_NAME',
  34. 'referrer' => 'HTTP_REFERER',
  35. 'user_agent' => 'HTTP_USER_AGENT',
  36. ];
  37. /**
  38. * @param array<string, mixed>|\ArrayAccess<string, mixed>|null $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
  39. * @param array<string, string>|array<string>|null $extraFields Field names and the related key inside $serverData to be added (or just a list of field names to use the default configured $serverData mapping). If not provided it defaults to: [url, ip, http_method, server, referrer] + unique_id if present in server data
  40. */
  41. public function __construct($serverData = null, ?array $extraFields = null)
  42. {
  43. if (null === $serverData) {
  44. $this->serverData = &$_SERVER;
  45. } elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) {
  46. $this->serverData = $serverData;
  47. } else {
  48. throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
  49. }
  50. $defaultEnabled = ['url', 'ip', 'http_method', 'server', 'referrer'];
  51. if (isset($this->serverData['UNIQUE_ID'])) {
  52. $this->extraFields['unique_id'] = 'UNIQUE_ID';
  53. $defaultEnabled[] = 'unique_id';
  54. }
  55. if (null === $extraFields) {
  56. $extraFields = $defaultEnabled;
  57. }
  58. if (isset($extraFields[0])) {
  59. foreach (array_keys($this->extraFields) as $fieldName) {
  60. if (!in_array($fieldName, $extraFields)) {
  61. unset($this->extraFields[$fieldName]);
  62. }
  63. }
  64. } else {
  65. $this->extraFields = $extraFields;
  66. }
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function __invoke(array $record): array
  72. {
  73. // skip processing if for some reason request data
  74. // is not present (CLI or wonky SAPIs)
  75. if (!isset($this->serverData['REQUEST_URI'])) {
  76. return $record;
  77. }
  78. $record['extra'] = $this->appendExtraFields($record['extra']);
  79. return $record;
  80. }
  81. public function addExtraField(string $extraName, string $serverName): self
  82. {
  83. $this->extraFields[$extraName] = $serverName;
  84. return $this;
  85. }
  86. /**
  87. * @param mixed[] $extra
  88. * @return mixed[]
  89. */
  90. private function appendExtraFields(array $extra): array
  91. {
  92. foreach ($this->extraFields as $extraName => $serverName) {
  93. $extra[$extraName] = $this->serverData[$serverName] ?? null;
  94. }
  95. return $extra;
  96. }
  97. }