8000 some PR feedback · laravel/framework@017b5d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 017b5d7

Browse files
committed
some PR feedback
1 parent 41be7dc commit 017b5d7

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

src/Illuminate/Log/Context/ContextLogProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __invoke(LogRecord $record): LogRecord
2525

2626
return $record->with(extra: [
2727
...$record->extra,
28-
...$app->get(ContextRepository::class)->allWithContextables(),
28+
...$app->get(ContextRepository::class)->all(),
2929
]);
3030
}
3131
}

src/Illuminate/Log/Context/Contracts/Contextable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface Contextable
77
/**
88
* The data to append to your log output.
99
*
10-
* @return array<string, mixed>
10+
* @param \Illuminate\Log\Context\Repository $repository
11+
* @return array<string, mixed>|null
1112
*/
12-
public function contextData();
13+
public function context($repository);
1314
}

src/Illuminate/Log/Context/Repository.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Repository
4242
protected $hidden = [];
4343

4444
/**
45-
* @var array<class-string<Contracts\Contextable>, Contracts\Contextable>
45+
* @var array<int, Contracts\Contextable>
4646
*/
4747
protected $contextables = [];
4848

@@ -111,16 +111,11 @@ public function missingHidden($key)
111111
* @return array<string, mixed>
112112
*/
113113
public function all()
114-
{
115-
return $this->data;
116-
}
117-
118-
public function allWithContextables()
119114
{
120115
$data = $this->data;
121116

122117
foreach($this->contextables as $contextable) {
123-
$data = array_merge($data, $contextable->contextData());
118+
$data = array_merge($data, $contextable->context($this) ?? []);
124119
}
125120

126121
return $data;
@@ -235,16 +230,20 @@ public function exceptHidden($keys)
235230
/**
236231
* Add a context value.
237232
*
238-
* @param string|array<string, mixed> $key
233+
* @param string|array<string, mixed>|\Illuminate\Log\Context\Contracts\Contextable $key
239234
* @param mixed $value
240235
* @return $this
241236
*/
242237
public function add($key, $value = null)
243238
{
244-
$this->data = array_merge(
245-
$this->data,
246-
is_array($key) ? $key : [$key => $value]
247-
);
239+
if ($key instanceof Contextable) {
240+
$this->contextables[] = $key;
241+
} else {
242+
$this->data = array_merge(
243+
$this->data,
244+
is_array($key) ? $key : [$key => $value]
245+
);
246+
}
248247

249248
return $this;
250249
}
@@ -398,7 +397,7 @@ public function contextable($contextable)
398397
$contextables = is_array($contextable) ? $contextable : [$contextable];
399398

400399
foreach($contextables as $contextable) {
401-
$this->contextables[$contextable::class] = $contextable;
400+
$this->contextables[] = $contextable;
402401
}
403402

404403
return $this;
@@ -407,7 +406,7 @@ public function contextable($contextable)
407406
/**
408407
* Retrieve all registered contextables.
409408
*
410-
* @return array<class-string<Contextable>, Contextable>
409+
* @return array<int, \Illuminate\Log\Context\Contracts\Contextable>
411410
*/
412411
public function getContextables()
413412
{
@@ -417,14 +416,18 @@ public function getContextables()
417416
/**
418417
* Remove a Contextable.
419418
*
420-
* @param class-string<Contextable>|Contextable $contextable
419+
* @param class-string<\Illuminate\Log\Context\Contracts\Contextable>|\Illuminate\Log\Context\Contracts\Contextable $contextable
421420
* @return $this
422421
*/
423-
public function forgetContextable($contextable)
422+
public function forgetContextable($contextableToRemove)
424423
{
425-
$class = is_string($contextable) ? $contextable : $contextable::class;
424+
foreach($this->contextables as $i => $contextable) {
425+
if ((is_string($contextableToRemove) && is_a($contextable, $contextableToRemove, true)) || ($contextableToRemove === $contextable)) {
426+
unset($this->contextables[$i]);
427+
}
428+
}
426429

427-
unset($this->contextables[$class]);
430+
$this->contextables = array_values($this->contextables);
428431

429432
return $this;
430433
}
@@ -697,7 +700,7 @@ public function flush()
697700
public function dehydrate()
698701
{
699702
$instance = (new static($this->events))
700-
->add($this->all())
703+
->add($this->data)
701704
->addHidden($this->allHidden())
702705
->contextable($this->getContextables());
703706

@@ -706,7 +709,7 @@ public function dehydrate()
706709
$serialize = fn ($value) => serialize($instance->getSerializedPropertyValue($value, withRelations: false));
707710

708711
return $instance->isEmpty() ? null : [
709-
'data' => array_map($serialize, $instance->all()),
712+
'data' => array_map($serialize, $instance->data),
710713
'hidden' => array_map($serialize, $instance->allHidden()),
711714
'contextables' => array_map($serialize, $instance->getContextables()),
712715
];

tests/Log/ContextTest.php

-28 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0