-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
1 parent
e8c1210
commit 2dac84d
Showing
5 changed files
with
202 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Support\Once; | ||
|
||
class Backtrace | ||
{ | ||
protected array $trace; | ||
|
||
protected array $zeroStack; | ||
|
||
public function __construct(array $trace) | ||
{ | ||
$this->trace = $trace[1]; | ||
|
||
$this->zeroStack = $trace[0]; | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
return $this->trace['args']; | ||
} | ||
|
||
public function getFunctionName(): string | ||
{ | ||
return $this->trace['function']; | ||
} | ||
|
||
public function getObjectName(): ?string | ||
{ | ||
return $this->trace['class'] ?? null; | ||
} | ||
|
||
public function getObject(): mixed | ||
{ | ||
if ($this->globalFunction()) { | ||
return $this->zeroStack['file']; | ||
} | ||
|
||
return $this->staticCall() ? $this->trace['class'] : $this->trace['object']; | ||
} | ||
|
||
public function getHash(): string | ||
{ | ||
$normalizedArguments = array_map(function ($argument) { | ||
return is_object($argument) ? spl_object_hash($argument) : $argument; | ||
}, $this->getArguments()); | ||
|
||
$prefix = $this->getObjectName() . $this->getFunctionName(); | ||
if (str_contains($prefix, '{closure')) { | ||
$prefix = $this->zeroStack['line']; | ||
} | ||
|
||
return md5($prefix . serialize($normalizedArguments)); | ||
} | ||
|
||
protected function staticCall(): bool | ||
{ | ||
return $this->trace['type'] === '::'; | ||
} | ||
|
||
protected function globalFunction(): bool | ||
{ | ||
return ! isset($this->trace['type']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Support\Once; | ||
|
||
use Countable; | ||
use WeakMap; | ||
|
||
class Cache implements Countable | ||
{ | ||
public WeakMap $values; | ||
|
||
/** | ||
* The cache instance. | ||
* @var static|null | ||
*/ | ||
protected static $cache; | ||
|
||
protected bool $enabled = true; | ||
|
||
protected function __construct() | ||
{ | ||
$this->values = new WeakMap(); | ||
} | ||
|
||
public static function getInstance(): static | ||
{ | ||
return static::$cache ??= new static(); | ||
} | ||
|
||
public function has(object $object, string $backtraceHash): bool | ||
{ | ||
if (! isset($this->values[$object])) { | ||
return false; | ||
} | ||
|
||
return array_key_exists($backtraceHash, $this->values[$object]); | ||
} | ||
|
||
public function get($object, string $backtraceHash): mixed | ||
{ | ||
return $this->values[$object][$backtraceHash]; | ||
} | ||
|
||
public function set(object $object, string $backtraceHash, mixed $value): void | ||
{ | ||
$cached = $this->values[$object] ?? []; | ||
|
||
$cached[$backtraceHash] = $value; | ||
|
||
$this->values[$object] = $cached; | ||
} | ||
|
||
public function forget(object $object): void | ||
{ | ||
unset($this->values[$object]); | ||
} | ||
|
||
public function flush(): self | ||
{ | ||
$this->values = new WeakMap(); | ||
|
||
return $this; | ||
} | ||
|
||
public function enable(): self | ||
{ | ||
$this->enabled = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function disable(): self | ||
{ | ||
$this->enabled = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->values); | ||
} | ||
} |
Oops, something went wrong.