Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <?php namespace Np\Api\Middleware;
  2.  
  3. use Closure;
  4. use Illuminate\Foundation\Application;
  5. use October\Rain\Support\Facades\Flash;
  6. use Illuminate\Support\Facades\DB;
  7. use Np\Structure\Models\Site;
  8. use Np\Structure\Models\Domain;
  9. use Np\Structure\Models\Cluster;
  10. use Np\Structure\Classes\DatabaseConnection;
  11.  
  12. class InitializedTenantForApi
  13. {
  14. /**
  15. * The Laravel Application
  16. *
  17. * @var Application
  18. */
  19. protected $app;
  20.  
  21. /**
  22. * Create a new middleware instance.
  23. *
  24. * @param Application $app
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. //$this->app = $app;
  30. }
  31. /**
  32. * Handle an incoming request.
  33. *
  34. * @param \Illuminate\Http\Request $request
  35. * @param \Closure $next
  36. * @return mixed
  37. */
  38.  
  39. public function handle($request, Closure $next)
  40. {
  41. $host = $request->input('host');
  42. if (!isset($host) || is_null($host)) {
  43. return response(['error' => 'host parameter is missing.'], 400);
  44. }
  45. $this->initDB($host);
  46. return $next($request);
  47. }
  48.  
  49. public function initDB($host)
  50. {
  51.  
  52. // find domain
  53. $domain = Domain::where('fqdn', $host)->first();
  54.  
  55. if(is_null($domain) || !isset($domain))
  56. return response(['error' => 'host not found.']);
  57.  
  58. $db = $domain->site->db;
  59. $cluster = $domain->site->cluster;
  60. DatabaseConnection::setConnection($cluster, $db);
  61.  
  62. echo '<pre>';
  63. print_r(DB::connection()->getDatabaseName());
  64. echo '</pre>';
  65. exit;
  66. // $key = 'database.connections.tenant';
  67. // $db = config($key);
  68. // $db['host'] = $domain->site->cluster->host;
  69. // $db['username'] = $domain->site->cluster->username;
  70. // $db['password'] = $domain->site->cluster->password;
  71. // $db['database'] = $domain->site->db->id;
  72. // config(
  73. // [$key => $db]
  74. // );
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement