|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of webman. |
| 4 | + * |
| 5 | + * Licensed under The MIT License |
| 6 | + * For full copyright and license information, please see the MIT-LICENSE.txt |
| 7 | + * Redistributions of files must retain the above copyright notice. |
| 8 | + * |
| 9 | + * @author walkor<[email protected]> |
| 10 | + * @copyright walkor<[email protected]> |
| 11 | + * @link http://www.workerman.net/ |
| 12 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Webman\Database; |
| 16 | + |
| 17 | +use Illuminate\Container\Container as IlluminateContainer; |
| 18 | +use Webman\Database\Manager as Capsule; |
| 19 | +use Illuminate\Events\Dispatcher; |
| 20 | +use Illuminate\Pagination\Paginator; |
| 21 | +use Illuminate\Pagination\CursorPaginator; |
| 22 | +use Illuminate\Pagination\Cursor; |
| 23 | +use Jenssegers\Mongodb\Connection as JenssegersMongodbConnection; |
| 24 | +use MongoDB\Laravel\Connection as LaravelMongodbConnection; |
| 25 | +use support\Container; |
| 26 | + |
| 27 | +class Initializer |
| 28 | +{ |
| 29 | + |
| 30 | + /** |
| 31 | + * @param $config |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public static function init($config): void |
| 35 | + {; |
| 36 | + $connections = $config['connections'] ?? []; |
| 37 | + if (!$connections) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $capsule = new Capsule(IlluminateContainer::getInstance()); |
| 42 | + |
| 43 | + $capsule->getDatabaseManager()->extend('mongodb', function ($config, $name) { |
| 44 | + $config['name'] = $name; |
| 45 | + return class_exists(LaravelMongodbConnection::class) ? new LaravelMongodbConnection($config) : new JenssegersMongodbConnection($config); |
| 46 | + }); |
| 47 | + |
| 48 | + $default = $config['default'] ?? false; |
| 49 | + if ($default) { |
| 50 | + $defaultConfig = $connections[$default] ?? false; |
| 51 | + if ($defaultConfig) { |
| 52 | + $capsule->addConnection($defaultConfig, $default); |
| 53 | + $capsule->getDatabaseManager()->setDefaultConnection($default); |
| 54 | + unset($connections[$default]); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + foreach ($connections as $name => $config) { |
| 59 | + $capsule->addConnection($config, $name); |
| 60 | + } |
| 61 | + |
| 62 | + if (class_exists(Dispatcher::class) && !$capsule->getEventDispatcher()) { |
| 63 | + $capsule->setEventDispatcher(Container::make(Dispatcher::class, [IlluminateContainer::getInstance()])); |
| 64 | + } |
| 65 | + |
| 66 | + $capsule->setAsGlobal(); |
| 67 | + |
| 68 | + $capsule->bootEloquent(); |
| 69 | + |
| 70 | + // Paginator |
| 71 | + if (class_exists(Paginator::class)) { |
| 72 | + if (method_exists(Paginator::class, 'queryStringResolver')) { |
| 73 | + Paginator::queryStringResolver(function () { |
| 74 | + $request = request(); |
| 75 | + return $request?->queryString(); |
| 76 | + }); |
| 77 | + } |
| 78 | + Paginator::currentPathResolver(function () { |
| 79 | + $request = request(); |
| 80 | + return $request ? $request->path(): '/'; |
| 81 | + }); |
| 82 | + Paginator::currentPageResolver(function ($pageName = 'page') { |
| 83 | + $request = request(); |
| 84 | + if (!$request) { |
| 85 | + return 1; |
| 86 | + } |
| 87 | + $page = (int)($request->input($pageName, 1)); |
| 88 | + return $page > 0 ? $page : 1; |
| 89 | + }); |
| 90 | + if (class_exists(CursorPaginator::class)) { |
| 91 | + CursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') { |
| 92 | + return Cursor::fromEncoded(request()->input($cursorName)); |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments