feat:examples:增加init预加载,应对协程并发下单例多次new的问题

This commit is contained in:
liujian 2021-09-18 12:27:18 +08:00
parent 3d76b84c8c
commit 8c16940c7a
40 changed files with 272 additions and 174 deletions

View File

@ -6,13 +6,16 @@ ini_set('error_reporting', E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use Dotenv\Dotenv;
use Mix\Cli\Cli;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
Cli::setName('app')->setVersion('0.0.0-alpha');
$cmds = [

View File

@ -7,14 +7,17 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init');
/**
* 多进程默认开启了协程
@ -29,10 +32,11 @@ $http->on('Request', $vega->handler());
$http->on('WorkerStart', function ($server, $workerId) {
// swoole 协程不支持 set_exception_handler 需要手动捕获异常
try {
Finder::in(__DIR__ . '/../src/Container')->exec('connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
} catch (\Throwable $ex) {
App\Error::handle($ex);
Error::handle($ex);
}
});
$http->set([

View File

@ -7,25 +7,28 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Swoole\Coroutine\run(function () {
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
$vega = Vega::new();
$host = '0.0.0.0';
$port = 9502;
$server = new Swoole\Coroutine\Http\Server($host, $port, false, false);
$server->handle('/', $vega->handler());
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
foreach ([SIGHUP, SIGINT, SIGTERM] as $signal) {
Swoole\Process::signal($signal, function () use ($server) {
Logger::instance()->info('Shutdown swoole coroutine server');

View File

@ -7,18 +7,24 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init');
$vega = Vega::new();
$addr = 'http://0.0.0.0:2345';
$http = new Workerman\Worker($addr);
$http->onWorkerStart = function ($worker) {
Finder::in(__DIR__ . '/../src/Container')->exec('connect');
};
$http->onMessage = $vega->handler();
$http->count = 4;

View File

@ -21,6 +21,7 @@
},
"require": {
"workerman/workerman": "^4.0",
"mix/init": "~3.0.0",
"mix/vega": "~3.0.0",
"mix/cli": "~3.0.0",
"mix/database": "~3.0.0",

View File

@ -14,15 +14,17 @@ class Config
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
/**
* @return \Noodlehaus\Config
*/
public static function instance(): \Noodlehaus\Config
{
if (!isset(self::$instance)) {
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
return self::$instance;
}
}
}

View File

@ -12,19 +12,21 @@ class DB
*/
static private $instance;
public static function connect(): void
{
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
/**
* @return Database
*/
public static function instance(): Database
{
if (!isset(self::$instance)) {
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
return self::$instance;
}

View File

@ -15,19 +15,21 @@ class Logger implements HandlerInterface
static private $instance;
public static function init(): void
{
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
/**
* @return \Monolog\Logger
*/
public static function instance(): \Monolog\Logger
{
if (!isset(self::$instance)) {
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
return self::$instance;
}

View File

@ -12,20 +12,22 @@ class RDS
*/
static private $instance;
public static function connect(): void
{
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
/**
* @return Redis
*/
public static function instance(): Redis
{
if (!isset(self::$instance)) {
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
return self::$instance;
}

View File

@ -6,13 +6,16 @@ ini_set('error_reporting', E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use Dotenv\Dotenv;
use Mix\Cli\Cli;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
Cli::setName('app')->setVersion('0.0.0-alpha');
$cmds = [

View File

@ -16,6 +16,7 @@
]
},
"require": {
"mix/init": "~3.0.0",
"mix/cli": "~3.0.0",
"mix/database": "~3.0.0",
"mix/redis": "~3.0.0",

View File

@ -14,15 +14,17 @@ class Config
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
/**
* @return \Noodlehaus\Config
*/
public static function instance(): \Noodlehaus\Config
{
if (!isset(self::$instance)) {
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
return self::$instance;
}
}
}

View File

@ -12,19 +12,21 @@ class DB
*/
static private $instance;
public static function connect(): void
{
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
/**
* @return Database
*/
public static function instance(): Database
{
if (!isset(self::$instance)) {
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
return self::$instance;
}

View File

@ -15,19 +15,21 @@ class Logger implements HandlerInterface
static private $instance;
public static function init(): void
{
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
/**
* @return \Monolog\Logger
*/
public static function instance(): \Monolog\Logger
{
if (!isset(self::$instance)) {
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
return self::$instance;
}

View File

@ -12,20 +12,22 @@ class RDS
*/
static private $instance;
public static function connect(): void
{
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
/**
* @return Redis
*/
public static function instance(): Redis
{
if (!isset(self::$instance)) {
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
return self::$instance;
}

View File

@ -6,13 +6,16 @@ ini_set('error_reporting', E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use Dotenv\Dotenv;
use Mix\Cli\Cli;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
Cli::setName('app')->setVersion('0.0.0-alpha');
$cmds = [

View File

@ -7,14 +7,17 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Grpc;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init');
/**
* 多进程默认开启了协程
@ -29,6 +32,7 @@ $http->on('Request', $grpc->handler());
$http->on('WorkerStart', function ($server, $workerId) {
// swoole 协程不支持 set_exception_handler 需要手动捕获异常
try {
Finder::in(__DIR__ . '/../src/Container')->exec('connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
} catch (\Throwable $ex) {

View File

@ -7,16 +7,22 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Grpc;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Swoole\Coroutine\run(function () {
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
$grpc = Grpc::new();
$host = '0.0.0.0';
$port = 9502;
@ -27,9 +33,6 @@ Swoole\Coroutine\run(function () {
'http_compression' => false,
]);
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
foreach ([SIGHUP, SIGINT, SIGTERM] as $signal) {
Swoole\Process::signal($signal, function () use ($server) {
Logger::instance()->info('Shutdown swoole coroutine server');

View File

@ -21,6 +21,7 @@
},
"require": {
"workerman/workerman": "^4.0",
"mix/init": "~3.0.0",
"mix/vega": "~3.0.0",
"mix/grpc": "~3.0.0",
"mix/cli": "~3.0.0",

View File

@ -14,15 +14,17 @@ class Config
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
/**
* @return \Noodlehaus\Config
*/
public static function instance(): \Noodlehaus\Config
{
if (!isset(self::$instance)) {
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
return self::$instance;
}
}
}

View File

@ -12,19 +12,21 @@ class DB
*/
static private $instance;
public static function connect(): void
{
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
/**
* @return Database
*/
public static function instance(): Database
{
if (!isset(self::$instance)) {
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
return self::$instance;
}

View File

@ -15,19 +15,21 @@ class Logger implements HandlerInterface
static private $instance;
public static function init(): void
{
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
/**
* @return \Monolog\Logger
*/
public static function instance(): \Monolog\Logger
{
if (!isset(self::$instance)) {
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
return self::$instance;
}

View File

@ -12,20 +12,22 @@ class RDS
*/
static private $instance;
public static function connect(): void
{
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
/**
* @return Redis
*/
public static function instance(): Redis
{
if (!isset(self::$instance)) {
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
return self::$instance;
}

View File

@ -6,13 +6,16 @@ ini_set('error_reporting', E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use Dotenv\Dotenv;
use Mix\Cli\Cli;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
Cli::setName('app')->setVersion('0.0.0-alpha');
$cmds = [

View File

@ -7,14 +7,17 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init');
/**
* 多进程默认开启了协程
@ -29,6 +32,7 @@ $http->on('Request', $vega->handler());
$http->on('WorkerStart', function ($server, $workerId) {
// swoole 协程不支持 set_exception_handler 需要手动捕获异常
try {
Finder::in(__DIR__ . '/../src/Container')->exec('connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
} catch (\Throwable $ex) {

View File

@ -7,25 +7,28 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Swoole\Coroutine\run(function () {
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
$vega = Vega::new();
$host = '0.0.0.0';
$port = 9502;
$server = new Swoole\Coroutine\Http\Server($host, $port, false, false);
$server->handle('/', $vega->handler());
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
foreach ([SIGHUP, SIGINT, SIGTERM] as $signal) {
Swoole\Process::signal($signal, function () use ($server) {
Logger::instance()->info('Shutdown swoole coroutine server');

View File

@ -7,18 +7,24 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init');
$vega = Vega::new();
$addr = 'http://0.0.0.0:2345';
$http = new Workerman\Worker($addr);
$http->onWorkerStart = function ($worker) {
Finder::in(__DIR__ . '/../src/Container')->exec('connect');
};
$http->onMessage = $vega->handler();
$http->count = 4;

View File

@ -21,6 +21,7 @@
},
"require": {
"workerman/workerman": "^4.0",
"mix/init": "~3.0.0",
"mix/vega": "~3.0.0",
"mix/cli": "~3.0.0",
"mix/database": "~3.0.0",

View File

@ -14,15 +14,17 @@ class Config
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
/**
* @return \Noodlehaus\Config
*/
public static function instance(): \Noodlehaus\Config
{
if (!isset(self::$instance)) {
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
return self::$instance;
}
}
}

View File

@ -12,19 +12,21 @@ class DB
*/
static private $instance;
public static function connect(): void
{
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
/**
* @return Database
*/
public static function instance(): Database
{
if (!isset(self::$instance)) {
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
return self::$instance;
}

View File

@ -15,19 +15,21 @@ class Logger implements HandlerInterface
static private $instance;
public static function init(): void
{
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
/**
* @return \Monolog\Logger
*/
public static function instance(): \Monolog\Logger
{
if (!isset(self::$instance)) {
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
return self::$instance;
}

View File

@ -12,20 +12,22 @@ class RDS
*/
static private $instance;
public static function connect(): void
{
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
/**
* @return Redis
*/
public static function instance(): Redis
{
if (!isset(self::$instance)) {
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
return self::$instance;
}

View File

@ -6,13 +6,16 @@ ini_set('error_reporting', E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use Dotenv\Dotenv;
use Mix\Cli\Cli;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
Cli::setName('app')->setVersion('0.0.0-alpha');
$cmds = [

View File

@ -7,26 +7,29 @@ ini_set('memory_limit', '1G');
require __DIR__ . '/../vendor/autoload.php';
use App\Error;
use App\Container\Logger;
use App\Container\Upgrader;
use App\Vega;
use Dotenv\Dotenv;
use Mix\Init\Finder;
Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load();
define("APP_DEBUG", env('APP_DEBUG'));
App\Error::register();
Error::register();
Swoole\Coroutine\run(function () {
Finder::in(__DIR__ . '/../src/Container')->exec('init', 'connect');
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
$vega = Vega::new();
$host = '0.0.0.0';
$port = 9502;
$server = new Swoole\Coroutine\Http\Server($host, $port, false, false);
$server->handle('/', $vega->handler());
App\Container\DB::enableCoroutine();
App\Container\RDS::enableCoroutine();
foreach ([SIGHUP, SIGINT, SIGTERM] as $signal) {
Swoole\Process::signal($signal, function () use ($server) {
Logger::instance()->info('Shutdown swoole coroutine server');

View File

@ -17,6 +17,7 @@
]
},
"require": {
"mix/init": "~3.0.0",
"mix/vega": "~3.0.0",
"mix/websocket": "~3.0.0",
"mix/cli": "~3.0.0",

View File

@ -14,14 +14,16 @@ class Config
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
/**
* @return \Noodlehaus\Config
*/
public static function instance(): \Noodlehaus\Config
{
if (!isset(self::$instance)) {
self::$instance = new \Noodlehaus\Config(__DIR__ . '/../../conf');
}
return self::$instance;
}

View File

@ -12,19 +12,21 @@ class DB
*/
static private $instance;
public static function connect(): void
{
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
/**
* @return Database
*/
public static function instance(): Database
{
if (!isset(self::$instance)) {
$dsn = $_ENV['DATABASE_DSN'];
$username = $_ENV['DATABASE_USERNAME'];
$password = $_ENV['DATABASE_PASSWORD'];
$db = new Database($dsn, $username, $password);
APP_DEBUG and $db->setLogger(new DBLogger());
self::$instance = $db;
}
return self::$instance;
}

View File

@ -15,19 +15,21 @@ class Logger implements HandlerInterface
static private $instance;
public static function init(): void
{
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
/**
* @return \Monolog\Logger
*/
public static function instance(): \Monolog\Logger
{
if (!isset(self::$instance)) {
$logger = new \Monolog\Logger('MIX');
$rotatingFileHandler = new RotatingFileHandler(__DIR__ . '/../../runtime/logs/mix.log', 7);
$rotatingFileHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", 'Y-m-d H:i:s.u'));
$logger->pushHandler($rotatingFileHandler);
$logger->pushHandler(new Logger());
self::$instance = $logger;
}
return self::$instance;
}

View File

@ -12,20 +12,22 @@ class RDS
*/
static private $instance;
public static function connect(): void
{
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
/**
* @return Redis
*/
public static function instance(): Redis
{
if (!isset(self::$instance)) {
$host = $_ENV['REDIS_HOST'];
$port = $_ENV['REDIS_PORT'];
$password = $_ENV['REDIS_PASSWORD'];
$database = $_ENV['REDIS_DATABASE'];
$rds = new Redis($host, $port, $password, $database);
APP_DEBUG and $rds->setLogger(new RDSLogger());
self::$instance = $rds;
}
return self::$instance;
}

View File

@ -14,14 +14,16 @@ class Upgrader
*/
static private $instance;
public static function init(): void
{
self::$instance = new \Mix\WebSocket\Upgrader();
}
/**
* @return \Mix\WebSocket\Upgrader
*/
public static function instance(): \Mix\WebSocket\Upgrader
{
if (!isset(self::$instance)) {
self::$instance = new \Mix\WebSocket\Upgrader();
}
return self::$instance;
}