Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function swoole_hook_flags. #555

Merged
merged 5 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand All @@ -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/",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -282,4 +286,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
1 change: 1 addition & 0 deletions src/command/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests export-ignore
1 change: 1 addition & 0 deletions src/command/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\Command\\": "tests/"
}
},
"require": {
Expand Down
12 changes: 11 additions & 1 deletion src/command/src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 27 additions & 0 deletions src/command/tests/Command/DefaultSwooleFlagsCommand.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions src/command/tests/Command/SwooleFlagsCommand.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 33 additions & 0 deletions src/command/tests/CommandTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion src/server/src/Command/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/utils/src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/utils/tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public function testRetryErrorTimes()
$this->assertSame(1, $result);
}
}

public function testSwooleHookFlags()
{
$this->assertSame(SWOOLE_HOOK_ALL, swoole_hook_flags());
}
}