Skip to content

Commit

Permalink
Related #13 跟进yii2驱动包 > initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 15, 2024
1 parent 69b24ef commit 745f3f2
Show file tree
Hide file tree
Showing 23 changed files with 604 additions and 111 deletions.
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudtay/ripple-driver",
"version": "v1.2.11",
"version": "dev-main",
"license": "MIT",
"autoload": {
"psr-4": {
Expand All @@ -23,7 +23,7 @@
"ext-posix": "*",
"ext-sockets": "*",
"ext-pdo": "*",
"cloudtay/ripple": "dev-main"
"cloudtay/ripple": "*"
},
"require-dev": {
"amphp/mysql": "^3.0",
Expand All @@ -37,7 +37,9 @@
"symfony/console": "*",
"topthink/framework": "*",
"workerman/workerman": "dev-master",
"workerman/webman-framework": "dev-master"
"workerman/webman-framework": "dev-master",
"yiisoft/yii2": "^2.0@beta",
"cloudtay/ripple": "dev-main"
},
"extra": {
"laravel": {
Expand All @@ -50,11 +52,12 @@
"Psc\\Drive\\ThinkPHP\\Service"
]
},
"include_files": [
"src/helpers.php"
]
"yii\\bootstrap": "Psc\\Drive\\Yii2\\Bootstrap"
},
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
26 changes: 13 additions & 13 deletions src/Laravel/Coroutine/ContainerMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
use Fiber;
use Illuminate\Container\Container;

use function spl_object_hash;
use function is_null;
use function spl_object_hash;

class ContainerMap
{
Expand Down Expand Up @@ -69,18 +69,6 @@ public static function unbind(): void
unset(ContainerMap::$applications[spl_object_hash($fiber)]);
}

/**
* @return \Illuminate\Container\Container
*/
public static function current(): Container
{
if (!$fiber = Fiber::getCurrent()) {
return Container::getInstance();
}

return ContainerMap::$applications[spl_object_hash($fiber)] ?? Container::getInstance();
}

