Skip to content

Commit

Permalink
Namespace optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Oct 24, 2021
1 parent 4e220c9 commit a72ba06
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 230 deletions.
5 changes: 0 additions & 5 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,5 @@
*/

return [
support\bootstrap\Container::class,
support\bootstrap\Session::class,
support\bootstrap\db\Laravel::class,
support\bootstrap\Redis::class,
support\bootstrap\Log::class,
support\bootstrap\Translation::class,
];
7 changes: 2 additions & 5 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Webman\Middleware;
use Dotenv\Dotenv;
use support\Request;
use support\bootstrap\Log;
use support\bootstrap\Container;
use support\Log;
use support\Container;

ini_set('display_errors', 'on');
error_reporting(E_ALL);
Expand Down Expand Up @@ -130,9 +130,6 @@
Config::reload(config_path(), ['route']);

$bootstrap = $config['bootstrap'] ?? config('bootstrap', []);
if (!in_array(support\bootstrap\Log::class, $bootstrap)) {
$bootstrap[] = support\bootstrap\Log::class;
}
foreach ($bootstrap as $class_name) {
/** @var \Webman\Bootstrap $class_name */
$class_name::start($worker);
Expand Down
49 changes: 49 additions & 0 deletions support/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace support;

use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Psr16Cache;

/**
* Class Cache
* @package support\bootstrap
*
* Strings methods
* @method static mixed get($key, $default = null)
* @method static bool set($key, $value, $ttl = null)
* @method static bool delete($key)
* @method static bool clear()
* @method static iterable getMultiple($keys, $default = null)
* @method static bool setMultiple($values, $ttl = null)
* @method static bool deleteMultiple($keys)
* @method static bool has($key)
*/
class Cache
{
/**
* @var Psr16Cache
*/
public static $_instance = null;

/**
* @return Psr16Cache
*/
public static function instance()
{
if (!static::$_instance) {
$adapter = new RedisAdapter(Redis::connection()->client());
self::$_instance = new Psr16Cache($adapter);
}
return static::$_instance;
}

/**
* @param $name
* @param $arguments
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
return static::instance()->{$name}(... $arguments);
}
}
27 changes: 9 additions & 18 deletions support/bootstrap/Container.php → support/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\bootstrap;
namespace support;

use Workerman\Worker;
use Webman\Bootstrap;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -24,20 +22,22 @@
* @method static mixed make($name, array $parameters)
* @method static bool has($name)
*/
class Container implements Bootstrap
class Container
{
/**
* @var ContainerInterface
*/
protected static $_instance = null;

/**
* @param Worker $worker
* @return void
* @return ContainerInterface
*/
public static function start($worker)
public static function instance()
{
static::$_instance = include config_path() . '/container.php';
if (!static::$_instance) {
static::$_instance = include config_path() . '/container.php';
}
return static::$_instance;
}

/**
Expand All @@ -47,15 +47,6 @@ public static function start($worker)
*/
public static function __callStatic($name, $arguments)
{
return static::$_instance->{$name}(... $arguments);
}

/**
* instance
* @return
*/
public static function instance()
{
return static::$_instance;
return static::instance()->{$name}(... $arguments);
}
}
92 changes: 92 additions & 0 deletions support/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,104 @@
namespace support;

use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Jenssegers\Mongodb\Connection;
use Workerman\Timer;

/**
* Class Db
* @package support
*/
class Db extends Manager
{
/**
* @return void
*/
public static function setInstance()
{
$capsule = new Capsule;
$configs = config('database');
if (empty($configs)) {
return;
}

$capsule->getDatabaseManager()->extend('mongodb', function ($config, $name) {
$config['name'] = $name;
return new Connection($config);
});

$default_config = $configs['connections'][$configs['default']];
$capsule->addConnection($default_config);

foreach ($configs['connections'] as $name => $config) {
$capsule->addConnection($config, $name);
}

if (class_exists('\Illuminate\Events\Dispatcher')) {
$capsule->setEventDispatcher(new Dispatcher(new Container));
}

$capsule->setAsGlobal();

$capsule->bootEloquent();

// Heartbeat
$connections = config('database.connections');
if (!$connections) {
return;
}
Timer::add(55, function () use ($connections) {
foreach ($connections as $key => $item) {
if ($item['driver'] == 'mysql') {
Db::connection($key)->select('select 1');
}
}
});
}

/**
* Get a connection instance from the global manager.
*
* @param string|null $connection
* @return \Illuminate\Database\Connection
*/
public static function connection($connection = null)
{
if (!static::$instance) {
static::setInstance();
}
return static::$instance->getConnection($connection);
}

/**
* Get a fluent query builder instance.
*
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
* @param string|null $as
* @param string|null $connection
* @return \Illuminate\Database\Query\Builder
*/
public static function table($table, $as = null, $connection = null)
{
if (!static::$instance) {
static::setInstance();
}
return static::$instance->connection($connection)->table($table, $as);
}

/**
* Get a schema builder instance.
*
* @param string|null $connection
* @return \Illuminate\Database\Schema\Builder
*/
public static function schema($connection = null)
{
if (!static::$instance) {
static::setInstance();
}
return static::$instance->connection($connection)->getSchemaBuilder();
}
}
43 changes: 18 additions & 25 deletions support/bootstrap/Log.php → support/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\bootstrap;
namespace support;

use Webman\Bootstrap;
use Monolog\Logger;

/**
Expand All @@ -30,40 +29,34 @@
* @method static void alert($message, array $context = [])
* @method static void emergency($message, array $context = [])
*/
class Log implements Bootstrap {

class Log
{
/**
* @var array
*/
protected static $_instance = [];

/**
* @param \Workerman\Worker $worker
* @return void
* @param string $name
* @return Logger
*/
public static function start($worker)
public static function channel($name = 'default')
{
$configs = config('log', []);
foreach ($configs as $channel => $config) {
$logger = static::$_instance[$channel] = new Logger($channel);
foreach ($config['handlers'] as $handler_config) {
$handler = new $handler_config['class'](... \array_values($handler_config['constructor']));
if (isset($handler_config['formatter'])) {
$formatter = new $handler_config['formatter']['class'](... \array_values($handler_config['formatter']['constructor']));
$handler->setFormatter($formatter);
if (!static::$_instance) {
$configs = config('log', []);
foreach ($configs as $channel => $config) {
$logger = static::$_instance[$channel] = new Logger($channel);
foreach ($config['handlers'] as $handler_config) {
$handler = new $handler_config['class'](... \array_values($handler_config['constructor']));
if (isset($handler_config['formatter'])) {
$formatter = new $handler_config['formatter']['class'](... \array_values($handler_config['formatter']['constructor']));
$handler->setFormatter($formatter);
}
$logger->pushHandler($handler);
}
$logger->pushHandler($handler);
}
}
}

/**
* @param string $name
* @return Logger;
*/
public static function channel($name = 'default')
{
return static::$_instance[$name] ?? null;
return static::$_instance[$name];
}


Expand Down
25 changes: 12 additions & 13 deletions support/bootstrap/Redis.php → support/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\bootstrap;
namespace support;

use Webman\Bootstrap;
use Illuminate\Redis\RedisManager;

/**
Expand Down Expand Up @@ -198,32 +197,32 @@
* @method static mixed getPersistentID()
* @method static mixed getAuth()
*/
class Redis implements Bootstrap {
class Redis
{

/**
* @var RedisManager
*/
protected static $_manager = null;
protected static $_instance = null;

/**
* @param \Workerman\Worker $worker
* @return void
* @return RedisManager
*/
public static function start($worker)
public static function instance()
{
if (!class_exists('\Illuminate\Redis\RedisManager')) {
return;
if (!static::$_instance) {
$config = config('redis');
static::$_instance = new RedisManager('', 'phpredis', $config);
}
$config = config('redis');
static::$_manager = new RedisManager('', 'phpredis', $config);
return static::$_instance;
}

/**
* @param string $name
* @return \Illuminate\Redis\Connections\Connection
*/
public static function connection($name = 'default') {
return static::$_manager->connection($name);
return static::instance()->connection($name);
}

/**
Expand All @@ -233,6 +232,6 @@ public static function connection($name = 'default') {
*/
public static function __callStatic($name, $arguments)
{
return static::$_manager->connection('default')->{$name}(... $arguments);
return static::instance()->connection('default')->{$name}(... $arguments);
}
}
Loading

0 comments on commit a72ba06

Please sign in to comment.