2021-07-26 04:59:03 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Server;
|
|
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-07-26 04:59:03 +08:00
|
|
|
use Swoole\Coroutine\Channel;
|
2021-08-03 10:30:26 +08:00
|
|
|
use const Grpc\CHANNEL_SHUTDOWN;
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
class ApplicationStore
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
private static ?ApplicationStore $applicationStore = null;
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
private Channel $lock;
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:53:15 +08:00
|
|
|
/**
|
|
|
|
|
* ApplicationStore constructor.
|
|
|
|
|
*/
|
2021-07-30 17:52:09 +08:00
|
|
|
private function __construct()
|
|
|
|
|
{
|
2021-08-03 10:30:26 +08:00
|
|
|
$this->lock = new Channel(1);
|
2021-07-30 17:52:09 +08:00
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
* @return ApplicationStore|null
|
|
|
|
|
*/
|
|
|
|
|
public static function getStore(): ?ApplicationStore
|
|
|
|
|
{
|
|
|
|
|
if (!(static::$applicationStore instanceof ApplicationStore)) {
|
|
|
|
|
static::$applicationStore = new ApplicationStore();
|
|
|
|
|
}
|
|
|
|
|
return static::$applicationStore;
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2021-08-03 10:30:26 +08:00
|
|
|
public function instance(): static
|
2021-07-30 17:52:09 +08:00
|
|
|
{
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function waite(): void
|
|
|
|
|
{
|
2021-08-03 10:30:26 +08:00
|
|
|
if ($this->lock->errCode == SWOOLE_CHANNEL_CLOSED) {
|
2021-07-30 17:52:09 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->lock->pop(-1);
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-08-03 10:30:26 +08:00
|
|
|
public function close()
|
|
|
|
|
{
|
|
|
|
|
$this->lock->push(1);
|
|
|
|
|
$this->lock->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function done(): void
|
|
|
|
|
{
|
|
|
|
|
$this->lock->pop();
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isBusy(): bool
|
|
|
|
|
{
|
|
|
|
|
return !$this->lock->isEmpty();
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
|
2021-07-30 17:52:09 +08:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function getStatus(): string
|
|
|
|
|
{
|
|
|
|
|
return env('state');
|
|
|
|
|
}
|
2021-07-26 04:59:03 +08:00
|
|
|
|
|
|
|
|
}
|