代码同步

This commit is contained in:
Ghost
2025-03-24 14:59:19 +08:00
committed by 柳清爽
parent 4e96b79acb
commit 725ea30d25
716 changed files with 90318 additions and 26155 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* Simple echo logger (only available when running in dev environment)
*/
namespace WebSocket;
class EchoLog implements \Psr\Log\LoggerInterface
{
use \Psr\Log\LoggerTrait;
public function log($level, $message, array $context = [])
{
$message = $this->interpolate($message, $context);
$context_string = empty($context) ? '' : json_encode($context);
echo str_pad($level, 8) . " | {$message} {$context_string}\n";
}
public function interpolate($message, array $context = [])
{
// Build a replacement array with braces around the context keys
$replace = [];
foreach ($context as $key => $val) {
// Check that the value can be cast to string
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
// Interpolate replacement values into the message and return
return strtr($message, $replace);
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* This class is used by tests to mock and track various socket/stream calls.
*/
namespace WebSocket;
class MockSocket
{
private static $queue = [];
private static $stored = [];
private static $asserter;
// Handler called by function overloads in mock-socket.php
public static function handle($function, $params = [])
{
$current = array_shift(self::$queue);
if ($function == 'get_resource_type' && is_null($current)) {
return null; // Catch destructors
}
self::$asserter->assertEquals($current['function'], $function);
foreach ($current['params'] as $index => $param) {
self::$asserter->assertEquals($param, $params[$index], json_encode([$current, $params]));
}
if (isset($current['error'])) {
$map = array_merge(['msg' => 'Error', 'type' => E_USER_NOTICE], (array)$current['error']);
trigger_error($map['msg'], $map['type']);
}
if (isset($current['return-op'])) {
return self::op($current['return-op'], $params, $current['return']);
}
if (isset($current['return'])) {
return $current['return'];
}
return call_user_func_array($function, $params);
}
// Check if all expected calls are performed
public static function isEmpty(): bool
{
return empty(self::$queue);
}
// Initialize call queue
public static function initialize($op_file, $asserter): void
{
$file = dirname(__DIR__) . "/scripts/{$op_file}.json";
self::$queue = json_decode(file_get_contents($file), true);
self::$asserter = $asserter;
}
// Special output handling
private static function op($op, $params, $data)
{
switch ($op) {
case 'chr-array':
// Convert int array to string
$out = '';
foreach ($data as $val) {
$out .= chr($val);
}
return $out;
case 'file':
$content = file_get_contents(__DIR__ . "/{$data[0]}");
return substr($content, $data[1], $data[2]);
case 'key-save':
preg_match('#Sec-WebSocket-Key:\s(.*)$#mUi', $params[1], $matches);
self::$stored['sec-websocket-key'] = trim($matches[1]);
return $data;
case 'key-respond':
$key = self::$stored['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
$encoded = base64_encode(pack('H*', sha1($key)));
return str_replace('{key}', $encoded, $data);
}
return $data;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* This file is used by tests to overload and mock various socket/stream calls.
*/
namespace WebSocket;
function stream_socket_server($local_socket, &$errno, &$errstr)
{
$args = [$local_socket, $errno, $errstr];
return MockSocket::handle('stream_socket_server', $args);
}
function stream_socket_accept()
{
$args = func_get_args();
return MockSocket::handle('stream_socket_accept', $args);
}
function stream_set_timeout()
{
$args = func_get_args();
return MockSocket::handle('stream_set_timeout', $args);
}
function stream_get_line()
{
$args = func_get_args();
return MockSocket::handle('stream_get_line', $args);
}
function stream_get_meta_data()
{
$args = func_get_args();
return MockSocket::handle('stream_get_meta_data', $args);
}
function feof()
{
$args = func_get_args();
return MockSocket::handle('feof', $args);
}
function ftell()
{
$args = func_get_args();
return MockSocket::handle('ftell', $args);
}
function fclose()
{
$args = func_get_args();
return MockSocket::handle('fclose', $args);
}
function fwrite()
{
$args = func_get_args();
return MockSocket::handle('fwrite', $args);
}
function fread()
{
$args = func_get_args();
return MockSocket::handle('fread', $args);
}
function fgets()
{
$args = func_get_args();
return MockSocket::handle('fgets', $args);
}
function stream_context_create()
{
$args = func_get_args();
return MockSocket::handle('stream_context_create', $args);
}
function stream_socket_client($remote_socket, &$errno, &$errstr, $timeout, $flags, $context)
{
$args = [$remote_socket, $errno, $errstr, $timeout, $flags, $context];
return MockSocket::handle('stream_socket_client', $args);
}
function get_resource_type()
{
$args = func_get_args();
return MockSocket::handle('get_resource_type', $args);
}
function stream_socket_get_name()
{
$args = func_get_args();
return MockSocket::handle('stream_socket_get_name', $args);
}

View File

@@ -0,0 +1,5 @@
128-chars
abscdefgheijklmnopqrstuvwxyz0123456789
abscdefgheijklmnopqrstuvwxyz0123456789
abscdefgheijklmnopqrstuvwxyz0123456789
a

File diff suppressed because it is too large Load Diff