@@ -42,7 +42,7 @@ class Repository
42
42
protected $ hidden = [];
43
43
44
44
/**
45
- * @var array<class-string<Contracts\Contextable> , Contracts\Contextable>
45
+ * @var array<int , Contracts\Contextable>
46
46
*/
47
47
protected $ contextables = [];
48
48
@@ -111,16 +111,11 @@ public function missingHidden($key)
111
111
* @return array<string, mixed>
112
112
*/
113
113
public function all ()
114
- {
115
- return $ this ->data ;
116
- }
117
-
118
- public function allWithContextables ()
119
114
{
120
115
$ data = $ this ->data ;
121
116
122
117
foreach ($ this ->contextables as $ contextable ) {
123
- $ data = array_merge ($ data , $ contextable ->contextData () );
118
+ $ data = array_merge ($ data , $ contextable ->context ( $ this ) ?? [] );
124
119
}
125
120
126
121
return $ data ;
@@ -235,16 +230,20 @@ public function exceptHidden($keys)
235
230
/**
236
231
* Add a context value.
237
232
*
238
- * @param string|array<string, mixed> $key
233
+ * @param string|array<string, mixed>|\Illuminate\Log\Context\Contracts\Contextable $key
239
234
* @param mixed $value
240
235
* @return $this
241
236
*/
242
237
public function add ($ key , $ value = null )
243
238
{
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
+ }
248
247
249
248
return $ this ;
250
249
}
@@ -398,7 +397,7 @@ public function contextable($contextable)
398
397
$ contextables = is_array ($ contextable ) ? $ contextable : [$ contextable ];
399
398
400
399
foreach ($ contextables as $ contextable ) {
401
- $ this ->contextables [$ contextable ::class ] = $ contextable ;
400
+ $ this ->contextables [] = $ contextable ;
402
401
}
403
402
404
403
return $ this ;
@@ -407,7 +406,7 @@ public function contextable($contextable)
407
406
/**
408
407
* Retrieve all registered contextables.
409
408
*
410
- * @return array<class-string<Contextable>, Contextable>
409
+ * @return array<int, \Illuminate\Log\Context\Contracts\ Contextable>
411
410
*/
412
411
public function getContextables ()
413
412
{
@@ -417,14 +416,18 @@ public function getContextables()
417
416
/**
418
417
* Remove a Contextable.
419
418
*
420
- * @param class-string<Contextable>|Contextable $contextable
419
+ * @param class-string<\Illuminate\Log\Context\Contracts\ Contextable>|\Illuminate\Log\Context\Contracts\ Contextable $contextable
421
420
* @return $this
422
421
*/
423
- public function forgetContextable ($ contextable )
422
+ public function forgetContextable ($ contextableToRemove )
424
423
{
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
+ }
426
429
427
- unset( $ this ->contextables [ $ class ] );
430
+ $ this ->contextables = array_values ( $ this -> contextables );
428
431
429
432
return $ this ;
430
433
}
@@ -697,7 +700,7 @@ public function flush()
697
700
public function dehydrate ()
698
701
{
699
702
$ instance = (new static ($ this ->events ))
700
- ->add ($ this ->all () )
703
+ ->add ($ this ->data )
701
704
->addHidden ($ this ->allHidden ())
702
705
->contextable ($ this ->getContextables ());
703
706
@@ -706,7 +709,7 @@ public function dehydrate()
706
709
$ serialize = fn ($ value ) => serialize ($ instance ->getSerializedPropertyValue ($ value , withRelations: false ));
707
710
708
711
return $ instance ->isEmpty () ? null : [
709
- 'data ' => array_map ($ serialize , $ instance ->all () ),
712
+ 'data ' => array_map ($ serialize , $ instance ->data ),
710
713
'hidden ' => array_map ($ serialize , $ instance ->allHidden ()),
711
714
'contextables ' => array_map ($ serialize , $ instance ->getContextables ()),
712
715
];
0 commit comments