mkm b7951806b8 feat(admin): 优化订单创建逻辑
- 将订单创建时的参数从 $order 变为 $params,提高代码可读性
- 更新 composer.json,将 webman-framework 版本升级到 1.6
- 修改 worker_start 函数,增加自定义 worker 类功能
- 优化 view 函数,增加对模板输入的统一处理
- 更新 cpu_count 函数,增加错误处理机制
2024-11-19 12:11:23 +08:00

138 lines
2.9 KiB
PHP

<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Webman;
use Fiber;
use SplObjectStorage;
use StdClass;
use Swow\Coroutine;
use WeakMap;
use Workerman\Events\Revolt;
use Workerman\Events\Swoole;
use Workerman\Events\Swow;
use Workerman\Worker;
use function property_exists;
/**
* Class Context
* @package Webman
*/
class Context
{
/**
* @var SplObjectStorage|WeakMap
*/
protected static $objectStorage;
/**
* @var StdClass
*/
protected static $object;
/**
* @return void
*/
public static function init()
{
if (!static::$objectStorage) {
static::$objectStorage = class_exists(WeakMap::class) ? new WeakMap() : new SplObjectStorage();
static::$object = new StdClass;
}
}
/**
* @return StdClass
*/
protected static function getObject(): StdClass
{
$key = static::getKey();
if (!isset(static::$objectStorage[$key])) {
static::$objectStorage[$key] = new StdClass;
}
return static::$objectStorage[$key];
}
/**
* @return mixed
*/
protected static function getKey()
{
switch (Worker::$eventLoopClass) {
case Revolt::class:
return Fiber::getCurrent();
case Swoole::class:
return \Swoole\Coroutine::getContext();
case Swow::class:
return Coroutine::getCurrent();
}
return static::$object;
}
/**
* @param string|null $key
* @return mixed
*/
public static function get(string $key = null)
{
$obj = static::getObject();
if ($key === null) {
return $obj;
}
return $obj->$key ?? null;
}
/**
* @param string $key
* @param $value
* @return void
*/
public static function set(string $key, $value): void
{
$obj = static::getObject();
$obj->$key = $value;
}
/**
* @param string $key
* @return void
*/
public static function delete(string $key): void
{
$obj = static::getObject();
unset($obj->$key);
}
/**
* @param string $key
* @return bool
*/
public static function has(string $key): bool
{
$obj = static::getObject();
return property_exists($obj, $key);
}
/**
* @return void
*/
public static function destroy(): void
{
unset(static::$objectStorage[static::getKey()]);
}
}