diff --git a/CHANGELOG.md b/CHANGELOG.md index b3050c11f4..f8b96c0416 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ - [#418](https://github.com/hyperf-cloud/hyperf/pull/418) Allows send WebSocket message to any fd in current server, even the worker process does not hold the fd - [#420](https://github.com/hyperf-cloud/hyperf/pull/420) Added listener for model. - [#441](https://github.com/hyperf-cloud/hyperf/pull/441) Automatically close the spare redis client when it is used in low frequency. -- [#455](https://github.com/hyperf-cloud/hyperf/pull/455) Added `download()` method of `Hyperf\HttpServer\Contract\ResponseInterface`. - [#500](https://github.com/hyperf-cloud/hyperf/pull/499) Added fluent method calls of `Hyperf\HttpServer\Contract\ResponseInterface`. - [#523](https://github.com/hyperf-cloud/hyperf/pull/523) Added option `table-mapping` for command `db:model`. +- [#555](https://github.com/hyperf-cloud/hyperf/pull/555) Added global function `swoole_hook_flags` to get the hook flags by constant `SWOOLE_HOOK_FLAGS`, and you could define in `bin/hyperf.php` via `! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);` to define the constant. ## Changed diff --git a/bootstrap.php b/bootstrap.php index 56c7355718..eb424aa34e 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -11,5 +11,6 @@ */ ! defined('BASE_PATH') && define('BASE_PATH', __DIR__); +! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL); require_once BASE_PATH . '/vendor/autoload.php'; diff --git a/composer.json b/composer.json index 380b615230..cb55c59a62 100644 --- a/composer.json +++ b/composer.json @@ -157,6 +157,7 @@ "Hyperf\\ServiceGovernance\\": "src/service-governance/src/", "Hyperf\\Snowflake\\": "src/snowflake/src/", "Hyperf\\Swagger\\": "src/swagger/src/", + "Hyperf\\SwooleEnterprise\\": "src/swoole-enterprise/src/", "Hyperf\\SwooleTracker\\": "src/swoole-tracker/src/", "Hyperf\\Task\\": "src/task/src/", "Hyperf\\Testing\\": "src/testing/src/", @@ -175,6 +176,7 @@ "HyperfTest\\Amqp\\": "src/amqp/tests/", "HyperfTest\\AsyncQueue\\": "src/async-queue/tests/", "HyperfTest\\Cache\\": "src/cache/tests/", + "HyperfTest\\Command\\": "src/command/tests/", "HyperfTest\\ConfigAliyunAcm\\": "src/config-aliyun-acm/tests/", "HyperfTest\\ConfigApollo\\": "src/config-apollo/tests/", "HyperfTest\\ConfigEtcd\\": "src/config-etcd/tests/", @@ -229,6 +231,7 @@ "Hyperf\\ConfigApollo\\ConfigProvider", "Hyperf\\ConfigEtcd\\ConfigProvider", "Hyperf\\Config\\ConfigProvider", + "Hyperf\\Constants\\ConfigProvider", "Hyperf\\Consul\\ConfigProvider", "Hyperf\\Crontab\\ConfigProvider", "Hyperf\\DbConnection\\ConfigProvider", @@ -260,6 +263,7 @@ "Hyperf\\ServiceGovernance\\ConfigProvider", "Hyperf\\Snowflake\\ConfigProvider", "Hyperf\\Swagger\\ConfigProvider", + "Hyperf\\SwooleEnterprise\\ConfigProvider", "Hyperf\\SwooleTracker\\ConfigProvider", "Hyperf\\Task\\ConfigProvider", "Hyperf\\Tracer\\ConfigProvider", @@ -282,4 +286,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file diff --git a/src/command/.gitattributes b/src/command/.gitattributes new file mode 100644 index 0000000000..bdd4ea29cc --- /dev/null +++ b/src/command/.gitattributes @@ -0,0 +1 @@ +/tests export-ignore \ No newline at end of file diff --git a/src/command/composer.json b/src/command/composer.json index 531e0fee5e..b6696b2bbc 100644 --- a/src/command/composer.json +++ b/src/command/composer.json @@ -15,6 +15,7 @@ }, "autoload-dev": { "psr-4": { + "HyperfTest\\Command\\": "tests/" } }, "require": { diff --git a/src/command/src/Command.php b/src/command/src/Command.php index 4f8956547b..66aa3f8550 100644 --- a/src/command/src/Command.php +++ b/src/command/src/Command.php @@ -58,6 +58,11 @@ abstract class Command extends SymfonyCommand */ protected $coroutine = true; + /** + * @var int + */ + protected $hookFlags; + /** * The mapping between human readable verbosity levels and Symfony's OutputInterface. * @@ -77,6 +82,11 @@ public function __construct(string $name = null) if (! $name && $this->name) { $name = $this->name; } + + if (! is_int($this->hookFlags)) { + $this->hookFlags = swoole_hook_flags(); + } + parent::__construct($name); } @@ -374,7 +384,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($this->coroutine && ! Coroutine::inCoroutine()) { run(function () { call([$this, 'handle']); - }); + }, $this->hookFlags); return 0; } diff --git a/src/command/tests/Command/DefaultSwooleFlagsCommand.php b/src/command/tests/Command/DefaultSwooleFlagsCommand.php new file mode 100644 index 0000000000..ba9f0d276a --- /dev/null +++ b/src/command/tests/Command/DefaultSwooleFlagsCommand.php @@ -0,0 +1,27 @@ +hookFlags; + } +} diff --git a/src/command/tests/Command/SwooleFlagsCommand.php b/src/command/tests/Command/SwooleFlagsCommand.php new file mode 100644 index 0000000000..dc34775ed2 --- /dev/null +++ b/src/command/tests/Command/SwooleFlagsCommand.php @@ -0,0 +1,29 @@ +hookFlags; + } +} diff --git a/src/command/tests/CommandTest.php b/src/command/tests/CommandTest.php new file mode 100644 index 0000000000..0b3df2811a --- /dev/null +++ b/src/command/tests/CommandTest.php @@ -0,0 +1,33 @@ +assertSame(SWOOLE_HOOK_ALL, $command->getHookFlags()); + + $command = new SwooleFlagsCommand('test:demo2'); + $this->assertSame(SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL, $command->getHookFlags()); + } +} diff --git a/src/server/src/Command/StartServer.php b/src/server/src/Command/StartServer.php index da5c68fa02..5a3e2cb89b 100644 --- a/src/server/src/Command/StartServer.php +++ b/src/server/src/Command/StartServer.php @@ -42,7 +42,7 @@ public function __construct(ContainerInterface $container) protected function execute(InputInterface $input, OutputInterface $output) { - \Swoole\Runtime::enableCoroutine(true); + \Swoole\Runtime::enableCoroutine(true, swoole_hook_flags()); $this->checkEnvironment($output); diff --git a/src/utils/src/Functions.php b/src/utils/src/Functions.php index ecb0096eb7..1081c7cdad 100644 --- a/src/utils/src/Functions.php +++ b/src/utils/src/Functions.php @@ -428,3 +428,13 @@ function run(callable $callback, int $flags = SWOOLE_HOOK_ALL): bool return $result; } } + +if (! function_exists('swoole_hook_flags')) { + /** + * Return the default swoole hook flags, you can rewrite it by defining `SWOOLE_HOOK_FLAGS`. + */ + function swoole_hook_flags(): int + { + return defined('SWOOLE_HOOK_FLAGS') ? SWOOLE_HOOK_FLAGS : SWOOLE_HOOK_ALL; + } +} diff --git a/src/utils/tests/FunctionTest.php b/src/utils/tests/FunctionTest.php index 29d9576581..f750bd0452 100644 --- a/src/utils/tests/FunctionTest.php +++ b/src/utils/tests/FunctionTest.php @@ -105,4 +105,9 @@ public function testRetryErrorTimes() $this->assertSame(1, $result); } } + + public function testSwooleHookFlags() + { + $this->assertSame(SWOOLE_HOOK_ALL, swoole_hook_flags()); + } }