Skip to content

Commit 5383071

Browse files
committed
Initial commit
0 parents  commit 5383071

16 files changed

Lines changed: 545 additions & 0 deletions

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2

.github/workflows/fix-style.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: fix-style
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
php-code-styling:
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 5
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
with:
20+
ref: ${{ github.head_ref }}
21+
22+
- name: Fix PHP code style issues
23+
uses: aglipanci/laravel-pint-action@2.5
24+
25+
- name: Commit changes
26+
uses: stefanzweifel/git-auto-commit-action@v5
27+
with:
28+
commit_message: Fix styling

.github/workflows/run-tests.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
test:
8+
runs-on: ${{ matrix.os }}
9+
services:
10+
opensearch:
11+
image: opensearchproject/opensearch:${{ matrix.opensearch }}
12+
env:
13+
discovery.type: single-node
14+
DISABLE_SECURITY_PLUGIN: true
15+
OPENSEARCH_JAVA_OPTS: -Xms512m -Xmx512m
16+
ports:
17+
- 9200:9200
18+
19+
strategy:
20+
fail-fast: true
21+
matrix:
22+
os: [ubuntu-latest]
23+
opensearch: [2.19.3, 3.7.0]
24+
php: [8.4, 8.3, 8.2]
25+
include:
26+
- php: 8.4
27+
laravel: 13.*
28+
testbench: 11.*
29+
30+
- php: 8.3
31+
laravel: 12.*
32+
testbench: 10.*
33+
34+
- php: 8.2
35+
laravel: 11.*
36+
testbench: 9.*
37+
38+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - OS${{ matrix.opensearch }} - ${{ matrix.os }}
39+
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v4
43+
44+
- name: Setup PHP
45+
uses: shivammathur/setup-php@v2
46+
with:
47+
php-version: ${{ matrix.php }}
48+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
49+
coverage: none
50+
51+
- name: Install dependencies
52+
run: |
53+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update --no-blocking
54+
composer update --prefer-dist --no-interaction --no-blocking
55+
56+
- name: Wait for OpenSearch
57+
run: |
58+
for attempt in {1..60}; do
59+
if curl --fail --silent http://127.0.0.1:9200 > /dev/null; then
60+
exit 0
61+
fi
62+
63+
sleep 1
64+
done
65+
66+
echo "OpenSearch did not become available in time."
67+
docker logs "${{ job.services.opensearch.id }}"
68+
exit 1
69+
70+
- name: Execute tests
71+
run: vendor/bin/pest tests/Integration tests/Unit --ci
72+
env:
73+
OPENSEARCH_HOST: http://127.0.0.1:9200

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.idea
2+
/.vscode
3+
/bin
4+
/vendor
5+
/composer.lock
6+
/phpunit.xml
7+
/.phpunit.cache
8+
/.phpunit.result.cache
9+
/.php_cs
10+
/.php_cs.cache
11+
/.php-cs-fixer.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Steve Bauman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# OpenSearch Client
2+
3+
A Laravel integration for the official [OpenSearch PHP client](https://github.com/opensearch-project/opensearch-php).
4+
5+
## Installation
6+
7+
Install the package with Composer:
8+
9+
```bash
10+
composer require directorytree/opensearch-client
11+
```
12+
13+
## Configuration
14+
15+
Publish the configuration file:
16+
17+
```bash
18+
php artisan vendor:publish --provider="DirectoryTree\OpenSearchClient\OpenSearchClientServiceProvider"
19+
```
20+
21+
The published `config/opensearch-client.php` file defines the default connection and any named OpenSearch connections:
22+
23+
```php
24+
return [
25+
'default' => env('OPENSEARCH_CONNECTION', 'default'),
26+
27+
'connections' => [
28+
'default' => [
29+
'base_uri' => env('OPENSEARCH_HOST', 'http://localhost:9200'),
30+
],
31+
],
32+
];
33+
```
34+
35+
Each connection is passed directly to `OpenSearch\GuzzleClientFactory`.
36+
37+
## Usage
38+
39+
Resolve `DirectoryTree\OpenSearchClient\OpenSearchManager` from the container to access OpenSearch clients:
40+
41+
```php
42+
namespace App\Console\Commands;
43+
44+
use DirectoryTree\OpenSearchClient\OpenSearchManager;
45+
use Illuminate\Console\Command;
46+
47+
class CreateIndex extends Command
48+
{
49+
protected $signature = 'create:index {name}';
50+
51+
protected $description = 'Creates an index';
52+
53+
public function handle(OpenSearchManager $opensearch): void
54+
{
55+
$client = $opensearch->default();
56+
57+
$client->indices()->create([
58+
'index' => $this->argument('name'),
59+
]);
60+
}
61+
}
62+
```
63+
64+
You can also resolve named connections:
65+
66+
```php
67+
$client = $opensearch->connection('write');
68+
```
69+
70+
You may also use the facade:
71+
72+
```php
73+
use DirectoryTree\OpenSearchClient\Facades\OpenSearch;
74+
75+
$client = OpenSearch::connection('write');
76+
```

composer.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "directorytree/opensearch-client",
3+
"description": "A Laravel integration for the official OpenSearch PHP client",
4+
"keywords": [
5+
"laravel",
6+
"opensearch",
7+
"client",
8+
"php"
9+
],
10+
"type": "library",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Steve Bauman",
15+
"email": "steven_bauman@outlook.com"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"DirectoryTree\\OpenSearchClient\\": "src"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"DirectoryTree\\OpenSearchClient\\Tests\\": "tests"
26+
}
27+
},
28+
"require": {
29+
"php": "^8.2",
30+
"guzzlehttp/guzzle": "^7.0",
31+
"illuminate/support": "^11.0|^12.0|^13.0",
32+
"opensearch-project/opensearch-php": "^2.6"
33+
},
34+
"require-dev": {
35+
"orchestra/testbench": "^9.0|^10.0|^11.0",
36+
"laravel/pint": "^1.0",
37+
"pestphp/pest": "^3.0"
38+
},
39+
"config": {
40+
"allow-plugins": {
41+
"php-http/discovery": true,
42+
"pestphp/pest-plugin": true
43+
}
44+
},
45+
"extra": {
46+
"laravel": {
47+
"providers": [
48+
"DirectoryTree\\OpenSearchClient\\OpenSearchClientServiceProvider"
49+
],
50+
"aliases": {
51+
"OpenSearch": "DirectoryTree\\OpenSearchClient\\Facades\\OpenSearch"
52+
}
53+
}
54+
}
55+
}

