From 7cbf6e3bdf4785df9b8448414d260ced80846582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 17 Aug 2021 16:23:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + Annotation/Model/Get.php | 43 -- Annotation/Model/Relation.php | 44 -- Annotation/Model/Set.php | 39 -- Gii/Command.php | 2 +- Gii/Gii.php | 2 +- HttpServer/Controller.php | 18 +- Server/ApplicationStore.php | 1 - System/Di/Attributes.php | 128 ++++- System/Di/Container.php | 13 +- System/Di/ContainerInterface.php | 12 + System/Kiri.php | 925 ++++++++++++++++--------------- 12 files changed, 631 insertions(+), 598 deletions(-) delete mode 100644 Annotation/Model/Get.php delete mode 100644 Annotation/Model/Relation.php delete mode 100644 Annotation/Model/Set.php create mode 100644 System/Di/ContainerInterface.php diff --git a/.gitignore b/.gitignore index efca283f..348143b1 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,8 @@ themes/classic/views/ out gen +db + composer.lock *.log diff --git a/Annotation/Model/Get.php b/Annotation/Model/Get.php deleted file mode 100644 index 3b101512..00000000 --- a/Annotation/Model/Get.php +++ /dev/null @@ -1,43 +0,0 @@ -addGets($class, $this->name, $method); - return true; - } - - -} diff --git a/Annotation/Model/Relation.php b/Annotation/Model/Relation.php deleted file mode 100644 index 46c48316..00000000 --- a/Annotation/Model/Relation.php +++ /dev/null @@ -1,44 +0,0 @@ -addRelate($class, $this->name, $method); - return true; - } - -} diff --git a/Annotation/Model/Set.php b/Annotation/Model/Set.php deleted file mode 100644 index 8e977fc6..00000000 --- a/Annotation/Model/Set.php +++ /dev/null @@ -1,39 +0,0 @@ -addSets($class, $this->name, $method); - return true; - } - - -} diff --git a/Gii/Command.php b/Gii/Command.php index a23d716a..7a5a7f05 100644 --- a/Gii/Command.php +++ b/Gii/Command.php @@ -33,7 +33,7 @@ class Command extends \Console\Command /** @var Gii $gii */ $gii = Kiri::app()->get('gii'); - $connections = Kiri::app()->db; + $connections = Kiri::app()->get('db'); if ($dtl->exists('databases')) { return $gii->run($connections->get($dtl->get('databases')), $dtl); } diff --git a/Gii/Gii.php b/Gii/Gii.php index 42f42c1b..6f269f25 100644 --- a/Gii/Gii.php +++ b/Gii/Gii.php @@ -123,7 +123,7 @@ class Gii return $this->makeByDatabases($make, $input); } $db = $this->input->get('databases', 'db'); - $this->db = Kiri::app()->db->get($db); + $this->db = Kiri::app()->get('db')->get($db); return $this->makeByDatabases($make, $input); } diff --git a/HttpServer/Controller.php b/HttpServer/Controller.php index f7c3a4c9..426d06ca 100644 --- a/HttpServer/Controller.php +++ b/HttpServer/Controller.php @@ -5,8 +5,12 @@ namespace HttpServer; use Annotation\Inject; +use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\TraitApplication; use Kiri\Application; +use Kiri\Di\Container; +use Kiri\Di\ContainerInterface; +use Kiri\Kiri; use Server\RequestInterface; use Server\ResponseInterface; @@ -22,14 +26,22 @@ class Controller /** - * @param Application $container + * @param Application $application */ - public function __construct(protected Application $container) + #[Pure] public function __construct(protected Application $application) { - } + /** + * inject di container + * + * @var ContainerInterface|null + */ + #[Inject(ContainerInterface::class)] + public ?ContainerInterface $container = null; + + /** * inject request * diff --git a/Server/ApplicationStore.php b/Server/ApplicationStore.php index 97ec1297..951c306f 100644 --- a/Server/ApplicationStore.php +++ b/Server/ApplicationStore.php @@ -6,7 +6,6 @@ namespace Server; use JetBrains\PhpStorm\Pure; use Swoole\Coroutine\Channel; -use const Grpc\CHANNEL_SHUTDOWN; class ApplicationStore { diff --git a/System/Di/Attributes.php b/System/Di/Attributes.php index 4ec7445d..b3a3a5fa 100644 --- a/System/Di/Attributes.php +++ b/System/Di/Attributes.php @@ -3,8 +3,10 @@ namespace Kiri\Di; use JetBrains\PhpStorm\Pure; +use ReflectionAttribute; use ReflectionClass; use ReflectionMethod; +use ReflectionProperty; trait Attributes { @@ -17,6 +19,20 @@ trait Attributes private array $_classProperty = []; + private array $_mapping = [ + 'attributeClass' => [ + 'className' => [ + 'property' => [ + '' + ], + 'method' => [ + + ] + ] + ] + ]; + + /** * @param ReflectionClass $class */ @@ -31,10 +47,65 @@ trait Attributes continue; } $this->_classTarget[$className][] = $attribute->newInstance(); + + $this->setMappingClass($attribute, $className); } } + /** + * @param ReflectionAttribute $attribute + * @param string $class + */ + private function setMappingClass(ReflectionAttribute $attribute, string $class) + { + if (!isset($this->_mapping[$attribute->getName()])) { + $this->_mapping[$attribute->getName()] = []; + } + $this->_mapping[$attribute->getName()][$class] = []; + } + + + /** + * @param ReflectionAttribute $attribute + * @param string $class + * @param string $method + */ + private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method) + { + $this->setMappingClass($attribute, $class); + + $mapping = $this->_mapping[$attribute->getName()][$class]; + if (!isset($mapping['method'])) { + $mapping['method'] = []; + } + if (!in_array($method, $mapping['method'])) { + $mapping['method'][] = $method; + } + $this->_mapping[$attribute->getName()][$class] = $mapping; + } + + + /** + * @param ReflectionAttribute $attribute + * @param string $class + * @param string $property + */ + private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property) + { + $this->setMappingClass($attribute, $class); + + $mapping = $this->_mapping[$attribute->getName()][$class]; + if (!isset($mapping['property'])) { + $mapping['property'] = []; + } + if (!in_array($property, $mapping['property'])) { + $mapping['property'][] = $property; + } + $this->_mapping[$attribute->getName()][$class] = $mapping; + } + + /** * @param mixed $class * @return array @@ -65,6 +136,8 @@ trait Attributes continue; } $this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $attribute->newInstance(); + + $this->setMappingMethod($attribute, $className, $ReflectionMethod->getName()); } } } @@ -98,19 +171,68 @@ trait Attributes { $className = $class->getName(); $this->_classProperty[$className] = $this->_classPropertyNote[$className] = []; - foreach ($class->getProperties(\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PUBLIC | - \ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) { + foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC | + ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) { $this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod; foreach ($ReflectionMethod->getAttributes() as $attribute) { if (!class_exists($attribute->getName())) { continue; } $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $attribute->newInstance(); + + $this->setMappingProperty($attribute, $className, $ReflectionMethod->getName()); } } } + /** + * @param string $attribute + * @param string|null $class + * @return array[] + */ + public function getAttributeTrees(string $attribute, string $class = null): array + { + $mapping = $this->_mapping[$attribute] ?? []; + if (empty($mapping) || empty($class)) { + return $mapping; + } + return $mapping[$class] ?? []; + } + + + /** + * @param string $attribute + * @param string $class + * @param string|null $method + * @return array + */ + public function getMethodByAnnotation(string $attribute, string $class, string $method = null): array + { + $class = $this->getAttributeTrees($attribute, $class); + if (empty($class) || !isset($class['method']) || empty($method)) { + return $class['method'] ?? []; + } + return $class['method'][$method] ?? []; + } + + + /** + * @param string $attribute + * @param string $class + * @param string $method + * @return array + */ + public function getPropertyByAnnotation(string $attribute, string $class, string $method): array + { + $class = $this->getAttributeTrees($attribute, $class); + if (empty($class) || !isset($class['property'])) { + return []; + } + return $class['property'][$method] ?? []; + } + + /** * @param ReflectionClass|string $class * @return array @@ -127,7 +249,7 @@ trait Attributes /** * @param ReflectionClass $class - * @return \ReflectionProperty[] + * @return ReflectionProperty[] */ #[Pure] public function getProperty(ReflectionClass $class): array { diff --git a/System/Di/Container.php b/System/Di/Container.php index 1b16327e..fcae53c4 100644 --- a/System/Di/Container.php +++ b/System/Di/Container.php @@ -28,7 +28,7 @@ use Server\ResponseInterface; * Class Container * @package Kiri\Di */ -class Container extends BaseObject +class Container extends BaseObject implements ContainerInterface { use Attributes; @@ -119,6 +119,17 @@ class Container extends BaseObject } + /** + * @param string $interface + * @param $object + */ + public function setBindings(string $interface, $object) + { + $this->_singletons[$interface] = $object; + $this->_interfaces[$interface] = get_class($object); + } + + /** * @param $class * @param array $constrict diff --git a/System/Di/ContainerInterface.php b/System/Di/ContainerInterface.php new file mode 100644 index 00000000..ba287c86 --- /dev/null +++ b/System/Di/ContainerInterface.php @@ -0,0 +1,12 @@ +getClassReflectionProperty($class::class); - /** - * @var string $property - * @var ReflectionProperty $attribute - */ - foreach ($attributes as $property => $attribute) { + /** + * @param object $class + */ + public static function injectProperty(object $class) + { + $attributes = static::getDi()->getClassReflectionProperty($class::class); + /** + * @var string $property + * @var ReflectionProperty $attribute + */ + foreach ($attributes as $property => $attribute) { - foreach ($attribute->getAttributes() as $item) { - $item->newInstance()->execute($class, $property); - } - } - } + foreach ($attribute->getAttributes() as $item) { + $item->newInstance()->execute($class, $property); + } + } + } - /** - * @param $service - * - * 初始化服务 - */ - public static function init($service) - { - static::$service = $service; - } + /** + * @param $service + * + * 初始化服务 + */ + public static function init($service) + { + static::$service = $service; + } - /** - * @param $alias - * @param array $array - * @throws Exception - */ - public static function set($alias, array $array = []) - { - static::app()->set($alias, $array); - } + /** + * @param $alias + * @param array $array + * @throws Exception + */ + public static function set($alias, array $array = []) + { + static::app()->set($alias, $array); + } - /** - * @param string $name - * @return mixed - * @throws Exception - */ - public static function getApp(string $name): mixed - { - return static::app()->get($name); - } + /** + * @param string $name + * @return mixed + * @throws Exception + */ + public static function getApp(string $name): mixed + { + return static::app()->get($name); + } - /** - * @return Application|null - */ - public static function app(): ?Application - { - return static::$service; - } + /** + * @return Application|null + */ + public static function app(): ?Application + { + return static::$service; + } - /** - * @return Application|null - */ - public static function getFactory(): ?Application - { - return static::$service; - } + /** + * @return Application|null + */ + public static function getFactory(): ?Application + { + return static::$service; + } /** * @param $name @@ -126,329 +126,329 @@ class Kiri * @throws NotFindClassException * @throws ReflectionException */ - public static function has($name): bool - { - return static::$service->has($name); - } + public static function has($name): bool + { + return static::$service->has($name); + } - /** - * @param $port - * @return bool - * @throws Exception - */ - public static function port_already($port): bool - { - if (empty($port)) { - return false; - } - if (Kiri::getPlatform()->isLinux()) { - exec('netstat -tunlp | grep ' . $port, $output); - } else { - exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); - } - return !empty($output); - } + /** + * @param $port + * @return bool + * @throws Exception + */ + public static function port_already($port): bool + { + if (empty($port)) { + return false; + } + if (Kiri::getPlatform()->isLinux()) { + exec('netstat -tunlp | grep ' . $port, $output); + } else { + exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); + } + return !empty($output); + } - /** - * @return Annotation - * @throws Exception - */ - public static function getAnnotation(): Annotation - { - return static::app()->getAnnotation(); - } + /** + * @return Annotation + * @throws Exception + */ + public static function getAnnotation(): Annotation + { + return static::app()->getAnnotation(); + } - /** - * @param $service - * @return string - */ - #[Pure] public static function listen($service): string - { - return sprintf('Check listen %s::%d -> ok', $service['host'], $service['port']); - } + /** + * @param $service + * @return string + */ + #[Pure] public static function listen($service): string + { + return sprintf('Check listen %s::%d -> ok', $service['host'], $service['port']); + } - /** - * @param $className - * @param array $construct - * @return mixed - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - public static function createObject($className, array $construct = []): mixed - { - if (is_string($className) && class_exists($className)) { - return static::$container->get($className, $construct); - } else if (is_array($className) && isset($className['class'])) { - $class = $className['class']; - unset($className['class']); - return static::$container->newObject($class, $construct, $className); - } else if (is_callable($className, TRUE)) { - return call_user_func($className, $construct); - } else { - throw new Exception('Unsupported configuration type: ' . gettype($className)); - } - } + /** + * @param $className + * @param array $construct + * @return mixed + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + public static function createObject($className, array $construct = []): mixed + { + if (is_string($className) && class_exists($className)) { + return static::$container->get($className, $construct); + } else if (is_array($className) && isset($className['class'])) { + $class = $className['class']; + unset($className['class']); + return static::$container->newObject($class, $construct, $className); + } else if (is_callable($className, TRUE)) { + return call_user_func($className, $construct); + } else { + throw new Exception('Unsupported configuration type: ' . gettype($className)); + } + } - /** - * @return string - * @throws Exception - */ - public static function getStoragePath(): string - { - $default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR; - $path = Config::get('storage', $default); - if (!is_dir($path)) { - mkdir($path, 0777, true); - } - return $path; - } + /** + * @return string + * @throws Exception + */ + public static function getStoragePath(): string + { + $default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR; + $path = Config::get('storage', $default); + if (!is_dir($path)) { + mkdir($path, 0777, true); + } + return $path; + } - /** - * @return bool - */ - public static function inCoroutine(): bool - { - return Coroutine::getCid() > 0; - } + /** + * @return bool + */ + public static function inCoroutine(): bool + { + return Coroutine::getCid() > 0; + } - /** - * @return Container - */ - public static function getDi(): Container - { - return static::$container; - } + /** + * @return Container + */ + public static function getDi(): Container + { + return static::$container; + } - /** - * @param $workerId - * @return mixed - * @throws Exception - */ - public static function setManagerId($workerId): mixed - { - if (empty($workerId) || static::isDocker()) { - return $workerId; - } + /** + * @param $workerId + * @return mixed + * @throws Exception + */ + public static function setManagerId($workerId): mixed + { + if (empty($workerId) || static::isDocker()) { + return $workerId; + } - $tmpFile = storage($workerId . '.sock', 'pid/manager'); + $tmpFile = storage($workerId . '.sock', 'pid/manager'); - return self::writeFile($tmpFile, $workerId); - } + return self::writeFile($tmpFile, $workerId); + } - /** - * @param $workerId - * @return mixed - * @throws Exception - */ - public static function setProcessId($workerId): mixed - { - if (empty($workerId) || static::isDocker()) { - return $workerId; - } + /** + * @param $workerId + * @return mixed + * @throws Exception + */ + public static function setProcessId($workerId): mixed + { + if (empty($workerId) || static::isDocker()) { + return $workerId; + } - $tmpFile = storage($workerId . '.sock', 'pid/process'); + $tmpFile = storage($workerId . '.sock', 'pid/process'); - return self::writeFile($tmpFile, $workerId); - } + return self::writeFile($tmpFile, $workerId); + } - /** - * @return bool - */ - public static function isDocker(): bool - { - $output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no'); - if (trim($output) === 'yes') { - return true; - } - return false; - } + /** + * @return bool + */ + public static function isDocker(): bool + { + $output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no'); + if (trim($output) === 'yes') { + return true; + } + return false; + } - /** - * @param $workerId - * @return mixed - * @throws Exception - */ - public static function setWorkerId($workerId): mixed - { - if (empty($workerId) || static::isDocker()) { - return $workerId; - } + /** + * @param $workerId + * @return mixed + * @throws Exception + */ + public static function setWorkerId($workerId): mixed + { + if (empty($workerId) || static::isDocker()) { + return $workerId; + } - $tmpFile = storage($workerId . '.sock', 'pid/worker'); + $tmpFile = storage($workerId . '.sock', 'pid/worker'); - return self::writeFile($tmpFile, $workerId); - } + return self::writeFile($tmpFile, $workerId); + } - /** - * @param $workerId - * @return mixed - * @throws Exception - */ - public static function setTaskId($workerId): mixed - { - if (empty($workerId) || static::isDocker()) { - return $workerId; - } + /** + * @param $workerId + * @return mixed + * @throws Exception + */ + public static function setTaskId($workerId): mixed + { + if (empty($workerId) || static::isDocker()) { + return $workerId; + } - $tmpFile = storage($workerId . '.sock', 'pid/task'); + $tmpFile = storage($workerId . '.sock', 'pid/task'); - return self::writeFile($tmpFile, $workerId); - } + return self::writeFile($tmpFile, $workerId); + } - /** - * @param $fileName - * @param $content - * @param null $is_append - * @return mixed - */ - public static function writeFile($fileName, $content, $is_append = null): mixed - { - $params = [$fileName, (string)$content]; - if ($is_append !== null) { - $params[] = $is_append; - } - return !self::inCoroutine() ? file_put_contents(...$params) : Coroutine::writeFile(...$params); - } + /** + * @param $fileName + * @param $content + * @param null $is_append + * @return mixed + */ + public static function writeFile($fileName, $content, $is_append = null): mixed + { + $params = [$fileName, (string)$content]; + if ($is_append !== null) { + $params[] = $is_append; + } + return !self::inCoroutine() ? file_put_contents(...$params) : Coroutine::writeFile(...$params); + } - /** - * @param $object - * @param $config - * @return mixed - */ - public static function configure($object, $config): mixed - { - foreach ($config as $key => $value) { - if (!property_exists($object, $key)) { - continue; - } - $object->$key = $value; - } - return $object; - } + /** + * @param $object + * @param $config + * @return mixed + */ + public static function configure($object, $config): mixed + { + foreach ($config as $key => $value) { + if (!property_exists($object, $key)) { + continue; + } + $object->$key = $value; + } + return $object; + } - /** - * @param $workerId - * @param bool $isWorker - * @throws Exception - */ - public static function clearProcessId($workerId, $isWorker = false) - { - clearstatcache(); - $directory = $isWorker === true ? 'pid/worker' : 'pid/task'; - if (!file_exists($file = storage($workerId, $directory))) { - return; - } - shell_exec('rm -rf ' . $file); - } + /** + * @param $workerId + * @param bool $isWorker + * @throws Exception + */ + public static function clearProcessId($workerId, $isWorker = false) + { + clearstatcache(); + $directory = $isWorker === true ? 'pid/worker' : 'pid/task'; + if (!file_exists($file = storage($workerId, $directory))) { + return; + } + shell_exec('rm -rf ' . $file); + } - /** - * @param string|null $taskPid - * @throws Exception - */ - public static function clearTaskPid(string $taskPid = null) - { - if (empty($taskPid)) { - exec('rm -rf ' . storage(null, 'pid/task')); - } else { - static::clearProcessId($taskPid); - } - } + /** + * @param string|null $taskPid + * @throws Exception + */ + public static function clearTaskPid(string $taskPid = null) + { + if (empty($taskPid)) { + exec('rm -rf ' . storage(null, 'pid/task')); + } else { + static::clearProcessId($taskPid); + } + } - /** - * @param $taskPid - * @throws Exception - */ - public static function clearWorkerPid($taskPid = null) - { - if (empty($taskPid)) { - exec('rm -rf ' . storage(null, 'pid/worker')); - } else { - static::clearProcessId($taskPid, true); - } - } + /** + * @param $taskPid + * @throws Exception + */ + public static function clearWorkerPid($taskPid = null) + { + if (empty($taskPid)) { + exec('rm -rf ' . storage(null, 'pid/worker')); + } else { + static::clearProcessId($taskPid, true); + } + } - /** - * @return Server|null - * @throws - */ - public static function getWebSocket(): ?\Swoole\Server - { - $server = static::app()->getSwoole(); - if (!($server instanceof \Swoole\Server)) { - return null; - } - return $server; - } + /** + * @return Server|null + * @throws + */ + public static function getWebSocket(): ?\Swoole\Server + { + $server = static::app()->getSwoole(); + if (!($server instanceof \Swoole\Server)) { + return null; + } + return $server; + } - /** - * @return false|string - * @throws Exception - */ - public static function getMasterPid(): bool|string - { - $pid = Kiri::app()->getSwoole()->setting['pid_file']; + /** + * @return false|string + * @throws Exception + */ + public static function getMasterPid(): bool|string + { + $pid = Kiri::app()->getSwoole()->setting['pid_file']; - return file_get_contents($pid); - } + return file_get_contents($pid); + } - /** - * @param int $fd - * @param $data - * @return mixed - * @throws Exception - */ - public static function push(int $fd, $data): mixed - { - $server = static::getWebSocket(); - if (empty($server) || !$server->isEstablished($fd)) { - return false; - } - if (!is_string($data)) { - $data = Json::encode($data); - } - return $server->push($fd, $data); - } + /** + * @param int $fd + * @param $data + * @return mixed + * @throws Exception + */ + public static function push(int $fd, $data): mixed + { + $server = static::getWebSocket(); + if (empty($server) || !$server->isEstablished($fd)) { + return false; + } + if (!is_string($data)) { + $data = Json::encode($data); + } + return $server->push($fd, $data); + } - /** - * @return mixed - */ - public static function localhost(): mixed - { - return current(swoole_get_local_ip()); - } + /** + * @return mixed + */ + public static function localhost(): mixed + { + return current(swoole_get_local_ip()); + } - /** - * @param string $class - * @param array $params - * @throws ReflectionException - * @throws Exception - */ - public static function async(string $class, array $params = []) - { - $manager = ServerManager::getContext(); - $manager->task(new $class(...$params)); - } + /** + * @param string $class + * @param array $params + * @throws ReflectionException + * @throws Exception + */ + public static function async(string $class, array $params = []) + { + $manager = ServerManager::getContext(); + $manager->task(new $class(...$params)); + } /** @@ -456,167 +456,168 @@ class Kiri * @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']); + #[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']); + $maxZ = max($v1['z'], $v2['z']); + $minZ = min($v1['z'], $v2['z']); - $dx = abs($maxX - $minX); - $dy = abs($maxZ - $minZ); + $dx = abs($maxX - $minX); + $dy = abs($maxZ - $minZ); - $sqrt = sqrt($dx * $dx + $dy * $dy); - if ($sqrt < 0) { - $sqrt = abs($sqrt); - } - return (float)$sqrt; - } + $sqrt = sqrt($dx * $dx + $dy * $dy); + if ($sqrt < 0) { + $sqrt = abs($sqrt); + } + return (float)$sqrt; + } - /** - * @param $process - * @throws Exception - */ - public static function shutdown($process): void - { - static::app()->getSwoole()->shutdown(); - if ($process instanceof Process) { - $process->exit(0); - } - } + /** + * @param $process + * @throws Exception + */ + public static function shutdown($process): void + { + static::app()->getSwoole()->shutdown(); + if ($process instanceof Process) { + $process->exit(0); + } + } - /** - * @param $tmp_name - * @return string - */ - public static function rename($tmp_name): string - { - $hash = md5_file($tmp_name); + /** + * @param $tmp_name + * @return string + */ + public static function rename($tmp_name): string + { + $hash = md5_file($tmp_name); - $later = '.' . exif_imagetype($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); + $match = '/(\w{12})(\w{5})(\w{9})(\w{6})/'; + $tmp = preg_replace($match, '$1-$2-$3-$4', $hash); - return strtoupper($tmp) . $later; - } + return strtoupper($tmp) . $later; + } - /** - * @return Environmental - * @throws - */ - public static function getPlatform(): Environmental - { - return Kiri::createObject(Environmental::class); - } + /** + * @return Environmental + * @throws + */ + public static function getPlatform(): Environmental + { + return Kiri::createObject(Environmental::class); + } - /** - * @return mixed - * @throws Exception - */ - public static function reload(): mixed - { - return Kiri::app()->getSwoole()->reload(); - } + /** + * @return mixed + * @throws Exception + */ + public static function reload(): mixed + { + return Kiri::app()->getSwoole()->reload(); + } - private static array $_autoload = []; + private static array $_autoload = []; - const PROCESS = 'process'; - const TASK = 'task'; - const WORKER = 'worker'; + const PROCESS = 'process'; + const TASK = 'task'; + const WORKER = 'worker'; - /** - * @param string $event - * @param null $data - * @return false|string - * @throws Exception - */ - public static function param(string $event, $data = NULL): bool|string - { - if (is_object($data)) { - if ($data instanceof ActiveRecord || $data instanceof Collection) { - $data = $data->getAttributes(); - } else { - $data = get_object_vars($data); - } - } - if (!is_array($data)) $data = ['data' => $data]; - return json_encode(array_merge(['callback' => $event], $data)); - } + /** + * @param string $event + * @param null $data + * @return false|string + * @throws Exception + */ + public static function param(string $event, $data = NULL): bool|string + { + if (is_object($data)) { + if ($data instanceof ActiveRecord || $data instanceof Collection) { + $data = $data->getAttributes(); + } else { + $data = get_object_vars($data); + } + } + if (!is_array($data)) $data = ['data' => $data]; + return json_encode(array_merge(['callback' => $event], $data)); + } - /** - * @return string|null - */ - #[Pure] public static function getEnvironmental(): ?string - { - return env('environmental'); - } + /** + * @return string|null + */ + #[Pure] public static function getEnvironmental(): ?string + { + return env('environmental'); + } - /** - * @return bool - */ - #[Pure] public static function isTask(): bool - { - return static::getEnvironmental() == static::TASK; - } + /** + * @return bool + */ + #[Pure] public static function isTask(): bool + { + return static::getEnvironmental() == static::TASK; + } - /** - * @return bool - */ - #[Pure] public static function isWorker(): bool - { - return static::getEnvironmental() == static::WORKER; - } + /** + * @return bool + */ + #[Pure] public static function isWorker(): bool + { + return static::getEnvironmental() == static::WORKER; + } - /** - * @return bool - */ - #[Pure] public static function isProcess(): bool - { - return static::getEnvironmental() == static::PROCESS; - } + /** + * @return bool + */ + #[Pure] public static function isProcess(): bool + { + return static::getEnvironmental() == static::PROCESS; + } - /** - * @param $class - * @param $file - */ - public static function setAutoload($class, $file) - { - if (isset(static::$_autoload[$class])) { - return; - } - static::$_autoload[$class] = $file; - include_once "$file"; - } + /** + * @param $class + * @param $file + */ + public static function setAutoload($class, $file) + { + if (isset(static::$_autoload[$class])) { + return; + } + static::$_autoload[$class] = $file; + include_once "$file"; + } - /** - * @param $className - */ - public static function autoload($className) - { - if (!isset(static::$_autoload[$className])) { - return; - } - $file = static::$_autoload[$className]; - require_once "$file"; - } + /** + * @param $className + */ + public static function autoload($className) + { + if (!isset(static::$_autoload[$className])) { + return; + } + $file = static::$_autoload[$className]; + require_once "$file"; + } } //spl_autoload_register([Kiri::class, 'autoload'], true, true); Kiri::$container = new Container(); +Kiri::$container->setBindings(ContainerInterface::class, Kiri::$container);