Skip to content

Commit 381374b

Browse files
committed
First commit
0 parents  commit 381374b

13 files changed

+887
-0
lines changed

.gitattributes

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
* text=auto
2+
3+
*.blade.php diff=html
4+
*.css diff=css
5+
*.html diff=html
6+
*.md diff=markdown
7+
*.php diff=php
8+
9+
/.github export-ignore
10+
/art export-ignore
11+
/tests export-ignore
12+
.editorconfig export-ignore
13+
.gitattributes export-ignore
14+
.gitignore export-ignore
15+
.styleci.yml export-ignore
16+
CHANGELOG.md export-ignore
17+
phpunit.xml.dist export-ignore
18+
RELEASE.md export-ignore
19+
UPGRADE.md export-ignore

.github/workflows/tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: run-tests
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
php: [7.4, 8.0, 8.1]
12+
laravel: [6.*, 7.*, 8.*]
13+
include:
14+
- laravel: 7.*
15+
testbench: 5.*
16+
- laravel: 8.*
17+
testbench: 6.*
18+
19+
name: P${{ matrix.php }} - L${{ matrix.laravel }}
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v1
24+
25+
- name: Cache dependencies
26+
uses: actions/cache@v1
27+
with:
28+
path: ~/.composer/cache/files
29+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
30+
31+
- name: Setup PHP
32+
uses: shivammathur/setup-php@v1
33+
with:
34+
php-version: ${{ matrix.php }}
35+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
36+
coverage: none
37+
38+
- name: Install dependencies
39+
run: |
40+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
41+
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
42+
43+
- name: Execute tests
44+
run: vendor/bin/phpunit

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
/.env
3+
composer.lock
4+
/phpunit.xml
5+
.phpunit.result.cache

.styleci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
preset: laravel
2+
3+
disabled:
4+
- single_class_element_per_statement

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `scout-elastic-app-search-driver` will be documented in this file
4+
5+
## 1.0.0 - 2022-02-05
6+
7+
- Initial Release

CONTRIBUTING.md

Whitespace-only changes.

composer.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "chrysanthos/scout-elastic-app-search-driver",
3+
"description": "Laravel Scout Adapter for Elastic Enterprise App Search",
4+
"keywords": [
5+
"chrysanthos",
6+
"app-search",
7+
"laravel",
8+
"laravel scout",
9+
"scout-elastic-app-search-driver"
10+
],
11+
"homepage": "https://github.com/chrysanthos/scout-elastic-app-search-driver",
12+
"license": "MIT",
13+
"type": "library",
14+
"authors": [
15+
{
16+
"name": "Chrysanthos Prodromou",
17+
"email": "me@chrysanthos.xyz",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"php": "^7.4|^8.0|^8.1",
23+
"elastic/enterprise-search": "^7.1",
24+
"laravel/scout": "^9.0"
25+
},
26+
"require-dev": {
27+
"orchestra/testbench": "^6.17",
28+
"phpunit/phpunit": "^9.3"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Chrysanthos\\ScoutElasticAppSearch\\": "src"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Chrysanthos\\ScoutElasticAppSearch\\Tests\\": "tests"
38+
}
39+
},
40+
"scripts": {
41+
"test": "vendor/bin/phpunit",
42+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
43+
"post-autoload-dump": [
44+
"@php ./vendor/bin/testbench package:discover --ansi"
45+
]
46+
},
47+
"config": {
48+
"sort-packages": true
49+
},
50+
"extra": {
51+
"laravel": {
52+
"providers": [
53+
"Chrysanthos\\ScoutElasticAppSearch\\ScoutElasticAppSearchServiceProvider"
54+
],
55+
"aliases": {
56+
"ScoutElasticAppSearch": "Chrysanthos\\ScoutElasticAppSearch\\ScoutElasticAppSearchFacade"
57+
}
58+
}
59+
}
60+
}

config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'endpoint' => env('SCOUT_ELASTIC_APP_SEARCH_ENDPOINT'),
5+
'search_token' => env('SCOUT_ELASTIC_APP_SEARCH_TOKEN'),
6+
'private_token' => env('SCOUT_ELASTIC_APP_PRIVATE_TOKEN'),
7+
];

