88use Illuminate \Support \Arr ;
99use Illuminate \Support \Str ;
1010use InvalidArgumentException ;
11- use phpDocumentor \Reflection \Types \Scalar ;
1211use RuntimeException ;
1312
1413
1514class Connection extends BaseConnection
1615{
17-
16+
1817 protected $ client ;
1918 protected $ index ;
2019 protected $ maxSize ;
2120 protected $ indexPrefix ;
22-
23-
21+
22+
2423 public function __construct (array $ config )
2524 {
2625 $ this ->config = $ config ;
27-
26+
2827 if (!empty ($ config ['index_prefix ' ])) {
2928 $ this ->indexPrefix = $ config ['index_prefix ' ];
3029 }
31-
30+
3231 $ this ->client = $ this ->buildConnection ();
33-
32+
3433 $ this ->useDefaultPostProcessor ();
35-
34+
3635 $ this ->useDefaultSchemaGrammar ();
37-
36+
3837 $ this ->useDefaultQueryGrammar ();
39-
38+
4039 }
41-
40+
4241 public function getIndexPrefix ()
4342 {
4443 return $ this ->indexPrefix ;
4544 }
46-
47-
45+
46+
4847 public function getTablePrefix ()
4948 {
5049 return $ this ->getIndexPrefix ();
5150 }
52-
51+
5352 public function setIndex ($ index )
5453 {
5554 $ this ->index = $ index ;
@@ -58,98 +57,98 @@ public function setIndex($index)
5857 $ this ->index = $ this ->indexPrefix .'_ ' .$ index ;
5958 }
6059 }
61-
60+
6261 return $ this ->getIndex ();
6362 }
64-
63+
6564 public function getSchemaGrammar ()
6665 {
6766 return new Schema \Grammar ($ this );
6867 }
69-
68+
7069 public function getIndex ()
7170 {
7271 return $ this ->index ;
7372 }
74-
73+
7574 public function setMaxSize ($ value )
7675 {
7776 $ this ->maxSize = $ value ;
7877 }
79-
78+
8079 public function table ($ table , $ as = null )
8180 {
8281 return $ this ->setIndex ($ table );
8382 }
84-
85-
83+
84+
8685 /**
8786 * @inheritdoc
8887 */
8988 public function getSchemaBuilder ()
9089 {
9190 return new Schema \Builder ($ this );
9291 }
93-
94-
92+
93+
9594 /**
9695 * @inheritdoc
9796 */
9897 public function disconnect ()
9998 {
10099 unset($ this ->connection );
101100 }
102-
103-
101+
102+
104103 /**
105104 * @inheritdoc
106105 */
107106 public function getDriverName ()
108107 {
109108 return 'elasticsearch ' ;
110109 }
111-
110+
112111 /**
113112 * @inheritdoc
114113 */
115114 protected function getDefaultPostProcessor ()
116115 {
117116 return new Query \Processor ();
118117 }
119-
118+
120119 /**
121120 * @inheritdoc
122121 */
123122 protected function getDefaultQueryGrammar ()
124123 {
125124 return new Query \Grammar ();
126125 }
127-
126+
128127 /**
129128 * @inheritdoc
130129 */
131130 protected function getDefaultSchemaGrammar ()
132131 {
133132 return new Schema \Grammar ();
134133 }
135-
136-
134+
135+
137136 //----------------------------------------------------------------------
138137 // Connection Builder
139138 //----------------------------------------------------------------------
140-
139+
141140 protected function buildConnection ()
142141 {
143142 $ type = config ('database.connections.elasticsearch.auth_type ' ) ?? null ;
144143 $ type = strtolower ($ type );
145144 if (!in_array ($ type , ['http ' , 'cloud ' , 'api ' ])) {
146145 throw new RuntimeException ('Invalid [auth_type] in database config. Must be: http, cloud or api ' );
147146 }
148-
147+
149148 return $ this ->{'_ ' .$ type .'Connection ' }();
150-
149+
151150 }
152-
151+
153152 protected function _httpConnection ()
154153 {
155154 $ hosts = config ('database.connections.elasticsearch.hosts ' ) ?? null ;
@@ -163,10 +162,10 @@ protected function _httpConnection()
163162 if ($ certPath ) {
164163 $ cb ->setSSLVerification ($ certPath );
165164 }
166-
165+
167166 return $ cb ->build ();
168167 }
169-
168+
170169 protected function _cloudConnection ()
171170 {
172171 $ cloudId = config ('database.connections.elasticsearch.cloud_id ' ) ?? null ;
@@ -184,11 +183,11 @@ protected function _cloudConnection()
184183 if ($ certPath ) {
185184 $ cb ->setSSLVerification ($ certPath );
186185 }
187-
186+
188187 return $ cb ->build ();
189188 }
190-
191-
189+
190+
192191 protected function _apiConnection ()
193192 {
194193 $ apiId = config ('database.connections.elasticsearch.api_id ' ) ?? null ;
@@ -198,19 +197,19 @@ protected function _apiConnection()
198197 if ($ certPath ) {
199198 $ cb ->setSSLVerification ($ certPath );
200199 }
201-
200+
202201 return $ cb ->build ();
203202 }
204-
205-
203+
204+
206205 //----------------------------------------------------------------------
207206 // Dynamic call routing to DSL bridge
208207 //----------------------------------------------------------------------
209-
208+
210209 public function __call ($ method , $ parameters )
211210 {
212211 $ bridge = new Bridge ($ this ->client , $ this ->index , $ this ->maxSize );
213-
212+
214213 return $ bridge ->{'process ' .Str::studly ($ method )}(...$ parameters );
215214 }
216215}
0 commit comments