Skip to content

Commit feccc98

Browse files
[12.x] Introduce ComputesOnceableHashInterface (#56009)
* [12.x] Introduce `ComputesOnceableHashInterface` * formatting * Remove declare --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent e480f24 commit feccc98

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Support;
4+
5+
interface HasOnceHash
6+
{
7+
/**
8+
* Compute the hash that should be used to represent the object when given to a function using "once".
9+
*
10+
* @return string
11+
*/
12+
public function onceHash();
13+
}

src/Illuminate/Support/Onceable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Support;
44

55
use Closure;
6+
use Illuminate\Contracts\Support\HasOnceHash;
67
use Laravel\SerializableClosure\Support\ReflectionClosure;
78

89
class Onceable
@@ -61,7 +62,17 @@ protected static function hashFromTrace(array $trace, callable $callable)
6162
}
6263

6364
$uses = array_map(
64-
fn (mixed $argument) => is_object($argument) ? spl_object_hash($argument) : $argument,
65+
static function (mixed $argument) {
66+
if ($argument instanceof HasOnceHash) {
67+
return $argument->onceHash();
68+
}
69+
70+
if (is_object($argument)) {
71+
return spl_object_hash($argument);
72+
}
73+
74+
return $argument;
75+
},
6576
$callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [],
6677
);
6778

0 commit comments

Comments
 (0)