diff --git a/config/bootstrap.php b/config/bootstrap.php index fb7cf28..95d2e87 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -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, ]; diff --git a/start.php b/start.php index 5e63f8c..1b43366 100644 --- a/start.php +++ b/start.php @@ -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); @@ -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); diff --git a/support/Cache.php b/support/Cache.php new file mode 100644 index 0000000..8d5898e --- /dev/null +++ b/support/Cache.php @@ -0,0 +1,49 @@ +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); + } +} diff --git a/support/bootstrap/Container.php b/support/Container.php similarity index 71% rename from support/bootstrap/Container.php rename to support/Container.php index 4af86d3..de5fd6c 100644 --- a/support/bootstrap/Container.php +++ b/support/Container.php @@ -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; /** @@ -24,7 +22,7 @@ * @method static mixed make($name, array $parameters) * @method static bool has($name) */ -class Container implements Bootstrap +class Container { /** * @var ContainerInterface @@ -32,12 +30,14 @@ class Container implements Bootstrap 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; } /** @@ -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); } } \ No newline at end of file diff --git a/support/Db.php b/support/Db.php index 8592832..1c6de3c 100644 --- a/support/Db.php +++ b/support/Db.php @@ -14,6 +14,11 @@ 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 @@ -21,5 +26,92 @@ */ 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(); + } } \ No newline at end of file diff --git a/support/bootstrap/Log.php b/support/Log.php similarity index 61% rename from support/bootstrap/Log.php rename to support/Log.php index 0bb0472..627c012 100644 --- a/support/bootstrap/Log.php +++ b/support/Log.php @@ -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; /** @@ -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]; } diff --git a/support/bootstrap/Redis.php b/support/Redis.php similarity index 94% rename from support/bootstrap/Redis.php rename to support/Redis.php index f420a82..7627a00 100644 --- a/support/bootstrap/Redis.php +++ b/support/Redis.php @@ -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; /** @@ -198,24 +197,24 @@ * @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; } /** @@ -223,7 +222,7 @@ public static function start($worker) * @return \Illuminate\Redis\Connections\Connection */ public static function connection($name = 'default') { - return static::$_manager->connection($name); + return static::instance()->connection($name); } /** @@ -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); } } diff --git a/support/Translation.php b/support/Translation.php new file mode 100644 index 0000000..3fdaffb --- /dev/null +++ b/support/Translation.php @@ -0,0 +1,86 @@ + + * @copyright walkor + * @link http://www.workerman.net/ + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +namespace support; + +use Symfony\Component\Translation\Translator; +use Symfony\Component\Translation\Loader\PhpFileLoader; +use Webman\Exception\NotFoundException; + +/** + * Class Translation + * @package support\bootstrap + * @method static string trans(?string $id, array $parameters = [], string $domain = null, string $locale = null) + * @method static void setLocale(string $locale) + * @method static string getLocale() + */ +class Translation +{ + + /** + * @var Translator + */ + protected static $_instance; + + /** + * @return Translator + * @throws NotFoundException + */ + public static function instance() + { + if (!static::$_instance) { + $config = config('translation', []); + if (!$translations_path = realpath($config['path'])) { + throw new NotFoundException("File {$config['path']} not found"); + } + + static::$_instance = $translator = new Translator($config['locale']); + $translator->setFallbackLocales($config['fallback_locale']); + + $classes = [ + 'Symfony\Component\Translation\Loader\PhpFileLoader' => [ + 'extension' => '.php', + 'format' => 'phpfile' + ], + 'Symfony\Component\Translation\Loader\PoFileLoader' => [ + 'extension' => '.po', + 'format' => 'pofile' + ] + ]; + + foreach ($classes as $class => $opts) + { + $translator->addLoader($opts['format'], new $class); + foreach (glob($translations_path . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '*' . $opts['extension']) as $file) { + $domain = basename($file, $opts['extension']); + $dir_name = pathinfo($file, PATHINFO_DIRNAME); + $locale = substr(strrchr($dir_name, DIRECTORY_SEPARATOR), 1); + if ($domain && $locale) { + $translator->addResource($opts['format'], $file, $locale, $domain); + } + } + } + } + return static::$_instance; + } + + /** + * @param $name + * @param $arguments + * @return mixed + */ + public static function __callStatic($name, $arguments) + { + return static::instance()->{$name}(... $arguments); + } +} diff --git a/support/bootstrap/Translation.php b/support/bootstrap/Translation.php deleted file mode 100644 index be8ece8..0000000 --- a/support/bootstrap/Translation.php +++ /dev/null @@ -1,85 +0,0 @@ - - * @copyright walkor - * @link http://www.workerman.net/ - * @license http://www.opensource.org/licenses/mit-license.php MIT License - */ -namespace support\bootstrap; - -use Webman\Bootstrap; -use Symfony\Component\Translation\Translator; -use Symfony\Component\Translation\Loader\PhpFileLoader; - -/** - * Class Translation - * @package support\bootstrap - * @method static string trans(?string $id, array $parameters = [], string $domain = null, string $locale = null) - * @method static void setLocale(string $locale) - * @method static string getLocale() - */ -class Translation implements Bootstrap { - - /** - * @var array - */ - protected static $_translator = []; - - /** - * @param \Workerman\Worker $worker - * @return void - */ - public static function start($worker) - { - if (!class_exists('\Symfony\Component\Translation\Translator')) { - return; - } - $config = config('translation', []); - if (!$translations_path = realpath($config['path'])) { - return; - } - - static::$_translator = $translator = new Translator($config['locale']); - $translator->setFallbackLocales($config['fallback_locale']); - - $classes = [ - 'Symfony\Component\Translation\Loader\PhpFileLoader' => [ - 'extension' => '.php', - 'format' => 'phpfile' - ], - 'Symfony\Component\Translation\Loader\PoFileLoader' => [ - 'extension' => '.po', - 'format' => 'pofile' - ] - ]; - - foreach ($classes as $class => $opts) - { - $translator->addLoader($opts['format'], new $class); - foreach (glob($translations_path . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '*' . $opts['extension']) as $file) { - $domain = basename($file, $opts['extension']); - $dir_name = pathinfo($file, PATHINFO_DIRNAME); - $locale = substr(strrchr($dir_name, DIRECTORY_SEPARATOR), 1); - if ($domain && $locale) { - $translator->addResource($opts['format'], $file, $locale, $domain); - } - } - } - } - - /** - * @param $name - * @param $arguments - * @return mixed - */ - public static function __callStatic($name, $arguments) - { - return static::$_translator->{$name}(... $arguments); - } -} diff --git a/support/bootstrap/db/Laravel.php b/support/bootstrap/db/Laravel.php deleted file mode 100644 index 7cc5923..0000000 --- a/support/bootstrap/db/Laravel.php +++ /dev/null @@ -1,77 +0,0 @@ - - * @copyright walkor - * @link http://www.workerman.net/ - * @license http://www.opensource.org/licenses/mit-license.php MIT License - */ -namespace support\bootstrap\db; - -use Webman\Bootstrap; -use Illuminate\Database\Capsule\Manager as Capsule; -use Illuminate\Events\Dispatcher; -use Illuminate\Container\Container; -use Jenssegers\Mongodb\Connection; -use Workerman\Timer; -use Workerman\Worker; -use support\Db; - -/** - * Class Laravel - * @package support\bootstrap\db - */ -class Laravel implements Bootstrap -{ - /** - * @param Worker $worker - * - * @return void - */ - public static function start($worker) - { - if (!class_exists('\Illuminate\Database\Capsule\Manager')) { - return; - } - $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(); - - $connections = config('database.connections'); - if (!$connections) { - return; - } - Timer::add(55, function () use ($connections){ - foreach ($connections as $key => $item) { - Db::connection($key)->select('select 1 limit 1'); - } - }); - } -} diff --git a/support/helpers.php b/support/helpers.php index ccf8557..26aa172 100644 --- a/support/helpers.php +++ b/support/helpers.php @@ -14,11 +14,10 @@ use support\Request; use support\Response; -use support\bootstrap\Translation; +use support\Translation; use Webman\App; use Webman\Config; use Webman\Route; -use Webman\Exception\ClassNotFoundException; define('BASE_PATH', realpath(__DIR__ . '/../'));