Merge pull request #555 from limingxinleo/1.1-runtime2

Added function swoole_hook_flags.
This commit is contained in:
李铭昕 2019-09-10 20:38:06 +08:00 committed by GitHub
commit 6eca089119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 125 additions and 4 deletions

View File

@ -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

View File

@ -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';

View File

@ -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
}
}

1
src/command/.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
/tests export-ignore

View File

@ -15,6 +15,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\Command\\": "tests/"
}
},
"require": {

View File

@ -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;
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Command\Command;
use Hyperf\Command\Command;
class DefaultSwooleFlagsCommand extends Command
{
public function handle()
{
}
public function getHookFlags(): int
{
return $this->hookFlags;
}
}

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Command\Command;
use Hyperf\Command\Command;
class SwooleFlagsCommand extends Command
{
protected $hookFlags = SWOOLE_HOOK_CURL | SWOOLE_HOOK_ALL;
public function handle()
{
}
public function getHookFlags(): int
{
return $this->hookFlags;
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Command;
use HyperfTest\Command\Command\DefaultSwooleFlagsCommand;
use HyperfTest\Command\Command\SwooleFlagsCommand;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class CommandTest extends TestCase
{
public function testHookFlags()
{
$command = new DefaultSwooleFlagsCommand('test:demo');
$this->assertSame(SWOOLE_HOOK_ALL, $command->getHookFlags());
$command = new SwooleFlagsCommand('test:demo2');
$this->assertSame(SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL, $command->getHookFlags());
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -105,4 +105,9 @@ class FunctionTest extends TestCase
$this->assertSame(1, $result);
}
}
public function testSwooleHookFlags()
{
$this->assertSame(SWOOLE_HOOK_ALL, swoole_hook_flags());
}
}