Generate a version 1 UUID using the PECL UUID extension

@param int|string $node Not used in this context
@param int $clockSeq Not used in this context
@return string A binary string
13005
public function generate($node = null, $clockSeq = null)
    {
        $uuid = uuid_create(UUID_TYPE_TIME);

        return uuid_parse($uuid);
    }
793000
public static function generateCacheKey(string $ip, string $username = null, string $userRole = null): string
    {
        if (!empty($username) && !empty($userRole)) {
            return sprintf('_api_rate_limit_metadata$%s', sha1($userRole . $username));
        }

        return sprintf('_api_rate_limit_metadata$%s', sha1($ip));
    }
342367
protected function createExpireNode(Node $node, Event $event, Pbjx $pbjx): ExpireNode
    {
        $curie = $node::schema()->getCurie();
        $commandCurie = "{$curie->getVendor()}:{$curie->getPackage()}:command:expire-{$curie->getMessage()}";

        /** @var ExpireNode $class */
        $class = MessageResolver::resolveCurie(SchemaCurie::fromString($commandCurie));
        return $class::create();
    }
764655
public static function createUUID()
    {
        return self::create(
                sprintf(
                    '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                    // 32 bits for "time_low"
                    mt_rand(0, 0xffff), mt_rand(0, 0xffff),
                    // 16 bits for "time_mid"
                                                mt_rand(0, 0xffff),
                    // 16 bits for "time_hi_and_version",
                    // four most significant bits holds version number 4
                                                        mt_rand(0, 0x0fff) | 0x4000,
                    // 16 bits, 8 bits for "clk_seq_hi_res",
                    // 8 bits for "clk_seq_low",
                    // two most significant bits holds zero and one for variant DCE1.1
                                                                mt_rand(0, 0x3fff) | 0x8000,
                    // 48 bits for "node"
                                                                        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
                )
        );
    }
1356797
public static function UUID(): string
    {
//        $data = Yii::$app->security->generateRandomKey(16);
//        assert(strlen($data) == 16);
//        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
//        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
//        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));

        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

            // 32 bits for "time_low"
            random_int(0, 0xffff), random_int(0, 0xffff),

            // 16 bits for "time_mid"
            random_int(0, 0xffff),

            // 16 bits for "time_hi_and_version", four most significant bits holds version number 4
            random_int(0, 0x0fff) | 0x4000,

            // 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low", two most significant bits holds zero and
            // one for variant DCE1.1
            random_int(0, 0x3fff) | 0x8000,

            // 48 bits for "node"
            random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff)
        );
    }
766125
static private function generateName($ns, $node, $hash, $version) {
    $ns_fmt = self::detectFormat($ns);
    $field = self::convert($ns, $ns_fmt, self::FMT_FIELD);

    /* Swap byte order to keep it in big endian on all platforms */
    $field['time_low'] = self::swap32($field['time_low']);
    $field['time_mid'] = self::swap16($field['time_mid']);
    $field['time_hi'] = self::swap16($field['time_hi']);

    /* Convert the namespace to binary and concatenate node */
    $raw = self::convert($field, self::FMT_FIELD, self::FMT_BINARY);
    $raw .= $node;

    /* Hash the namespace and node and convert to a byte array */
    $val = $hash($raw, true);
    $tmp = unpack('C16', $val);
    foreach (array_keys($tmp) as $key)
      $byte[$key - 1] = $tmp[$key];

    /* Convert byte array to a field array */
    $field = self::conv_byte2field($byte);

    $field['time_low'] = self::swap32($field['time_low']);
    $field['time_mid'] = self::swap16($field['time_mid']);
    $field['time_hi'] = self::swap16($field['time_hi']);

    /* Apply version and constants */
    $field['clock_seq_hi'] &= 0x3f;
    $field['clock_seq_hi'] |= (1 << 7);
    $field['time_hi'] &= 0x0fff;
    $field['time_hi'] |= ($version << 12);

    return ($field);
  }
217848
protected function pExpr_Yield(Yield_ $node): string
    {
        if ($node->value === null) {
            return 'yield';
        }

        $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
        $shouldAddBrackets = $parentNode instanceof Expression;

        return sprintf(
            '%syield %s%s%s',
            $shouldAddBrackets ? '(' : '',
            $node->key !== null ? $this->p($node->key) . ' => ' : '',
            $this->p($node->value),
            $shouldAddBrackets ? ')' : ''
        );
    }
1320105
public static function getElementId(\DOMElement $node)
    {
        if ($node->hasAttribute('dqn_tmp_id')) {
            return $node->getAttribute('dqn_tmp_id');
        }

        $node_id = md5(mt_rand());
        $node->setAttribute('dqn_tmp_id', $node_id);
        return $node_id;
    }
884628
private function createNode(NodeInterface $node, $pathSegment, $uuid)
    {
        if ($node->hasNode($pathSegment)) {
            return $node->getNode($pathSegment);
        }

        $node = $node->addNode($pathSegment);
        $node->addMixin('mix:referenceable');
        $node->setProperty('jcr:uuid', $uuid);

        return $node;
    }
