Added dot user can defined.

This commit is contained in:
李铭昕 2019-09-03 16:17:37 +08:00
parent 007e4bb2b7
commit 72eaea7131
4 changed files with 68 additions and 11 deletions

View File

@ -25,7 +25,7 @@ class InitProcessTitleListener implements ListenerInterface
/**
* @var string
*/
protected $prefix = '';
protected $name = '';
/**
* @var string
@ -36,7 +36,7 @@ class InitProcessTitleListener implements ListenerInterface
{
if ($container->has(ConfigInterface::class)) {
if ($name = $container->get(ConfigInterface::class)->get('app_name')) {
$this->prefix = $name . $this->dot;
$this->name = $name;
}
}
}
@ -53,23 +53,35 @@ class InitProcessTitleListener implements ListenerInterface
public function process(object $event)
{
$array = [];
if ($this->name !== '') {
$array[] = $this->name;
}
if ($event instanceof OnStart) {
$this->setTitle('Master');
$array[] = 'Master';
} elseif ($event instanceof OnManagerStart) {
$this->setTitle('Manager');
$array[] = 'Manager';
} elseif ($event instanceof AfterWorkerStart) {
if ($event->server->taskworker) {
$this->setTitle('TaskWorker.' . $event->workerId);
$array[] = 'TaskWorker';
$array[] = $event->workerId;
} else {
$this->setTitle('Worker.' . $event->workerId);
$array[] = 'Worker';
$array[] = $event->workerId;
}
} elseif ($event instanceof BeforeProcessHandle) {
$this->setTitle($event->process->name . '.' . $event->index);
$array[] = $event->process->name;
$array[] = $event->index;
}
if ($title = implode($this->dot, $array)) {
$this->setTitle($title);
}
}
protected function setTitle($title)
protected function setTitle(string $title)
{
@cli_set_process_title($this->prefix . $title);
@cli_set_process_title($title);
}
}

View File

@ -22,6 +22,7 @@ use Hyperf\Server\Listener\InitProcessTitleListener;
use Hyperf\Utils\Context;
use HyperfTest\Server\Stub\DemoProcess;
use HyperfTest\Server\Stub\InitProcessTitleListenerStub;
use HyperfTest\Server\Stub\InitProcessTitleListenerStub2;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
@ -83,4 +84,22 @@ class InitProcessTitleListenerTest extends TestCase
$this->assertSame($name . '.test.demo.0', Context::get('test.server.process.title'));
}
public function testUserDefinedDot()
{
$name = 'hyperf-skeleton.' . uniqid();
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('has')->with(ConfigInterface::class)->andReturn(true);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturn(false);
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
'app_name' => $name,
]));
$listener = new InitProcessTitleListenerStub2($container);
$process = new DemoProcess($container);
$listener->process(new BeforeProcessHandle($process, 0));
$this->assertSame($name . '#test.demo#0', Context::get('test.server.process.title'));
}
}

View File

@ -17,8 +17,8 @@ use Hyperf\Utils\Context;
class InitProcessTitleListenerStub extends InitProcessTitleListener
{
public function setTitle($title)
public function setTitle(string $title)
{
Context::set('test.server.process.title', $this->prefix . $title);
Context::set('test.server.process.title', $title);
}
}

View File

@ -0,0 +1,26 @@
<?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\Server\Stub;
use Hyperf\Server\Listener\InitProcessTitleListener;
use Hyperf\Utils\Context;
class InitProcessTitleListenerStub2 extends InitProcessTitleListener
{
protected $dot = '#';
public function setTitle(string $title)
{
Context::set('test.server.process.title', $title);
}
}