config/opensearch-client.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Default OpenSearch Connection
7+
|--------------------------------------------------------------------------
8+
|
9+
| This option controls the OpenSearch connection that will be returned by
10+
| default when no connection name is requested. You may define
11+
| additional connections below and switch between them as needed.
12+
|
13+
*/
14+
15+
'default' => env('OPENSEARCH_CONNECTION', 'default'),
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| OpenSearch Connections
20+
|--------------------------------------------------------------------------
21+
|
22+
| Here you may configure each OpenSearch connection for your application. The
23+
| connection options are passed directly to OpenSearch\GuzzleClientFactory,
24+
| so any supported Guzzle client configuration value may be added here.
25+
|
26+
*/
27+
28+
'connections' => [
29+
'default' => [
30+
'base_uri' => env('OPENSEARCH_HOST', 'http://localhost:9200'),
31+
],
32+
],
33+
];

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
6+
<testsuites>
7+
<testsuite name="unit">
8+
<directory suffix="Test.php">tests/Unit</directory>
9+
</testsuite>
10+
</testsuites>
11+
<source>
12+
<include>
13+
<directory suffix=".php">src</directory>
14+
</include>
15+
</source>
16+
</phpunit>

src/Facades/OpenSearch.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace DirectoryTree\OpenSearchClient\Facades;
4+
5+
use DirectoryTree\OpenSearchClient\OpenSearchManager;
6+
use Illuminate\Support\Facades\Facade;
7+
8+
/**
9+
* @method static \OpenSearch\Client connection(?string $name = null)
10+
* @method static \OpenSearch\Client default()
11+
* @method static \OpenSearch\Client driver(\UnitEnum|string|null $driver = null)
12+
*
13+
* @see OpenSearchManager
14+
*/
15+
class OpenSearch extends Facade
16+
{
17+
/**
18+
* Get the registered name of the component.
19+
*/
20+
protected static function getFacadeAccessor(): string
21+
{
22+
return OpenSearchManager::class;
23+
}
24+
}

0 commit comments

Comments
 (0)