|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\ClientBuilder; |
|
6
|
|
|
use Isswp101\Persimmon\Collection\IElasticsearchCollection; |
|
7
|
|
|
use Isswp101\Persimmon\DI\Container; |
|
8
|
|
|
use Isswp101\Persimmon\DI\DI; |
|
9
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
|
10
|
|
|
use Isswp101\Persimmon\Relationship\BelongsToRelationship; |
|
11
|
|
|
use Isswp101\Persimmon\Relationship\HasManyRelationship; |
|
12
|
|
|
use Isswp101\Persimmon\Repository\ElasticsearchRepository; |
|
13
|
|
|
use Isswp101\Persimmon\Repository\RuntimeCacheRepository; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @method static static|null find(string $id, array $columns = []) |
|
17
|
|
|
* @method static IElasticsearchCollection all(IQueryBuilder $query, callable $callback = null) |
|
18
|
|
|
*/ |
|
19
|
|
|
class ElasticsearchModel extends Eloquent implements IElasticsearchModel |
|
20
|
|
|
{ |
|
21
|
|
|
protected static function di(): Container |
|
22
|
|
|
{ |
|
23
|
|
|
$container = DI::make('elasticsearch'); |
|
24
|
|
|
if ($container == null) { |
|
25
|
|
|
DI::bind('elasticsearch', function () { |
|
26
|
|
|
$client = ClientBuilder::create()->build(); |
|
27
|
|
|
return new Container(new ElasticsearchRepository($client), new RuntimeCacheRepository()); |
|
28
|
|
|
}, DI::SINGLETON); |
|
29
|
|
|
$container = DI::make('elasticsearch'); |
|
30
|
|
|
} |
|
31
|
|
|
return $container; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function belongsTo(string $class): BelongsToRelationship |
|
35
|
|
|
{ |
|
36
|
|
|
return new BelongsToRelationship($this, $class); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function hasMany(string $class): HasManyRelationship |
|
40
|
|
|
{ |
|
41
|
|
|
return new HasManyRelationship($this, $class); |
|
42
|
|
|
} |
|
43
|
|
|
} |