From 607883c9a010b74d7354e46f16d1e6ddb238212d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 10 Sep 2019 16:26:54 +0800 Subject: [PATCH 1/5] Added function swoole_hook_flags. --- bootstrap.php | 1 + src/command/src/Command.php | 12 +++++++++++- src/server/src/Command/StartServer.php | 2 +- src/utils/src/Functions.php | 10 ++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index 56c735571..eb424aa34 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -11,5 +11,6 @@ declare(strict_types=1); */ ! 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/src/command/src/Command.php b/src/command/src/Command.php index 4f8956547..66aa3f855 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 @@ abstract class Command extends SymfonyCommand if (! $name && $this->name) { $name = $this->name; } + + if (! is_int($this->hookFlags)) { + $this->hookFlags = swoole_hook_flags(); + } + parent::__construct($name); } @@ -374,7 +384,7 @@ abstract class Command extends SymfonyCommand if ($this->coroutine && ! Coroutine::inCoroutine()) { run(function () { call([$this, 'handle']); - }); + }, $this->hookFlags); return 0; } diff --git a/src/server/src/Command/StartServer.php b/src/server/src/Command/StartServer.php index da5c68fa0..5a3e2cb89 100644 --- a/src/server/src/Command/StartServer.php +++ b/src/server/src/Command/StartServer.php @@ -42,7 +42,7 @@ class StartServer extends SymfonyCommand 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 ecb0096eb..1081c7cda 100644 --- a/src/utils/src/Functions.php +++ b/src/utils/src/Functions.php @@ -428,3 +428,13 @@ if (! function_exists('run')) { 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; + } +} From b7394e79d50a673816627e44c5342c1d2088aca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 10 Sep 2019 16:31:10 +0800 Subject: [PATCH 2/5] Update FunctionTest.php --- src/utils/tests/FunctionTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/tests/FunctionTest.php b/src/utils/tests/FunctionTest.php index 29d957658..f750bd045 100644 --- a/src/utils/tests/FunctionTest.php +++ b/src/utils/tests/FunctionTest.php @@ -105,4 +105,9 @@ class FunctionTest extends TestCase $this->assertSame(1, $result); } } + + public function testSwooleHookFlags() + { + $this->assertSame(SWOOLE_HOOK_ALL, swoole_hook_flags()); + } } From ca05d123a9e4677401cc83b3636817507e027ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 10 Sep 2019 17:13:55 +0800 Subject: [PATCH 3/5] Added testing. --- composer.json | 6 +++- src/command/.gitattributes | 1 + src/command/composer.json | 1 + .../Command/DefaultSwooleFlagsCommand.php | 27 +++++++++++++++ .../tests/Command/SwooleFlagsCommand.php | 29 ++++++++++++++++ src/command/tests/CommandTest.php | 33 +++++++++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/command/.gitattributes create mode 100644 src/command/tests/Command/DefaultSwooleFlagsCommand.php create mode 100644 src/command/tests/Command/SwooleFlagsCommand.php create mode 100644 src/command/tests/CommandTest.php diff --git a/composer.json b/composer.json index 380b61523..cb55c59a6 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 000000000..bdd4ea29c --- /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 531e0fee5..b6696b2bb 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/tests/Command/DefaultSwooleFlagsCommand.php b/src/command/tests/Command/DefaultSwooleFlagsCommand.php new file mode 100644 index 000000000..ba9f0d276 --- /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 000000000..dc34775ed --- /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 000000000..0b3df2811 --- /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()); + } +} From 630e01a88572b6718315b7fbcb670ea8f2510ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 10 Sep 2019 17:18:59 +0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3050c11f..866bb5589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,35 @@ - [#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 function `swoole_hook_flags` to get the hook flags by defining `SWOOLE_HOOK_FLAGS`. + +bin/hyperf.php + +```php +#!/usr/bin/env php +get(\Hyperf\Contract\ApplicationInterface::class); + $application->run(); +})(); + +``` ## Changed From d6ebc0c6e200b275ae149ae9ca0ac8cc3897eeae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Tue, 10 Sep 2019 17:24:06 +0800 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 866bb5589..f8b96c041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,38 +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 function `swoole_hook_flags` to get the hook flags by defining `SWOOLE_HOOK_FLAGS`. - -bin/hyperf.php - -```php -#!/usr/bin/env php -get(\Hyperf\Contract\ApplicationInterface::class); - $application->run(); -})(); - -``` +- [#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