summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Util
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2023-10-20 17:12:29 +0300
committerAndrew Dolgov <[email protected]>2023-10-20 21:13:39 +0300
commitcdd7ad020e165fe680703b6d3319b908b682fb7a (patch)
treeb51eb09b7b4587e8fbc5624ac8d88d28cfcd0b04 /vendor/open-telemetry/sdk/Common/Util
parent45a9ff0c88cbd33892ff16ab837e9059937d656e (diff)
jaeger-client -> opentelemetry
Diffstat (limited to 'vendor/open-telemetry/sdk/Common/Util')
-rw-r--r--vendor/open-telemetry/sdk/Common/Util/ClassConstantAccessor.php35
-rw-r--r--vendor/open-telemetry/sdk/Common/Util/ShutdownHandler.php82
-rw-r--r--vendor/open-telemetry/sdk/Common/Util/WeakMap.php175
-rw-r--r--vendor/open-telemetry/sdk/Common/Util/functions.php52
4 files changed, 344 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Common/Util/ClassConstantAccessor.php b/vendor/open-telemetry/sdk/Common/Util/ClassConstantAccessor.php
new file mode 100644
index 000000000..237e70ba5
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Util/ClassConstantAccessor.php
@@ -0,0 +1,35 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Util;
+
+use LogicException;
+
+class ClassConstantAccessor
+{
+ public static function requireValue(string $className, string $constantName)
+ {
+ $constant = self::getFullName($className, $constantName);
+
+ if (!defined($constant)) {
+ throw new LogicException(
+ sprintf('The class "%s" does not have a constant "%s"', $className, $constantName)
+ );
+ }
+
+ return constant($constant);
+ }
+
+ public static function getValue(string $className, string $constantName)
+ {
+ $constant = self::getFullName($className, $constantName);
+
+ return defined($constant) ? constant($constant) : null;
+ }
+
+ private static function getFullName(string $className, string $constantName): string
+ {
+ return sprintf('%s::%s', $className, $constantName);
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Util/ShutdownHandler.php b/vendor/open-telemetry/sdk/Common/Util/ShutdownHandler.php
new file mode 100644
index 000000000..2de6d47df
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Util/ShutdownHandler.php
@@ -0,0 +1,82 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Util;
+
+use function array_key_last;
+use ArrayAccess;
+use Closure;
+use function register_shutdown_function;
+
+final class ShutdownHandler
+{
+ /** @var array<int, Closure>|null */
+ private static ?array $handlers = null;
+ /** @var ArrayAccess<object, self>|null */
+ private static ?ArrayAccess $weakMap = null;
+
+ private array $ids = [];
+
+ private function __construct()
+ {
+ }
+
+ public function __destruct()
+ {
+ if (!self::$handlers) {
+ return;
+ }
+ foreach ($this->ids as $id) {
+ unset(self::$handlers[$id]);
+ }
+ }
+
+ /**
+ * Registers a function that will be executed on shutdown.
+ *
+ * If the given function is bound to an object, then the function will only
+ * be executed if the bound object is still referenced on shutdown handler
+ * invocation.
+ *
+ * ```php
+ * ShutdownHandler::register([$tracerProvider, 'shutdown']);
+ * ```
+ *
+ * @param callable $shutdownFunction function to register
+ *
+ * @see register_shutdown_function
+ */
+ public static function register(callable $shutdownFunction): void
+ {
+ self::registerShutdownFunction();
+ self::$handlers[] = weaken(closure($shutdownFunction), $target);
+
+ if (!$object = $target) {
+ return;
+ }
+
+ self::$weakMap ??= WeakMap::create();
+ $handler = self::$weakMap[$object] ??= new self();
+ $handler->ids[] = array_key_last(self::$handlers);
+ }
+
+ private static function registerShutdownFunction(): void
+ {
+ if (self::$handlers === null) {
+ register_shutdown_function(static function (): void {
+ $handlers = self::$handlers;
+ self::$handlers = null;
+ self::$weakMap = null;
+
+ // Push shutdown to end of queue
+ // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
+ register_shutdown_function(static function (array $handlers): void {
+ foreach ($handlers as $handler) {
+ $handler();
+ }
+ }, $handlers);
+ });
+ }
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Util/WeakMap.php b/vendor/open-telemetry/sdk/Common/Util/WeakMap.php
new file mode 100644
index 000000000..3b62d6d64
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Util/WeakMap.php
@@ -0,0 +1,175 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Util;
+
+use ArrayAccess;
+use function assert;
+use function class_exists;
+use function count;
+use Countable;
+use Error;
+use function get_class;
+use function is_object;
+use IteratorAggregate;
+use const PHP_VERSION_ID;
+use function spl_object_id;
+use function sprintf;
+use Traversable;
+use TypeError;
+use WeakReference;
+
+/**
+ * @internal
+ */
+final class WeakMap implements ArrayAccess, Countable, IteratorAggregate
+{
+ private const KEY = '__otel_weak_map';
+
+ /**
+ * @var array<int, WeakReference>
+ */
+ private array $objects = [];
+
+ private function __construct()
+ {
+ }
+
+ /**
+ * @return ArrayAccess&Countable&IteratorAggregate
+ */
+ public static function create(): ArrayAccess
+ {
+ if (PHP_VERSION_ID >= 80000) {
+ /** @phan-suppress-next-line PhanUndeclaredClassReference */
+ assert(class_exists(\WeakMap::class, false));
+ /** @phan-suppress-next-line PhanUndeclaredClassMethod */
+ $map = new \WeakMap();
+ assert($map instanceof ArrayAccess);
+ assert($map instanceof Countable);
+ assert($map instanceof IteratorAggregate);
+
+ return $map;
+ }
+
+ return new self();
+ }
+
+ public function offsetExists($offset): bool
+ {
+ if (!is_object($offset)) {
+ throw new TypeError('WeakMap key must be an object');
+ }
+
+ return isset($offset->{self::KEY}[spl_object_id($this)]);
+ }
+
+ /**
+ * @phan-suppress PhanUndeclaredClassAttribute
+ */
+ #[\ReturnTypeWillChange]
+ public function offsetGet($offset)
+ {
+ if (!is_object($offset)) {
+ throw new TypeError('WeakMap key must be an object');
+ }
+ if (!$this->contains($offset)) {
+ throw new Error(sprintf('Object %s#%d not contained in WeakMap', get_class($offset), spl_object_id($offset)));
+ }
+
+ return $offset->{self::KEY}[spl_object_id($this)];
+ }
+
+ public function offsetSet($offset, $value): void
+ {
+ if ($offset === null) {
+ throw new Error('Cannot append to WeakMap');
+ }
+ if (!is_object($offset)) {
+ throw new TypeError('WeakMap key must be an object');
+ }
+ if (!$this->contains($offset)) {
+ $this->expunge();
+ }
+
+ $offset->{self::KEY}[spl_object_id($this)] = $value;
+ $this->objects[spl_object_id($offset)] = WeakReference::create($offset);
+ }
+
+ public function offsetUnset($offset): void
+ {
+ if (!is_object($offset)) {
+ throw new TypeError('WeakMap key must be an object');
+ }
+ if (!$this->contains($offset)) {
+ return;
+ }
+
+ unset(
+ $offset->{self::KEY}[spl_object_id($this)],
+ $this->objects[spl_object_id($offset)],
+ );
+ if (!$offset->{self::KEY}) {
+ unset($offset->{self::KEY});
+ }
+ }
+
+ public function count(): int
+ {
+ $this->expunge();
+
+ return count($this->objects);
+ }
+
+ public function getIterator(): Traversable
+ {
+ $this->expunge();
+
+ foreach ($this->objects as $reference) {
+ if (($object = $reference->get()) && $this->contains($object)) {
+ yield $object => $this[$object];
+ }
+ }
+ }
+
+ public function __debugInfo(): array
+ {
+ $debugInfo = [];
+ foreach ($this as $key => $value) {
+ $debugInfo[] = ['key' => $key, 'value' => $value];
+ }
+
+ return $debugInfo;
+ }
+
+ public function __destruct()
+ {
+ foreach ($this->objects as $reference) {
+ if ($object = $reference->get()) {
+ unset($this[$object]);
+ }
+ }
+ }
+
+ private function contains(object $offset): bool
+ {
+ $reference = $this->objects[spl_object_id($offset)] ?? null;
+ if ($reference && $reference->get() === $offset) {
+ return true;
+ }
+
+ unset($this->objects[spl_object_id($offset)]);
+
+ return false;
+ }
+
+ private function expunge(): void
+ {
+ foreach ($this->objects as $id => $reference) {
+ if (!$reference->get()) {
+ unset($this->objects[$id]);
+ }
+ }
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Util/functions.php b/vendor/open-telemetry/sdk/Common/Util/functions.php
new file mode 100644
index 000000000..f4fb13b80
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Util/functions.php
@@ -0,0 +1,52 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Util;
+
+use Closure;
+use function get_class;
+use ReflectionFunction;
+use stdClass;
+use WeakReference;
+
+/**
+ * @internal
+ */
+function closure(callable $callable): Closure
+{
+ return Closure::fromCallable($callable);
+}
+
+/**
+ * @internal
+ * @see https://github.com/amphp/amp/blob/f682341c856b1f688026f787bef4f77eaa5c7970/src/functions.php#L140-L191
+ */
+function weaken(Closure $closure, ?object &$target = null): Closure
+{
+ $reflection = new ReflectionFunction($closure);
+ if (!$target = $reflection->getClosureThis()) {
+ return $closure;
+ }
+
+ $scope = $reflection->getClosureScopeClass();
+ $name = $reflection->getShortName();
+ if ($name !== '{closure}') {
+ /** @psalm-suppress InvalidScope @phpstan-ignore-next-line @phan-suppress-next-line PhanUndeclaredThis */
+ $closure = fn (...$args) => $this->$name(...$args);
+ if ($scope !== null) {
+ $closure = $closure->bindTo(null, $scope->name);
+ }
+ }
+
+ static $placeholder;
+ $placeholder ??= new stdClass();
+ $closure = $closure->bindTo($placeholder);
+
+ $ref = WeakReference::create($target);
+
+ /** @psalm-suppress PossiblyInvalidFunctionCall */
+ return $scope && get_class($target) === $scope->name && !$scope->isInternal()
+ ? static fn (...$args) => ($obj = $ref->get()) ? $closure->call($obj, ...$args) : null
+ : static fn (...$args) => ($obj = $ref->get()) ? $closure->bindTo($obj)(...$args) : null;
+}