33namespace PDPhilip \Elasticsearch ;
44
55use PDPhilip \Elasticsearch \DSL \Bridge ;
6- use Elasticsearch \ClientBuilder ;
6+ use Elastic \ Elasticsearch \ClientBuilder ;
77use Illuminate \Database \Connection as BaseConnection ;
88use Illuminate \Support \Arr ;
99use Illuminate \Support \Str ;
1414
1515class Connection extends BaseConnection
1616{
17-
17+
1818 protected $ client ;
1919 protected $ index ;
2020 protected $ maxSize ;
2121 protected $ indexPrefix ;
22-
23-
22+
23+
2424 public function __construct (array $ config )
2525 {
2626 $ this ->config = $ config ;
27-
27+
2828 if (!empty ($ config ['index_prefix ' ])) {
2929 $ this ->indexPrefix = $ config ['index_prefix ' ];
3030 }
31-
31+
3232 $ this ->client = $ this ->buildConnection ();
33-
33+
3434 $ this ->useDefaultPostProcessor ();
35-
35+
3636 $ this ->useDefaultSchemaGrammar ();
37-
37+
3838 $ this ->useDefaultQueryGrammar ();
39-
39+
4040 }
41-
41+
4242 public function getIndexPrefix ()
4343 {
4444 return $ this ->indexPrefix ;
4545 }
46-
47-
46+
47+
4848 public function getTablePrefix ()
4949 {
5050 return $ this ->getIndexPrefix ();
5151 }
52-
52+
5353 public function setIndex ($ index )
5454 {
5555 $ this ->index = $ index ;
@@ -58,99 +58,99 @@ public function setIndex($index)
5858 $ this ->index = $ this ->indexPrefix .'_ ' .$ index ;
5959 }
6060 }
61-
61+
6262 return $ this ->getIndex ();
6363 }
64-
64+
6565 public function getSchemaGrammar ()
6666 {
6767 return new Schema \Grammar ($ this );
6868 }
69-
69+
7070 public function getIndex ()
7171 {
7272 return $ this ->index ;
7373 }
74-
74+
7575 public function setMaxSize ($ value )
7676 {
7777 $ this ->maxSize = $ value ;
7878 }
79-
79+
8080 public function table ($ table , $ as = null )
8181 {
8282 return $ this ->setIndex ($ table );
8383 }
84-
85-
84+
85+
8686 /**
8787 * @inheritdoc
8888 */
8989 public function getSchemaBuilder ()
9090 {
9191 return new Schema \Builder ($ this );
9292 }
93-
94-
93+
94+
9595 /**
9696 * @inheritdoc
9797 */
9898 public function disconnect ()
9999 {
100100 unset($ this ->connection );
101101 }
102-
103-
102+
103+
104104 /**
105105 * @inheritdoc
106106 */
107107 public function getDriverName ()
108108 {
109109 return 'elasticsearch ' ;
110110 }
111-
111+
112112 /**
113113 * @inheritdoc
114114 */
115115 protected function getDefaultPostProcessor ()
116116 {
117117 return new Query \Processor ();
118118 }
119-
119+
120120 /**
121121 * @inheritdoc
122122 */
123123 protected function getDefaultQueryGrammar ()
124124 {
125125 return new Query \Grammar ();
126126 }
127-
127+
128128 /**
129129 * @inheritdoc
130130 */
131131 protected function getDefaultSchemaGrammar ()
132132 {
133133 return new Schema \Grammar ();
134134 }
135-
136-
135+
136+
137137 //----------------------------------------------------------------------
138138 // Connection Builder
139139 //----------------------------------------------------------------------
140-
140+
141141 protected function buildConnection ()
142142 {
143143 $ type = config ('database.connections.elasticsearch.auth_type ' ) ?? null ;
144144 $ type = strtolower ($ type );
145- if (!in_array ($ type , ['http ' , 'cloud ' , ' api ' ])) {
145+ if (!in_array ($ type , ['https ' , 'cloud ' ,])) {
146146 throw new RuntimeException ('Invalid [auth_type] in database config. Must be: http, cloud or api ' );
147147 }
148-
148+
149149 return $ this ->{'_ ' .$ type .'Connection ' }();
150-
150+
151151 }
152-
153- protected function _httpConnection ()
152+
153+ protected function _httpsConnection ()
154154 {
155155 $ hosts = config ('database.connections.elasticsearch.hosts ' ) ?? null ;
156156 $ username = config ('database.connections.elasticsearch.username ' ) ?? null ;
@@ -161,12 +161,12 @@ protected function _httpConnection()
161161 $ cb ->setBasicAuthentication ($ username , $ pass )->build ();
162162 }
163163 if ($ certPath ) {
164- $ cb ->setSSLVerification ($ certPath );
164+ $ cb ->setCABundle ($ certPath );
165165 }
166-
166+
167167 return $ cb ->build ();
168168 }
169-
169+
170170 protected function _cloudConnection ()
171171 {
172172 $ cloudId = config ('database.connections.elasticsearch.cloud_id ' ) ?? null ;
@@ -177,40 +177,26 @@ protected function _cloudConnection()
177177 $ certPath = config ('database.connections.elasticsearch.ssl_cert ' ) ?? null ;
178178 $ cb = ClientBuilder::create ()->setElasticCloudId ($ cloudId );
179179 if ($ apiId && $ apiKey ) {
180- $ cb ->setApiKey ($ apiId , $ apiKey )->build ();
180+ $ cb ->setApiKey ($ apiKey , $ apiId )->build ();
181181 } elseif ($ username && $ pass ) {
182182 $ cb ->setBasicAuthentication ($ username , $ pass )->build ();
183183 }
184184 if ($ certPath ) {
185185 $ cb ->setSSLVerification ($ certPath );
186186 }
187-
188- return $ cb ->build ();
189- }
190-
191-
192- protected function _apiConnection ()
193- {
194- $ apiId = config ('database.connections.elasticsearch.api_id ' ) ?? null ;
195- $ apiKey = config ('database.connections.elasticsearch.api_key ' ) ?? null ;
196- $ certPath = config ('database.connections.elasticsearch.ssl_cert ' ) ?? null ;
197- $ cb = ClientBuilder::create ()->setApiKey ($ apiId , $ apiKey );
198- if ($ certPath ) {
199- $ cb ->setSSLVerification ($ certPath );
200- }
201-
187+
202188 return $ cb ->build ();
203189 }
204-
205-
190+
191+
206192 //----------------------------------------------------------------------
207193 // Dynamic call routing to DSL bridge
208194 //----------------------------------------------------------------------
209-
195+
210196 public function __call ($ method , $ parameters )
211197 {
212198 $ bridge = new Bridge ($ this ->client , $ this ->index , $ this ->maxSize );
213-
199+
214200 return $ bridge ->{'process ' .Str::studly ($ method )}(...$ parameters );
215201 }
216202}
0 commit comments