src/ElasticAppProxy.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Chrysanthos\ScoutElasticAppSearch;
4+
5+
use Elastic\EnterpriseSearch\AppSearch\Request\CreateEngine;
6+
use Elastic\EnterpriseSearch\AppSearch\Request\DeleteDocuments;
7+
use Elastic\EnterpriseSearch\AppSearch\Request\GetEngine;
8+
use Elastic\EnterpriseSearch\AppSearch\Request\IndexDocuments;
9+
use Elastic\EnterpriseSearch\AppSearch\Request\Search;
10+
use Elastic\EnterpriseSearch\AppSearch\Schema\Engine;
11+
use Elastic\EnterpriseSearch\AppSearch\Schema\SearchRequestParams;
12+
use Elastic\EnterpriseSearch\Client;
13+
14+
class ElasticAppProxy
15+
{
16+
/**
17+
* @var Client
18+
*/
19+
protected $client;
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $engine;
25+
26+
public function __construct(Client $client)
27+
{
28+
$this->client = $client->appSearch();
29+
}
30+
31+
public function setEngine($name): self
32+
{
33+
$this->engine = str_replace('_', '-', $name);
34+
35+
return $this;
36+
}
37+
38+
public function getClient()
39+
{
40+
return $this->client;
41+
}
42+
43+
public function getEngine()
44+
{
45+
$this->client->getEngine(new GetEngine($this->engine));
46+
}
47+
48+
public function indexDocuments($objects)
49+
{
50+
$this->client->indexDocuments(new IndexDocuments($this->engine, $objects));
51+
}
52+
53+
public function deleteDocuments($objects)
54+
{
55+
$this->client->deleteDocuments(new DeleteDocuments($this->engine, $objects));
56+
}
57+
58+
/**
59+
* Ensure the Engine exists by checking for it and if not there creating it.
60+
*
61+
* @param $name
62+
*/
63+
public function ensureEngine($name)
64+
{
65+
$this->setEngine($name);
66+
67+
try {
68+
$this->getEngine();
69+
} catch (\Elastic\EnterpriseSearch\Exception\ClientErrorResponseException $e) {
70+
$this->client->createEngine(new CreateEngine(new Engine($name)));
71+
}
72+
}
73+
74+
/**
75+
* Ensure the Engine exists by checking for it and if not there creating it.
76+
*
77+
* @param $name
78+
*/
79+
public function search($name)
80+
{
81+
$this->setEngine($name);
82+
83+
try {
84+
$this->getEngine();
85+
} catch (\Elastic\EnterpriseSearch\Exception\ClientErrorResponseException $e) {
86+
$this->client->search(new Search($name, new SearchRequestParams()));
87+
}
88+
}
89+
90+
/*
91+
* Flush the engine
92+
*/
93+
public function flushEngine($name = null)
94+
{
95+
if ($name) {
96+
$this->setEngine($name);
97+
}
98+
99+
$this->deleteEngine();
100+
$this->createEngine();
101+
}
102+
103+
/**
104+
* Dynamically call the Elastic client instance. Add the engine name to methods that require it.
105+
*
106+
* @param string $method
107+
* @param array $parameters
108+
*
109+
* @return mixed
110+
* @throws EngineNotInitialisedException
111+
*/
112+
public function __call($method, $parameters)
113+
{
114+
if (!method_exists($this->client, $method)) {
115+
throw new \BadMethodCallException($method . ' method not found on ' . get_class($this->client));
116+
}
117+
118+
if ($method !== 'listEngines' && !$this->engine) {
119+
throw new EngineNotInitialisedException('Unable to proxy call to Elastic App Client, no Engine initialised');
120+
}
121+
122+
if ($method !== 'listEngines' && $this->engine) {
123+
array_unshift($parameters, $this->engine);
124+
}
125+
126+
return $this->client->$method(...$parameters);
127+
}
128+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Chrysanthos\ScoutElasticAppSearch;
4+
5+
class EngineNotInitialisedException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)