This commit is contained in:
2026-06-12 23:57:25 +08:00
parent ca8cc081bc
commit 8479106b9f
12 changed files with 148 additions and 286 deletions
-66
View File
@@ -37,13 +37,6 @@ class Kiri
}
/**
* @return Container|null
*/
public static function getContainerContext(): ?Container
{
return static::getContainer();
}
/**
@@ -131,17 +124,6 @@ class Kiri
}
/**
* @return bool
*/
public static function isDocker(): bool
{
$output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no');
if (trim($output) === 'yes') {
return true;
}
return false;
}
/**
@@ -177,54 +159,6 @@ class Kiri
}
/**
* @return mixed
*/
public static function localhost(): mixed
{
return current(swoole_get_local_ip());
}
/**
* @param array $v1
* @param array $v2
* @return float
*/
#[Pure] public static function distance(array $v1, array $v2): float
{
$maxX = max($v1['x'], $v2['x']);
$minX = min($v1['x'], $v2['x']);
$maxZ = max($v1['z'], $v2['z']);
$minZ = min($v1['z'], $v2['z']);
$dx = abs($maxX - $minX);
$dy = abs($maxZ - $minZ);
$sqrt = sqrt($dx * $dx + $dy * $dy);
if ($sqrt < 0) {
$sqrt = abs($sqrt);
}
return (float)$sqrt;
}
/**
* @param $tmp_name
* @return string
*/
public static function rename($tmp_name): string
{
$hash = md5_file($tmp_name);
$later = '.' . exif_imagetype($tmp_name);
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
return strtoupper($tmp) . $later;
}
/**
+1 -1
View File
@@ -9,7 +9,7 @@
],
"license": "MIT",
"require": {
"php": ">=8.4",
"php": ">=8.5",
"ext-json": "*",
"ext-fileinfo": "*",
"ext-pdo": "*",
+11 -3
View File
@@ -46,13 +46,21 @@ abstract class BaseApplication extends LocalService
public function __construct(public EventProvider $provider, public ConfigProvider $config, public ContainerInterface $container)
{
$this->mapping($config);
$this->parseStorage($config);
$this->parseEvents($config);
parent::__construct();
}
/**
* @return void
* @throws
*/
public function init(): void
{
$this->parseStorage($this->config);
$this->parseEvents($this->config);
}
/**
* @param ConfigProvider $config
* @return void
+17 -81
View File
@@ -1,15 +1,8 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:28
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri;
@@ -18,44 +11,23 @@ use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Psr\Container\ContainerInterface;
/**
* Class Component
* @package Kiri\Base
* @property ContainerInterface $container
* @property EventDispatch $dispatch
* @property EventProvider $provider
*/
class Component implements Configure
class Component
{
/**
* BaseAbstract constructor.
*/
public function __construct()
{
}
/**
* @return void
*/
public function init(): void
{
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
* @return StdoutLogger
* @throws
*/
public function getLogger(): StdoutLogger
@@ -63,10 +35,22 @@ class Component implements Configure
return Kiri::getLogger();
}
public function getDispatch(): EventDispatch
{
return Kiri::getDi()->get(EventDispatch::class);
}
public function getProvider(): EventProvider
{
return Kiri::getDi()->get(EventProvider::class);
}
public function getContainer(): ContainerInterface
{
return Kiri::getDi();
}
/**
* @param string $name
* @return mixed
* @throws
*/
public function __get(string $name)
@@ -74,56 +58,8 @@ class Component implements Configure
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
}
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
}
}
/**
* @param string $name
* @param $value
* @return void
* @throws
*/
public function __set(string $name, $value): void
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
}
}
/**
* @return EventDispatch
*/
public function getDispatch(): EventDispatch
{
return Kiri::getDi()->get(EventDispatch::class);
}
/**
* @return EventProvider
*/
public function getProvider(): EventProvider
{
return Kiri::getDi()->get(EventProvider::class);
}
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface
{
return Kiri::getDi();
}
}
-18
View File
@@ -1,18 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:11
*/
declare(strict_types=1);
namespace Kiri\Abstracts;
/**
* Interface Configure
* @package Kiri\Base
*/
interface Configure
{
}
+4 -6
View File
@@ -7,9 +7,7 @@ use Kiri\Coordinator;
class CoordinatorManager
{
private static array $_waite = [];
private static array $_items = [];
/**
@@ -18,10 +16,10 @@ class CoordinatorManager
*/
public static function utility(string $category): Coordinator
{
if (!((static::$_waite[$category] ?? null) instanceof Coordinator)) {
static::$_waite[$category] = new Coordinator();
if (!((static::$_items[$category] ?? null) instanceof Coordinator)) {
static::$_items[$category] = new Coordinator();
}
return static::$_waite[$category];
return static::$_items[$category];
}
}
+3
View File
@@ -57,9 +57,12 @@ class Application extends BaseApplication
$this->errorHandler->registerShutdownHandler(config('site.error.shutdown', []));
$this->errorHandler->registerExceptionHandler(config('site.error.exception', []));
$this->errorHandler->registerErrorHandler(config('site.error.error', []));
$this->id = config('site.id', uniqid('id.'));
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
parent::init();
}
+17 -7
View File
@@ -1,15 +1,19 @@
<?php
declare(strict_types=1);
namespace Kiri;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
class Coordinator
{
const string WORKER_START = 'worker:start';
private bool $waite = true;
private bool $wait = true;
private ?Channel $channel = null;
/**
@@ -17,18 +21,24 @@ class Coordinator
*/
public function yield(): void
{
while ($this->waite) {
if (Coroutine::getCid() > 0) {
$this->channel = new Channel(1);
$this->channel->pop();
} else {
while ($this->wait) {
usleep(1000);
}
}
}
/**
* @return void
*/
public function waite(): void
public function wait(): void
{
$this->waite = true;
$this->wait = true;
$this->channel = null;
}
@@ -37,8 +47,8 @@ class Coordinator
*/
public function done(): void
{
$this->waite = false;
$this->wait = false;
$this->channel?->push(true);
}
}
+4 -13
View File
@@ -22,14 +22,9 @@ class Environmental
*/
public function isMac(): bool
{
$output = strtolower(PHP_OS | PHP_OS_FAMILY);
if (str_contains('mac', $output)) {
return true;
} else if (str_contains('darwin', $output)) {
return true;
} else {
return false;
}
$os = strtolower(PHP_OS);
return str_contains($os, 'mac') || str_contains($os, 'darwin');
}
@@ -38,11 +33,7 @@ class Environmental
*/
#[Pure] public function isLinux(): bool
{
if (!static::isMac()) {
return true;
} else {
return false;
}
return PHP_OS_FAMILY === 'Linux' || strtolower(PHP_OS) === 'linux';
}
}