/**
* @param string|null $abstract
* @param array $parameters
Expand All @@ -97,4 +85,16 @@ public static function app(string $abstract = null, array $parameters = []): mix

return $container->make($abstract, $parameters);
}

/**
* @return \Illuminate\Container\Container
*/
public static function current(): Container
{
if (!$fiber = Fiber::getCurrent()) {
return Container::getInstance();
}

return ContainerMap::$applications[spl_object_hash($fiber)] ?? Container::getInstance();
}
}
4 changes: 2 additions & 2 deletions src/Laravel/Events/RequestHandled.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class RequestHandled
public function __construct(
public Application $app,
public Application $sandbox,
public Request $request,
public Response $response
public Request $request,
public Response $response
) {
}
}
2 changes: 1 addition & 1 deletion src/Laravel/Events/RequestReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RequestReceived
public function __construct(
public Application $app,
public Application $sandbox,
public Request $request
public Request $request
) {
ContainerMap::bind($sandbox);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Laravel/Events/RequestTerminated.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class RequestTerminated
public function __construct(
public Application $app,
public Application $sandbox,
public Request $request,
public Response $response
public Request $request,
public Response $response
) {
ContainerMap::unbind();

Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Events/WorkerErrorOccurred.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WorkerErrorOccurred
public function __construct(
public Application $app,
public Application $sandbox,
public Throwable $exception
public Throwable $exception
) {
ContainerMap::unbind();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function register(): void
public function boot(): void
{
$this->publishes([
__DIR__.'/config/ripple.php' => config_path('ripple.php'),
__DIR__ . '/config/ripple.php' => config_path('ripple.php'),
], 'ripple-config');
}
}
2 changes: 1 addition & 1 deletion src/Laravel/Response/IteratorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IteratorResponse extends Response

/**
* @param Iterator|Closure $generator
* @param array $headers
* @param array $headers
*/
public function __construct(Iterator|Closure $generator, array $headers = [])
{
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Worker extends \Psc\Worker\Worker
*/
public function __construct(
private readonly string $address = 'http://127.0.0.1:8008',
int $count = 4,
int $count = 4,
private readonly bool $sandbox = true,
) {
$this->name = 'http-server';
Expand Down
1 change: 1 addition & 0 deletions src/Laravel/config/ripple.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

/*
* Copyright (c) 2023-2024.
*
Expand Down
24 changes: 12 additions & 12 deletions src/ThinkPHP/Coroutine/AppMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ public static function unbind(): void
unset(AppMap::$reference[spl_object_hash(Fiber::getCurrent())]);
}

/**
* @return \think\App|\think\Container
*/
public static function current(): App|Container
{
if (!Fiber::getCurrent()) {
return App::getInstance();
}

return \Co\container()->get(App::class) ?? App::getInstance();
}

/**
* @param string $name
* @param array $args
Expand All @@ -93,4 +81,16 @@ public static function app(string $name = '', array $args = [], bool $newInstanc

return $container->make($name ?: App::class, $args, $newInstance);
}

/**
* @return \think\App|\think\Container
*/
public static function current(): App|Container
{
if (!Fiber::getCurrent()) {
return App::getInstance();
}

return \Co\container()->get(App::class) ?? App::getInstance();
}
}
2 changes: 1 addition & 1 deletion src/ThinkPHP/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Worker extends \Psc\Worker\Worker
*/
public function __construct(
private readonly string $address = 'http://127.0.0.1:8008',
int $count = 4
int $count = 4
) {
$this->count = $count;
$this->name = 'http-server';
Expand Down
30 changes: 15 additions & 15 deletions src/Utils/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,11 @@
namespace Psc\Drive\Utils;

use function in_array;
use function strtolower;
use function is_string;
use function strtolower;

class Config
{
/**
* @param mixed $value
*
* @return bool
*/
public static function value2bool(mixed $value): bool
{
if (is_string($value)) {
$value = strtolower($value);
}
return in_array($value, ['on', 'true', 'yes', '1', 1, true], true);
}

/**
* @param mixed $value
* @param string $type
Expand All @@ -62,8 +49,21 @@ public static function value2bool(mixed $value): bool
public static function value2string(mixed $value, string $type): string
{
return match ($type) {
'bool' => Config::value2bool($value) ? 'on' : 'off',
'bool' => Config::value2bool($value) ? 'on' : 'off',
default => (string)$value,
};
}

/**
* @param mixed $value
*
* @return bool
*/
public static function value2bool(mixed $value): bool
{
if (is_string($value)) {
$value = strtolower($value);
}
return in_array($value, ['on', 'true', 'yes', '1', 1, true], true);
}
}
2 changes: 1 addition & 1 deletion src/Workerman/Driver4.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use Workerman\Events\EventInterface;
use Workerman\Worker;

use function array_search;
use function call_user_func;
use function call_user_func_array;
use function Co\cancel;
Expand All @@ -64,7 +65,6 @@
use function sleep;
use function str_contains;
use function string2int;
use function array_search;

class Driver4 implements EventInterface
{
Expand Down
26 changes: 13 additions & 13 deletions src/Workerman/Driver5.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
use Psc\Utils\Output;
use Revolt\EventLoop;
use Revolt\EventLoop\UnsupportedFeatureException;
use Workerman\Events\EventInterface;
use Throwable;
use Workerman\Events\EventInterface;

use function array_shift;
use function Co\cancelAll;
use function count;
use function getmypid;
use function pcntl_signal;
use function sleep;
use function array_shift;
use function getmypid;

use const SIGINT;
use const SIG_IGN;
use const SIGINT;

final class Driver5 implements EventInterface
{
Expand Down Expand Up @@ -268,24 +268,24 @@ public function offSignal(int $signal): bool
*
* @return bool
*/
public function offDelay(int $timerId): bool
public function offRepeat(int $timerId): bool
{
if (isset($this->eventTimer[$timerId])) {
\Co\cancel($this->eventTimer[$timerId]);
unset($this->eventTimer[$timerId]);
return true;
}
return false;
return $this->offDelay($timerId);
}

/**
* @param int $timerId
*
* @return bool
*/
public function offRepeat(int $timerId): bool
public function offDelay(int $timerId): bool
{
return $this->offDelay($timerId);
if (isset($this->eventTimer[$timerId])) {
\Co\cancel($this->eventTimer[$timerId]);
unset($this->eventTimer[$timerId]);
return true;
}
return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Workerman/Extensions/IteratorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class IteratorResponse extends Response
public function __construct(
Iterator|Closure $iterator,
protected readonly TcpConnection $tcpConnection,
protected readonly bool $autopilot = true,
protected readonly bool $closeWhenFinish = false,
protected readonly bool $autopilot = true,
) {
if ($iterator instanceof Closure) {
$iterator = $iterator();
Expand Down
Loading

0 comments on commit 745f3f2

Please sign in to comment.