181621
public function pExpr_New(Expr\New_ $node)
    {
        if ($node->class instanceof Stmt\Class_) {
            $args = $node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '';
            return 'new ' . $this->pClassCommon($node->class, $args);
        }
        return sprintf(
            '%s %s%s%s%s',
            $this->color('new', SyntaxHighlighterConfig::TYPE_RETURN_NEW),
            $this->color($this->p($node->class), SyntaxHighlighterConfig::TYPE_CLASS),
            $this->color('(', SyntaxHighlighterConfig::TYPE_CALL_PARENTHESIS),
            $this->pCommaSeparated($node->args),
            $this->color(')', SyntaxHighlighterConfig::TYPE_CALL_PARENTHESIS)
        );
    }
1237441
public static function getRandomUuid()
    {
        return sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            // 32 bits for "time_low"
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            // 16 bits for "time_mid"
            mt_rand(0, 0xffff),
            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 4
            mt_rand(0, 0x0fff) | 0x4000,
            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            mt_rand(0, 0x3fff) | 0x8000,
            // 48 bits for "node"
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
    }
1072736
public function generateGUID5()
    {
        $hash = $this->generateUUID();

        $uuid = sprintf('%08s-%04s-%04x-%04x-%12s', // 32 bits for "time_low"
            substr($hash, 0, 8), // 16 bits for "time_mid"
            substr($hash, 8, 4), // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 5
            (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, // 48 bits for "node"
            substr($hash, 20, 12));

        return $uuid;
    }
1178858
protected static function mintName($ver, $node, $ns): string
    {
        if (empty($node)) {
            throw new \InvalidArgumentException('A name-string is required for Version 3 or 5 UUIDs.');
        }

        // if the namespace UUID isn't binary, make it so
        $ns = static::makeBin($ns, 16);
        if (null === $ns) {
            throw new \InvalidArgumentException('A binary namespace is required for Version 3 or 5 UUIDs.');
        }

        $version = $uuid = null;

        switch ($ver) {
            case static::MD5:
                $version = static::VERSION_3;
                $uuid = \md5($ns . $node, 1);
                break;
            case static::SHA1:
                $version = static::VERSION_5;
                $uuid = \substr(\sha1($ns . $node, 1), 0, 16);
                break;
            default:
                // no default really required here
        }

        // set variant
        $uuid[8] = \chr(\ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC);

        // set version
        $uuid[6] = \chr(\ord($uuid[6]) & static::CLEAR_VER | $version);

        return $uuid;
    }
683031
public function dumpNode(BaseNode $node)
    {
        $this->reference = '';
        $this->writeNode($node, 1);

        $reference = $this->reference;
        $this->reference = null;

        return $reference;
    }
1295199
private function parseXml(\DOMNode $node, array $context = [])
    {
        $data = $this->parseXmlAttributes($node, $context);

        $value = $this->parseXmlValue($node, $context);

        if (!\count($data)) {
            return $value;
        }

        if (!\is_array($value)) {
            $data['#'] = $value;

            return $data;
        }

        if (1 === \count($value) && key($value)) {
            $data[key($value)] = current($value);

            return $data;
        }

        foreach ($value as $key => $val) {
            $data[$key] = $val;
        }

        return $data;
    }
1173404
protected function build($node) {
		return preg_replace_callback(
			'/\{~(.+?)~\}|\{\*(.+?)\*\}|\{\-(.+?)\-\}|'.
			'\{\{(.+?)\}\}((\r?\n)*)/s',
			function($expr) {
				if ($expr[1])
					$str='<?php '.$this->token($expr[1]).' ?>';
				elseif ($expr[2])
					return '';
				elseif ($expr[3])
					$str=$expr[3];
				else {
					$str='<?= ('.trim($this->token($expr[4])).')'.
						($this->interpolation?
							(!empty($expr[6])?'."'.$expr[6].'"':''):'').' ?>';
					if (isset($expr[5]))
						$str.=$expr[5];
				}
				return $str;
			},
			$node
		);
	}
1174946
public synchronized UUID nextUUID() {
        byte[] rawUUID = new byte[UUID_BYTE_LENGTH];
        long time = 0;
        short clockSq = 0;
        Node node = manager.currentNode();
        while (time < 1) {
            try {
                manager.lockNode(node);
                time = node.getUUIDTime();
                clockSq = node.getClockSequence();
                System.arraycopy(node.getNodeIdentifier(), 0, rawUUID, NODE_ID_BYTE10, NODE_ID_BYTE_LENGTH);
                manager.releaseNode(node);
            } catch (OverClockedException e) {
                node = manager.nextAvailableNode();
            } finally {
                manager.releaseNode(node);
            }
        }
        byte[] timeBytes = Bytes.toBytes(time);
        //Copy time low
        System.arraycopy(timeBytes, TIME_LOW_TS_POS, rawUUID, TIME_LOW_START_POS, TIME_LOW_BYTE_LEN);
        //Copy time mid
        System.arraycopy(timeBytes, TIME_MID_TS_POS, rawUUID, TIME_MID_START_POS, TIME_MID_BYTE_LEN);
        //Copy time hi
        System.arraycopy(timeBytes, TIME_HI_TS_POS, rawUUID, TIME_HI_START_POS, TIME_HI_BYTE_LEN);
        //Set version
        rawUUID[6] |= 0x10; // 0001 0000
        //Set clock sequence
        rawUUID[CLOCK_HI_VARIANT_BYTE8] = (byte) ((clockSq & 0x3F00) >>> 8);
        rawUUID[CLOCK_HI_VARIANT_BYTE8] |= 0x80;
        rawUUID[CLOCK_LOW_BYTE9] = (byte) (clockSq & 0xFF);

        return new UUID(rawUUID);
    }