WebProcessor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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|\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 = [
  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|null $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 (isset($this->serverData['UNIQUE_ID'])) {
  50. $this->extraFields['unique_id'] = 'UNIQUE_ID';
  51. }
  52. if (null !== $extraFields) {
  53. if (isset($extraFields[0])) {
  54. foreach (array_keys($this->extraFields) as $fieldName) {
  55. if (!in_array($fieldName, $extraFields)) {
  56. unset($this->extraFields[$fieldName]);
  57. }
  58. }
  59. } else {
  60. $this->extraFields = $extraFields;
  61. }
  62. }
  63. }
  64. public function __invoke(array $record): array
  65. {
  66. // skip processing if for some reason request data
  67. // is not present (CLI or wonky SAPIs)
  68. if (!isset($this->serverData['REQUEST_URI'])) {
  69. return $record;
  70. }
  71. $record['extra'] = $this->appendExtraFields($record['extra']);
  72. return $record;
  73. }
  74. public function addExtraField(string $extraName, string $serverName): self
  75. {
  76. $this->extraFields[$extraName] = $serverName;
  77. return $this;
  78. }
  79. private function appendExtraFields(array $extra): array
  80. {
  81. foreach ($this->extraFields as $extraName => $serverName) {
  82. $extra[$extraName] = $this->serverData[$serverName] ?? null;
  83. }
  84. return $extra;
  85. }
  86. }